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