Skip to content

Commit 1318ba7

Browse files
author
Yang Guo
committed
support linux env varable for working dir
1 parent e5416f1 commit 1318ba7

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

platform-cmd-runner/src/main/java/com/flow/platform/cmd/CmdExecutor.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import com.flow.platform.domain.CmdResult;
2020
import com.flow.platform.util.DateUtil;
21+
import com.flow.platform.util.SystemUtil;
2122
import com.google.common.base.Strings;
2223
import com.google.common.collect.Lists;
2324

2425
import java.io.*;
2526
import java.lang.reflect.Field;
2627
import java.nio.file.Files;
28+
import java.nio.file.Path;
2729
import java.time.ZonedDateTime;
2830
import java.util.*;
2931
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -153,17 +155,17 @@ public CmdExecutor(final ProcListener procListener,
153155

154156
// check and init working dir
155157
if (workingDir != null) {
156-
File dir = new File(workingDir);
158+
Path dir = SystemUtil.replacePathWithEnv(workingDir);
157159

158-
if (!dir.exists()) {
160+
if (!Files.exists(dir)) {
159161
try {
160-
Files.createDirectories(dir.toPath());
162+
Files.createDirectories(dir);
161163
} catch (IOException e) {
162164
throw new IllegalArgumentException("Unable to create working dir: " + dir);
163165
}
164166
}
165167

166-
this.pBuilder.directory(dir);
168+
this.pBuilder.directory(dir.toFile());
167169
}
168170

169171
// init inputs env

platform-api/src/main/java/com/flow/platform/api/util/SystemUtil.java renamed to platform-util/src/main/java/com/flow/platform/util/SystemUtil.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.flow.platform.api.util;
17+
package com.flow.platform.util;
1818

1919
import java.io.File;
2020
import java.nio.file.Path;
@@ -26,6 +26,10 @@
2626
*/
2727
public class SystemUtil {
2828

29+
private final static char ENV_VAR_START_CHAR = '$';
30+
private final static char ENV_VAR_LEFT_BRACKET = '{';
31+
private final static char ENV_VAR_RIGHT_BRACKET = '}';
32+
2933
/**
3034
* Parse path with ${xxx} variable to absolute path
3135
*
@@ -38,19 +42,38 @@ public static Path replacePathWithEnv(String pathWithEnv) {
3842

3943
for (String pathItem : paths) {
4044
int index = pathItem.indexOf("$", 0);
41-
int leftBracket = pathItem.indexOf("{", 0);
42-
int rightBracket = pathItem.indexOf("}", 0);
4345

44-
if (index < 0 || leftBracket < 0 || rightBracket < 0) {
46+
if (index < 0) {
4547
path = Paths.get(path.toString(), pathItem);
4648
continue;
4749
}
4850

49-
String envName = pathItem.substring(leftBracket + 1, rightBracket);
50-
String envValue = System.getenv(envName);
51-
path = Paths.get(path.toString(), envValue);
51+
path = Paths.get(path.toString(), parseEnv(pathItem));
5252
}
5353

5454
return path;
5555
}
56+
57+
/**
58+
* Parse ${xx} variable to exact value
59+
*/
60+
public static String parseEnv(String env) {
61+
if (env == null) {
62+
throw new IllegalArgumentException();
63+
}
64+
65+
if (env.charAt(0) != ENV_VAR_START_CHAR) {
66+
throw new IllegalArgumentException();
67+
}
68+
69+
boolean hasBracket = env.charAt(1) == ENV_VAR_LEFT_BRACKET;
70+
env = env.substring(1);
71+
72+
if (!hasBracket) {
73+
return System.getenv(env);
74+
}
75+
76+
env = env.substring(1, env.length() - 1);
77+
return System.getenv(env);
78+
}
5679
}

platform-api/src/test/java/com/flow/platform/api/test/util/SystemUtilTest.java renamed to platform-util/src/test/java/com/flow/platform/util/test/SystemUtilTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.flow.platform.api.test.util;
17+
package com.flow.platform.util.test;
1818

19-
import com.flow.platform.api.util.SystemUtil;
19+
import com.flow.platform.util.SystemUtil;
2020
import java.nio.file.Path;
2121
import java.nio.file.Paths;
2222
import org.junit.Assert;
@@ -27,6 +27,18 @@
2727
*/
2828
public class SystemUtilTest {
2929

30+
@Test
31+
public void should_parse_linux_env() throws Throwable {
32+
String home = SystemUtil.parseEnv("${HOME}");
33+
Assert.assertEquals(System.getenv("HOME"), home);
34+
35+
home = SystemUtil.parseEnv("$HOME");
36+
Assert.assertEquals(System.getenv("HOME"), home);
37+
38+
home = SystemUtil.parseEnv("${HOME");
39+
Assert.assertNull(home);
40+
}
41+
3042
@Test
3143
public void should_replace_path_with_env() throws Throwable {
3244
// single placeholder

0 commit comments

Comments
 (0)