Skip to content

Commit 881dbbf

Browse files
authored
Merge pull request #25 from marques-work/honor-stdin
Plugin CLI should accept STDIN input for syntax check
2 parents 036d0c6 + bdf273f commit 881dbbf

File tree

9 files changed

+126
-78
lines changed

9 files changed

+126
-78
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ Then to validate your `gopipeline.json` file run something like:
9191
java -jar /usr/lib/gocd-json-plugin/json-config-plugin.jar syntax mypipe.gopipeline.json
9292
```
9393

94+
## Usage with `gocd` command-line helper
95+
96+
This is a product in development, so its command syntax is not stable and there are no distributed binaries yet.
97+
98+
The `gocd` tool is built in [golang](https://golang.org/) so you will need to familiarize yourself with how to set up your [go workspace](https://golang.org/doc/code.html#Workspaces).
99+
100+
Build the `gocd` binary:
101+
102+
```bash
103+
go get github.com/gocd-contrib/gocd-cli
104+
cd ${GOPATH:-~/go}/src/github.com/gocd-contrib/gocd-cli
105+
./build.sh
106+
```
107+
108+
Follow the steps on [https://github.com/gocd-contrib/gocd-cli](https://github.com/gocd-contrib/gocd-cli) to install the plugin jar to the correct place.
109+
110+
Then:
111+
112+
```bash
113+
./gocd configrepo check -i json.config.plugin /path/to/your-pipeline.gopipeline.json
114+
```
115+
94116
## Usage with IDE and docker
95117

96118
[IDE](https://github.com/ai-traders/ide) is a bash script, a cli wrapper around docker to help with running development tasks in docker.
@@ -746,7 +768,7 @@ Optionally any task can have `run_if` and `on_cancel`.
746768
"docs",
747769
"install"
748770
],
749-
"working_directory": null
771+
"working_directory": null
750772
}
751773
```
752774

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group = 'com.thoughtworks.go'
2-
version = '0.3.1'
2+
version = '0.3.2'
33

44
apply plugin: 'java'
55

src/com.tw.go.config.json/ConfigDirectoryParser.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import com.google.gson.JsonElement;
44

55
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileNotFoundException;
68

79
class ConfigDirectoryParser {
810
private ConfigDirectoryScanner scanner;
9-
private JsonFileParser parser;
11+
private JsonConfigParser parser;
1012
private String pipelinePattern;
1113
private String environmentPattern;
1214

13-
ConfigDirectoryParser(ConfigDirectoryScanner scanner, JsonFileParser parser, String pipelinePattern, String environmentPattern) {
15+
ConfigDirectoryParser(ConfigDirectoryScanner scanner, JsonConfigParser parser, String pipelinePattern, String environmentPattern) {
1416
this.scanner = scanner;
1517
this.parser = parser;
1618
this.pipelinePattern = pipelinePattern;
@@ -19,18 +21,26 @@ class ConfigDirectoryParser {
1921

2022
JsonConfigCollection parseDirectory(File baseDir) {
2123
JsonConfigCollection config = new JsonConfigCollection();
22-
for (String environmentFile : scanner.getFilesMatchingPattern(baseDir, environmentPattern)) {
23-
JsonElement environment = JsonFileParser.processFile(config, parser, new File(baseDir, environmentFile));
24-
if (null != environment) {
25-
config.addEnvironment(environment, environmentFile);
24+
File currentFile;
25+
26+
try {
27+
for (String environmentFile : scanner.getFilesMatchingPattern(baseDir, environmentPattern)) {
28+
currentFile = new File(baseDir, environmentFile);
29+
JsonElement environment = JsonConfigParser.parseStream(config, parser, new FileInputStream(currentFile), currentFile.getPath());
30+
if (null != environment) {
31+
config.addEnvironment(environment, environmentFile);
32+
}
2633
}
27-
}
2834

29-
for (String pipelineFile : scanner.getFilesMatchingPattern(baseDir, pipelinePattern)) {
30-
JsonElement pipeline = JsonFileParser.processFile(config, parser, new File(baseDir, pipelineFile));
31-
if (null != pipeline) {
32-
config.addPipeline(pipeline, pipelineFile);
35+
for (String pipelineFile : scanner.getFilesMatchingPattern(baseDir, pipelinePattern)) {
36+
currentFile = new File(baseDir, pipelineFile);
37+
JsonElement pipeline = JsonConfigParser.parseStream(config, parser, new FileInputStream(currentFile), currentFile.getPath());
38+
if (null != pipeline) {
39+
config.addPipeline(pipeline, pipelineFile);
40+
}
3341
}
42+
} catch (FileNotFoundException e) {
43+
config.addError(new PluginError(e.getMessage()));
3444
}
3545

3646
return config;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.tw.go.config.json;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParseException;
6+
import com.google.gson.JsonParser;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.io.Reader;
12+
13+
import static java.lang.String.format;
14+
15+
public class JsonConfigParser {
16+
17+
private final JsonParser parser;
18+
19+
public JsonConfigParser() {
20+
parser = new JsonParser();
21+
}
22+
23+
public static JsonElement parseStream(JsonConfigCollection result, InputStream input, String location) {
24+
return parseStream(result, new JsonConfigParser(), input, location);
25+
}
26+
27+
public static JsonElement parseStream(JsonConfigCollection result, JsonConfigParser parser, InputStream input, String location) {
28+
try (InputStreamReader contentReader = new InputStreamReader(input)) {
29+
if (input.available() < 1) {
30+
result.addError(new PluginError("File is empty", location));
31+
return null;
32+
}
33+
34+
JsonElement el = parser.parse(contentReader);
35+
36+
if (el == null || el.isJsonNull()) {
37+
PluginError error = new PluginError("File is empty", location);
38+
result.addError(error);
39+
} else if (el.equals(new JsonObject())) {
40+
PluginError error = new PluginError("Definition is empty", location);
41+
result.addError(error);
42+
} else {
43+
return el;
44+
}
45+
} catch (IOException | JsonParseException e) {
46+
PluginError error = new PluginError(format("Failed to parse file as JSON: %s", e.getMessage()), location);
47+
result.addError(error);
48+
}
49+
50+
return null;
51+
}
52+
53+
private JsonElement parse(Reader reader) {
54+
return parser.parse(reader);
55+
}
56+
}

src/com.tw.go.config.json/JsonConfigPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private GoPluginApiResponse handleParseDirectoryRequest(GoPluginApiRequest reque
115115
ParsedRequest parsed = ParsedRequest.parse(request);
116116
File baseDir = new File(parsed.getStringParam("directory"));
117117

118-
JsonFileParser parser = new JsonFileParser();
118+
JsonConfigParser parser = new JsonConfigParser();
119119
PluginSettings settings = getPluginSettings();
120120
ConfigDirectoryScanner scanner = new AntDirectoryScanner();
121121

src/com.tw.go.config.json/JsonFileParser.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/com.tw.go.config.json/cli/JsonPluginCli.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import com.beust.jcommander.ParameterException;
55
import com.google.gson.JsonObject;
66
import com.tw.go.config.json.JsonConfigCollection;
7-
import com.tw.go.config.json.JsonFileParser;
7+
import com.tw.go.config.json.JsonConfigParser;
88
import com.tw.go.config.json.PluginError;
99

1010
import java.io.File;
11+
import java.io.FileInputStream;
12+
import java.io.FileNotFoundException;
13+
import java.io.InputStream;
1114

1215
import static java.lang.String.format;
1316

@@ -44,24 +47,38 @@ public static void main(String[] args) {
4447
JsonConfigCollection collection = new JsonConfigCollection();
4548
JsonObject result = collection.getJsonObject();
4649

47-
File file = new File(syntax.file);
50+
String location = getLocation(syntax.file);
4851

4952
try {
50-
JsonFileParser.processFile(collection, new JsonFileParser(), file);
53+
JsonConfigParser.parseStream(collection, getFileAsStream(syntax.file), location);
5154
} catch (Exception e) {
52-
collection.addError(new PluginError(e.getMessage(), file.getAbsolutePath()));
55+
collection.addError(new PluginError(e.getMessage(), location));
5356
}
5457

5558
result.remove("environments");
5659
result.remove("pipelines");
5760

5861
if (collection.getErrors().size() > 0) {
59-
die(syntax.quiet, 1, result.toString());
62+
die(1, result.toString());
6063
} else {
61-
die(syntax.quiet, 0, "OK");
64+
die(0, "OK");
6265
}
6366
}
6467

68+
private static String getLocation(String file) {
69+
return "-".equals(file) ? "<STDIN>" : file;
70+
}
71+
72+
private static InputStream getFileAsStream(String file) {
73+
InputStream s = null;
74+
try {
75+
s = "-".equals(file) ? System.in : new FileInputStream(new File(".", file));
76+
} catch (FileNotFoundException e) {
77+
die(1, e.getMessage());
78+
}
79+
return s;
80+
}
81+
6582
private static void echo(String message, Object... items) {
6683
System.out.println(format(message, items));
6784
}
@@ -79,13 +96,6 @@ private static void die(int exitCode, String message, Object... items) {
7996
System.exit(exitCode);
8097
}
8198

82-
private static void die(boolean quietly, int exitCode, String message, Object... items) {
83-
if (quietly) {
84-
System.exit(exitCode);
85-
}
86-
die(exitCode, message, items);
87-
}
88-
8999
private static void printUsageAndExit(int exitCode, JCommander cmd, String command) {
90100
StringBuilder out = new StringBuilder();
91101
if (null == command) {

src/com.tw.go.config.json/cli/SyntaxCmd.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import com.beust.jcommander.Parameter;
44

55
class SyntaxCmd {
6-
@Parameter(names = {"--quiet", "-q"}, description = "Silence output; validation indicated only by exit status")
7-
boolean quiet;
8-
96
@Parameter(names = {"--help", "-h"}, help = true, description = "Print this help message")
107
boolean help;
118

test/com.tw.go.config.json/ConfigDirectoryParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void SetUp() throws Exception {
2828
FileUtils.deleteDirectory(directory);
2929

3030
parser = new ConfigDirectoryParser(
31-
new AntDirectoryScanner(), new JsonFileParser(),
31+
new AntDirectoryScanner(), new JsonConfigParser(),
3232
JsonConfigPlugin.DEFAULT_PIPELINE_PATTERN,
3333
JsonConfigPlugin.DEFAULT_ENVIRONMENT_PATTERN);
3434

0 commit comments

Comments
 (0)