|
4 | 4 | */ |
5 | 5 | package io.github.pastorgl.datacooker.cli; |
6 | 6 |
|
7 | | -import io.github.pastorgl.datacooker.config.InvalidConfigurationException; |
8 | | -import io.github.pastorgl.datacooker.scripting.VariablesContext; |
9 | 7 | import org.apache.commons.cli.*; |
10 | | -import org.apache.hadoop.fs.Path; |
11 | | -import org.apache.spark.api.java.JavaSparkContext; |
12 | | -import scala.Tuple2; |
13 | 8 |
|
14 | | -import java.io.StringReader; |
15 | | -import java.util.*; |
| 9 | +import java.util.ListIterator; |
16 | 10 |
|
17 | 11 | public class Configuration { |
18 | 12 | protected Options options; |
19 | 13 | protected CommandLine commandLine; |
20 | 14 |
|
21 | | - private HelpFormatter hf = new HelpFormatter(); |
| 15 | + private final HelpFormatter hf = new HelpFormatter(); |
22 | 16 |
|
23 | 17 | public Configuration() { |
24 | 18 | options = new Options(); |
25 | 19 | hf.setOptionComparator(null); |
26 | 20 |
|
27 | | - addOption("h", "help", false, "Print a list of command line options and exit"); |
28 | | - addOption("s", "script", true, "TDL4 script file"); |
29 | | - addOption("d", "dry", false, "Dry run: only check script syntax and print errors to console, if found"); |
| 21 | + addOption("h", "help", false, "Print full list of command line options and exit"); |
| 22 | + addOption("s", "script", true, "TDL4 script file. Mandatory for batch modes"); |
30 | 23 | addOption("v", "variablesFile", true, "Path to variables file, name=value pairs per each line"); |
31 | 24 | addOption("V", "variables", true, "Pass contents of variables file encoded as Base64"); |
32 | | - addOption("l", "local", false, "Run in local mode (its options have no effect otherwise)"); |
| 25 | + addOption("l", "local", false, "Run in local batch mode (cluster batch mode otherwise)"); |
| 26 | + addOption("d", "dry", false, "-l: Dry run (only check script syntax and print errors to console, if found)"); |
33 | 27 | addOption("m", "driverMemory", true, "-l: Driver memory, by default Spark uses 1g"); |
34 | 28 | addOption("u", "sparkUI", false, "-l: Enable Spark UI, by default it is disabled"); |
35 | 29 | addOption("L", "localCores", true, "-l: Set cores #, by default * (all cores)"); |
36 | | - addOption("R", "repl", false, "Run in local mode with interactive REPL interface. -s is optional"); |
37 | | - addOption("i", "history", true, "-R: Set history file location"); |
38 | | - } |
39 | | - |
40 | | - public VariablesContext variables(JavaSparkContext context) throws Exception { |
41 | | - StringBuilder variablesSource = new StringBuilder(); |
42 | | - if (hasOption("v")) { |
43 | | - String variablesFile = getOptionValue("v"); |
44 | | - |
45 | | - Path sourcePath = new Path(variablesFile); |
46 | | - String qualifiedPath = sourcePath.getFileSystem(context.hadoopConfiguration()).makeQualified(sourcePath).toString(); |
47 | | - |
48 | | - int lastSlash = variablesFile.lastIndexOf('/'); |
49 | | - variablesFile = (lastSlash < 0) ? variablesFile : variablesFile.substring(0, lastSlash); |
50 | | - |
51 | | - variablesSource.append(context.wholeTextFiles(variablesFile) |
52 | | - .filter(t -> t._1.equals(qualifiedPath)) |
53 | | - .map(Tuple2::_2) |
54 | | - .first()); |
55 | | - } |
56 | | - if (hasOption("V")) { |
57 | | - variablesSource.append("\n"); |
58 | | - variablesSource.append(new String(Base64.getDecoder().decode(getOptionValue("V")))); |
59 | | - } |
60 | | - |
61 | | - Properties properties = new Properties(); |
62 | | - if (variablesSource.length() > 0) { |
63 | | - properties.load(new StringReader(variablesSource.toString())); |
64 | | - } |
65 | | - |
66 | | - Map<String, Object> variables = new HashMap<>(); |
67 | | - for (Map.Entry e : properties.entrySet()) { |
68 | | - String key = String.valueOf(e.getKey()); |
69 | | - Object v = e.getValue(); |
70 | | - String value = String.valueOf(v); |
71 | | - |
72 | | - int last = value.length() - 1; |
73 | | - if ((value.indexOf('[') == 0) && (value.lastIndexOf(']') == last)) { |
74 | | - value = value.substring(1, last); |
75 | | - |
76 | | - if (value.contains("'")) { |
77 | | - boolean inString = false; |
78 | | - List<String> strings = new ArrayList<>(); |
79 | | - StringBuilder cur = null; |
80 | | - for (int i = 0, len = value.length(); i < len; i++) { |
81 | | - char c = value.charAt(i); |
82 | | - if (inString) { |
83 | | - if (c != '\'') { |
84 | | - cur.append(c); |
85 | | - } else { // c == ' |
86 | | - if ((i + 1) < len) { |
87 | | - if (value.charAt(i + 1) != '\'') { |
88 | | - inString = false; |
89 | | - strings.add(cur.toString()); |
90 | | - } else { |
91 | | - cur.append("'"); |
92 | | - i++; |
93 | | - } |
94 | | - } else { |
95 | | - strings.add(cur.toString()); |
96 | | - } |
97 | | - } |
98 | | - } else { |
99 | | - if (c == '\'') { |
100 | | - inString = true; |
101 | | - cur = new StringBuilder(); |
102 | | - } |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - v = strings.toArray(); |
107 | | - } else { |
108 | | - String[] vv = value.split(","); |
109 | | - v = Arrays.stream(vv).map(vvv -> Double.parseDouble(vvv.trim())).toArray(); |
110 | | - } |
111 | | - } else if ((value.indexOf('\'') == 0) && (value.lastIndexOf('\'') == last)) { |
112 | | - v = value.substring(1, last); |
113 | | - } |
114 | | - variables.put(key, v); |
115 | | - } |
116 | | - |
117 | | - VariablesContext variablesContext = new VariablesContext(); |
118 | | - variablesContext.putAll(variables); |
119 | | - return variablesContext; |
120 | | - } |
121 | | - |
122 | | - public String script(JavaSparkContext context, String sourceFile) { |
123 | | - try { |
124 | | - Path sourcePath = new Path(sourceFile); |
125 | | - String qualifiedPath = sourcePath.getFileSystem(context.hadoopConfiguration()).makeQualified(sourcePath).toString(); |
126 | | - |
127 | | - int lastSlash = sourceFile.lastIndexOf('/'); |
128 | | - sourceFile = (lastSlash < 0) ? sourceFile : sourceFile.substring(0, lastSlash); |
129 | | - |
130 | | - return context.wholeTextFiles(sourceFile) |
131 | | - .filter(t -> t._1.equals(qualifiedPath)) |
132 | | - .map(Tuple2::_2) |
133 | | - .first(); |
134 | | - } catch (Exception e) { |
135 | | - throw new InvalidConfigurationException("Error while reading TDL4 script file"); |
136 | | - } |
| 30 | + addOption("R", "repl", false, "Run in local mode with interactive REPL interface. Implies -l. -s is optional"); |
| 31 | + addOption("i", "history", true, "-R, -r: Set history file location"); |
| 32 | + addOption("e", "serveRepl", false, "Start REPL server in local or cluster mode. -s is optional"); |
| 33 | + addOption("r", "remoteRepl", false, "Connect to a remote REPL server"); |
| 34 | + addOption("i", "host", true, "Use specified network address:\n" + |
| 35 | + "-e: to listen at (default is all)\n" + |
| 36 | + "-r: to connect to (in this case, mandatory parameter)"); |
| 37 | + addOption("p", "port", true, "-e, -r: Use specified port to listen at or connect to. Default is 9595"); |
137 | 38 | } |
138 | 39 |
|
139 | 40 | public void addOption(String opt, String longOpt, boolean hasArg, String description) { |
|
0 commit comments