1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.creadur.whisker.cli; |
20 | |
|
21 | |
import org.apache.commons.cli.CommandLine; |
22 | |
import org.apache.commons.cli.CommandLineParser; |
23 | |
import org.apache.commons.cli.GnuParser; |
24 | |
import org.apache.commons.cli.HelpFormatter; |
25 | |
import org.apache.commons.cli.MissingOptionException; |
26 | |
import org.apache.commons.cli.Options; |
27 | |
import org.apache.commons.cli.ParseException; |
28 | |
import org.apache.creadur.whisker.app.Act; |
29 | |
import org.apache.creadur.whisker.app.Whisker; |
30 | |
import org.apache.creadur.whisker.app.load.StreamableResourceFactory; |
31 | |
import org.apache.creadur.whisker.app.out.WriteResultsToSystemOutFactory; |
32 | |
import org.apache.creadur.whisker.out.velocity.VelocityEngine; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public final class Main { |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
private static final String HELP_HEADER = "\nPass " + |
43 | |
"the descriptor containing license meta-data " + |
44 | |
"and the act to be done.\n"; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
private static final String HELP_FOOTER = |
50 | |
"\nApache Whisker assists assembled applications " + |
51 | |
"maintain correct legal documentation. " + |
52 | |
"Whisker is part of the " + |
53 | |
"Apache Creadur suite of tools for " + |
54 | |
"auditing and comprehending software distributions, " + |
55 | |
"and is open source community developed software. " + |
56 | |
"Get involved at http://www.apache.org.\n\n"; |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private static final String APP_NAME = "apache-whisker-cli"; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private static final int SYSTEM_EXIT_OK = 0; |
67 | |
|
68 | |
private static final int SYSTEM_EXIT_CLI_PARSE_FAILED = 1; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public static void main(final String[] args) throws Exception { |
76 | 0 | System.exit(new Main(app()).run(args)); |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private static Whisker app() { |
84 | 0 | return new Whisker(); |
85 | |
} |
86 | |
|
87 | |
|
88 | |
private final Whisker whisker; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public Main(final Whisker whisker) { |
95 | 28 | super(); |
96 | 28 | this.whisker = whisker; |
97 | 28 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
private CommandLineParser parser() { |
105 | 60 | return new GnuParser(); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
public CommandLine parse(final String[] args) throws ParseException { |
115 | 52 | return parser().parse(options(), args); |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public Whisker configure(final String[] args) throws ParseException { |
125 | 52 | return configure(parse(args)); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private Whisker configure( |
136 | |
final CommandLine commandLine) throws MissingOptionException { |
137 | 46 | whisker.setEngine(new VelocityEngine(new SystemLog())); |
138 | 46 | whisker.setSource(CommandLineOption.SOURCE.getOptionValue(commandLine)); |
139 | 46 | whisker.setLicenseDescriptor( |
140 | |
new StreamableResourceFactory().streamFromResource( |
141 | |
licenseDescriptorName(commandLine))); |
142 | 46 | whisker.setWriterFactory(new WriteResultsToSystemOutFactory()); |
143 | 46 | if (CommandLineOption.ACT_TO_AUDIT.isSetOn(commandLine)) { |
144 | 18 | whisker.setAct(Act.AUDIT); |
145 | 28 | } else if (CommandLineOption.ACT_TO_GENERATE.isSetOn(commandLine)) { |
146 | 24 | whisker.setAct(Act.GENERATE); |
147 | 4 | } else if (CommandLineOption.ACT_TO_SKELETON.isSetOn(commandLine)) { |
148 | 4 | whisker.setAct(Act.SKELETON); |
149 | |
} |
150 | |
|
151 | 46 | if (whisker.getSource() == null |
152 | |
&& whisker.getAct().isSourceRequired()) { |
153 | 2 | throw new MissingOptionException("-" |
154 | |
+ CommandLineOption.SOURCE.getShortName() + " " |
155 | |
+ CommandLineOption.SOURCE.getDescription()); |
156 | |
} |
157 | 44 | return whisker; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
private String licenseDescriptorName(final CommandLine commandLine) { |
167 | 46 | return CommandLineOption.LICENSE_DESCRIPTION |
168 | |
.getOptionValue(commandLine); |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public int run(final String[] args) throws Exception { |
178 | |
try { |
179 | 0 | if (printHelp(args)) { |
180 | 0 | help(); |
181 | |
} else { |
182 | 0 | configure(args).act(); |
183 | |
} |
184 | 0 | return SYSTEM_EXIT_OK; |
185 | 0 | } catch (ParseException e) { |
186 | 0 | System.out.println(); |
187 | 0 | System.out.println(e.getMessage()); |
188 | 0 | System.out.println(); |
189 | 0 | help(); |
190 | 0 | return SYSTEM_EXIT_CLI_PARSE_FAILED; |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public boolean printHelp(String[] args) throws ParseException { |
202 | 8 | final CommandLineOption help = CommandLineOption.PRINT_HELP; |
203 | |
try { |
204 | 8 | return help.isSetOn( |
205 | |
parser().parse(new Options().addOption( |
206 | |
help.create()), args)); |
207 | 6 | } catch (ParseException e) { |
208 | 6 | return false; |
209 | |
} |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
private void help() { |
216 | 0 | final HelpFormatter formatter = new HelpFormatter(); |
217 | 0 | formatter.printHelp(APP_NAME, HELP_HEADER, options(), HELP_FOOTER, true); |
218 | 0 | } |
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
private Options options() { |
225 | 52 | return CommandLineOption.options(); |
226 | |
} |
227 | |
} |