Skip to content

Commit

Permalink
add delimited list format
Browse files Browse the repository at this point in the history
  • Loading branch information
takemikami committed Jun 20, 2018
1 parent df9acf4 commit 661499d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,25 @@ run sample workflow.
```
digdag run sample.dig
```

## Operators

### sh_result>: Shell scripts and store output

sh_result> operator runs a shell scripts and set output to digdag store.

#### sh_result>: COMMAND [ARGS...]

Name of the command to run.

#### destination_variable: Name

Specifies a digdag-variable to store the standard output of script in.

#### stdout_format: Name

Type of standard output format.

- json-list-map
- newline-delimited
- space-delimited
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = 'com.github.takemikami'
version = '0.0.1'
version = '0.0.2-SNAPSHOT'

def digdagVersion = '0.9.25'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -160,6 +161,44 @@ public static void collectEnvironmentVariables(Map<String, String> env,
}
}

public static Object createVariableObjectFromStdout(
String stdoutData,
String stdoutFormat
) {
// case of '*-delimited'
String delimiter = null;
if ("newline-delimited".equals(stdoutFormat)) {
delimiter = "\n";
} else if ("space-delimited".equals(stdoutFormat)) {
delimiter = "\n| ";
}
if (delimiter != null) {
List<String> listObj = new LinkedList<>();
for (String s : stdoutData.split(delimiter)) {
if (s.trim().length() > 0) {
listObj.add(s.trim());
}
}
return listObj;
}

if ("json-list-map".equals(stdoutFormat)) {
// stdout is json format
List<Map<String, Object>> jsonObj;
try {
ObjectMapper mapper = new ObjectMapper();
jsonObj = mapper
.readValue(stdoutData, new TypeReference<ArrayList<HashMap<String, Object>>>() {
});
} catch (IOException e) {
throw Throwables.propagate(e);
}
return jsonObj;

}
return null;
}

private static boolean isValidEnvKey(String key) {
return VALID_ENV_KEY.matcher(key).matches();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ public void testType() throws Exception {
assertEquals("sh_result", factory.getType());
}

@Test
public void testCreateVariableObjectFromStdoutJsonListMap() throws Exception {
String stdoutData = "[{\"id\": \"001\",\"name\": \"hoge\"},{\"id\": \"002\",\"name\": \"fuga\"}]";
List<Map<String, Object>> obj = (List<Map<String, Object>>) ShResultOperatorFactory
.createVariableObjectFromStdout(stdoutData, "json-list-map");
assertEquals(2, obj.size());
assertEquals("001", obj.get(0).get("id"));
assertEquals("hoge", obj.get(0).get("name"));
}

@Test
public void testCreateVariableObjectFromStdoutNewlineDelimited() throws Exception {
String stdoutData = "hoge\nfuga\n";
List<String> obj = (List<String>) ShResultOperatorFactory
.createVariableObjectFromStdout(stdoutData, "newline-delimited");
assertEquals(2, obj.size());
assertEquals("hoge", obj.get(0));
}

@Test
public void testCreateVariableObjectFromStdoutSpaceDelimited() throws Exception {
String stdoutData = "hoge fuga\nfoo bar";
List<String> obj = (List<String>) ShResultOperatorFactory
.createVariableObjectFromStdout(stdoutData, "space-delimited");
System.out.println(obj);
assertEquals(4, obj.size());
assertEquals("hoge", obj.get(0));
assertEquals("bar", obj.get(3));
}

@Test
public void testCollectEnvironmentVariablesSuccess() throws Exception {
Map<String, String> env = new HashMap<>();
Expand Down

0 comments on commit 661499d

Please sign in to comment.