1 package org
.argeo
.slc
.cli
;
4 import java
.util
.Properties
;
6 import org
.apache
.commons
.cli
.CommandLine
;
7 import org
.apache
.commons
.cli
.CommandLineParser
;
8 import org
.apache
.commons
.cli
.GnuParser
;
9 import org
.apache
.commons
.cli
.HelpFormatter
;
10 import org
.apache
.commons
.cli
.Option
;
11 import org
.apache
.commons
.cli
.OptionBuilder
;
12 import org
.apache
.commons
.cli
.Options
;
13 import org
.apache
.commons
.cli
.ParseException
;
14 import org
.argeo
.slc
.core
.SlcException
;
15 import org
.springframework
.core
.io
.FileSystemResource
;
17 public class SlcMain
{
22 public final static String MODE_SINGLE
= "single";
23 public final static String MODE_AGENT
= "agent";
25 private final static Option modeOpt
= OptionBuilder
.withLongOpt("mode")
26 .withArgName("mode").hasArg().isRequired().withDescription(
27 "SLC execution mode, one of: " + listModeValues()).create(
30 private final static Option propertyOpt
= OptionBuilder
.withLongOpt(
31 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
32 .withValueSeparator(',').withDescription(
33 "use value for given property").create('p');
35 private final static Option scriptOpt
= OptionBuilder
.withLongOpt("script")
36 .withArgName("script").hasArg().withType(File
.class)
37 .withDescription("SLC script to execute").create('s');
39 private final static Options options
;
41 private final static String commandName
= "slc";
44 options
= new Options();
45 options
.addOption(modeOpt
);
46 options
.addOption(scriptOpt
);
47 options
.addOption(propertyOpt
);
50 public static void main(String
[] args
) {
52 Properties properties
= new Properties();
56 CommandLineParser clParser
= new GnuParser();
57 CommandLine cl
= clParser
.parse(options
, args
);
60 String modeStr
= cl
.getOptionValue(modeOpt
.getOpt());
62 mode
= Mode
.valueOf(modeStr
);
63 } catch (IllegalArgumentException e
) {
64 throw new SlcException("Unrecognized mode '" + modeStr
+ "'", e
);
66 System
.out
.println("Mode: " + mode
);
69 if (mode
.equals(Mode
.single
)) {
70 if (!cl
.hasOption(scriptOpt
.getOpt()))
71 throw new SlcException("Mode " + Mode
.single
72 + " requires option '" + scriptOpt
.getLongOpt()
74 script
= (File
) cl
.getOptionObject(scriptOpt
.getOpt());
76 System
.out
.println("Script: " + script
.getAbsolutePath());
79 if (cl
.hasOption(propertyOpt
.getOpt())) {
80 for (String property
: cl
.getOptionValues(propertyOpt
.getOpt())) {
81 addProperty(properties
, property
);
84 System
.out
.print("Properties: " + properties
);
85 } catch (ParseException e
) {
86 System
.err
.println("Problem with command line arguments. "
89 } catch (SlcException e
) {
90 System
.err
.println(e
.getMessage());
94 if (mode
.equals(Mode
.single
)) {
95 DefaultSlcRuntime runtime
= new DefaultSlcRuntime();
96 runtime
.executeScript(new FileSystemResource(script
), properties
,
101 public static void printUsage() {
102 new HelpFormatter().printHelp(commandName
, options
, true);
105 private static String
listModeValues() {
106 StringBuffer buf
= new StringBuffer("");
107 for (Mode mode
: Mode
.values()) {
108 buf
.append(mode
).append(", ");
110 String str
= buf
.toString();
111 // unsafe, but there will be at least one value in the enum
112 return str
.substring(0, str
.length() - 2);
115 private static void addProperty(Properties properties
, String property
) {
116 int eqIndex
= property
.indexOf('=');
118 throw new SlcException("Badly formatted property " + property
);
121 String key
= property
.substring(0, eqIndex
);
122 String value
= property
.substring(eqIndex
+ 1);
123 properties
.setProperty(key
, value
);
126 properties
.setProperty(property
, "true");