1
|
|
|
/* |
2
|
|
|
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender; |
17
|
|
|
|
18
|
|
|
import com.strider.datadefender.database.metadata.TableMetaData.ColumnMetaData; |
19
|
|
|
import com.strider.datadefender.requirement.file.Generator; |
20
|
|
|
import java.io.File; |
21
|
|
|
import java.util.List; |
22
|
|
|
|
23
|
|
|
import java.util.Set; |
24
|
|
|
import java.util.concurrent.Callable; |
25
|
|
|
import java.util.stream.Collectors; |
26
|
|
|
|
27
|
|
|
import picocli.CommandLine; |
28
|
|
|
import picocli.CommandLine.ArgGroup; |
29
|
|
|
import picocli.CommandLine.Command; |
30
|
|
|
import picocli.CommandLine.Model.CommandSpec; |
31
|
|
|
import picocli.CommandLine.Option; |
32
|
|
|
import picocli.CommandLine.Spec; |
33
|
|
|
|
34
|
|
|
import lombok.extern.log4j.Log4j2; |
35
|
|
|
import lombok.Getter; |
36
|
|
|
import lombok.Setter; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* "discover" picocli subcommand, configures and executes the data discoverer. |
40
|
|
|
* |
41
|
|
|
* TODO(ZB): Look into setting up command mixins sp dbconfig options don't have |
42
|
|
|
* to appear before subcommands [https://picocli.info/#_mixins] |
43
|
|
|
* |
44
|
|
|
* @author Zaahid Bateson |
45
|
|
|
*/ |
46
|
|
|
@Command( |
47
|
|
|
name = "discover", |
48
|
|
|
version = "1.0", |
49
|
|
|
description = "Run data discovery utility", |
50
|
|
|
mixinStandardHelpOptions = true, |
51
|
|
|
subcommands = { |
52
|
|
|
DiscoverColumns.class, |
53
|
|
|
DiscoverData.class, |
54
|
|
|
DiscoverFiles.class |
55
|
|
|
}, |
56
|
|
|
subcommandsRepeatable = true |
57
|
|
|
) |
58
|
|
|
@Log4j2 |
59
|
|
|
public class Discover implements Callable<Integer> { |
60
|
|
|
|
61
|
|
|
@Getter |
62
|
|
|
@Setter |
63
|
|
|
@ArgGroup(exclusive = false, multiplicity = "0..1", heading = "Database connection settings%n") |
64
|
|
|
private DbConfig dbConfig; |
65
|
|
|
|
66
|
|
|
@Option(names = { "-o", "--output" }, description = "Generate a requirements xml file and write it out to the specified file") |
67
|
|
|
@Setter |
68
|
|
|
private File outputFile; |
69
|
|
|
|
70
|
|
|
@Spec |
71
|
|
|
private CommandSpec spec; |
72
|
|
|
|
73
|
|
|
public void afterSubcommand() { |
74
|
|
|
List<IRequirementCommand> subcommands = spec |
75
|
|
|
.commandLine() |
76
|
|
|
.getParseResult() |
77
|
|
|
.subcommands() |
78
|
|
|
.stream() |
79
|
|
|
.map((p) -> p.commandSpec().userObject()) |
80
|
|
|
.filter((u) -> (u instanceof IRequirementCommand)) |
81
|
|
|
.map((r) -> (IRequirementCommand) r) |
82
|
|
|
.collect(Collectors.toList()); |
83
|
|
|
if (subcommands.stream().allMatch((r) -> r.getColumnMetaData() != null)) { |
84
|
|
|
Set<ColumnMetaData> combined = subcommands.stream().flatMap((r) -> r.getColumnMetaData().stream()).collect(Collectors.toSet()); |
85
|
|
|
try { |
86
|
|
|
Generator.write(Generator.create(combined), outputFile); |
87
|
|
|
} catch (Exception e) { |
88
|
|
|
log.error("Error creating or writing to an output xml file", e); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
@Override |
94
|
|
|
public Integer call() throws Exception { |
95
|
|
|
CommandLine.usage(this, System.out); |
96
|
|
|
return 0; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|