]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
6184c5a7630a27b0b6dc1cca3099d92a5915ae48
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / SlcMain.java
1 package org.argeo.slc.cli;
2
3 import java.io.File;
4 import java.util.Properties;
5
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;
16
17 public class SlcMain {
18 public enum Mode {
19 single, agent
20 }
21
22 public final static String MODE_SINGLE = "single";
23 public final static String MODE_AGENT = "agent";
24
25 private final static Option modeOpt = OptionBuilder.withLongOpt("mode")
26 .withArgName("mode").hasArg().isRequired().withDescription(
27 "SLC execution mode, one of: " + listModeValues()).create(
28 'm');
29
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');
34
35 private final static Option scriptOpt = OptionBuilder.withLongOpt("script")
36 .withArgName("script").hasArg().withType(File.class)
37 .withDescription("SLC script to execute").create('s');
38
39 private final static Options options;
40
41 private final static String commandName = "slc";
42
43 static {
44 options = new Options();
45 options.addOption(modeOpt);
46 options.addOption(scriptOpt);
47 options.addOption(propertyOpt);
48 }
49
50 public static void main(String[] args) {
51 Mode mode = null;
52 Properties properties = new Properties();
53 File script = null;
54
55 try {
56 CommandLineParser clParser = new GnuParser();
57 CommandLine cl = clParser.parse(options, args);
58
59 // Mode
60 String modeStr = cl.getOptionValue(modeOpt.getOpt());
61 try {
62 mode = Mode.valueOf(modeStr);
63 } catch (IllegalArgumentException e) {
64 throw new SlcException("Unrecognized mode '" + modeStr + "'", e);
65 }
66 System.out.println("Mode: " + mode);
67
68 // Script
69 if (mode.equals(Mode.single)) {
70 if (!cl.hasOption(scriptOpt.getOpt()))
71 throw new SlcException("Mode " + Mode.single
72 + " requires option '" + scriptOpt.getLongOpt()
73 + "'");
74 script = (File) cl.getOptionObject(scriptOpt.getOpt());
75 }
76 System.out.println("Script: " + script.getAbsolutePath());
77
78 // Properties
79 if (cl.hasOption(propertyOpt.getOpt())) {
80 for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
81 addProperty(properties, property);
82 }
83 }
84 System.out.print("Properties: " + properties);
85 } catch (ParseException e) {
86 System.err.println("Problem with command line arguments. "
87 + e.getMessage());
88 printUsage();
89 } catch (SlcException e) {
90 System.err.println(e.getMessage());
91 printUsage();
92 }
93
94 if (mode.equals(Mode.single)) {
95 DefaultSlcRuntime runtime = new DefaultSlcRuntime();
96 runtime.executeScript(new FileSystemResource(script), properties,
97 null);
98 }
99 }
100
101 public static void printUsage() {
102 new HelpFormatter().printHelp(commandName, options, true);
103 }
104
105 private static String listModeValues() {
106 StringBuffer buf = new StringBuffer("");
107 for (Mode mode : Mode.values()) {
108 buf.append(mode).append(", ");
109 }
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);
113 }
114
115 private static void addProperty(Properties properties, String property) {
116 int eqIndex = property.indexOf('=');
117 if (eqIndex == 0)
118 throw new SlcException("Badly formatted property " + property);
119
120 if (eqIndex > 0) {
121 String key = property.substring(0, eqIndex);
122 String value = property.substring(eqIndex + 1);
123 properties.setProperty(key, value);
124
125 } else {
126 properties.setProperty(property, "true");
127 }
128
129 }
130 }