Skip to content

Commit 5085f20

Browse files
authored
Add support for creating directories (#33)
1 parent 49523bb commit 5085f20

File tree

7 files changed

+93
-1
lines changed

7 files changed

+93
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ jvmOpts:
3131
# OPTIONAL - Arguments passed to the main method of the main class
3232
args:
3333
- arg1
34+
# OPTIONAL - A list of directories to be created before executing the command. Must be relative to CWD and over [A-Za-z0-9].
35+
args:
36+
- var/data/tmp
37+
- var/log
3438
```
3539
3640
```yaml
@@ -47,6 +51,10 @@ executable: "{{CWD}}/service/bin/postgres"
4751
# OPTIONAL - Arguments passed to the main method of the excutable or main class
4852
args:
4953
- arg1
54+
# OPTIONAL - A list of directories to be created before executing the command. Must be relative to CWD and over [A-Za-z0-9].
55+
args:
56+
- var/data/tmp
57+
- var/log
5058
```
5159
5260
```yaml

integration_test/integration_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package integration_test
1616

1717
import (
18+
"os"
1819
"os/exec"
1920
"testing"
2021

@@ -49,6 +50,21 @@ func TestMainMethodWithoutCustomConfig(t *testing.T) {
4950
assert.Regexp(t, `\nmain method\n`, string(output))
5051
}
5152

53+
func TestCreatesDirs(t *testing.T) {
54+
output, err := runMainWithArgs(t, "test_resources/launcher-static-with-dirs.yml", "foo")
55+
require.NoError(t, err, "failed: %s", output)
56+
57+
dir, err := os.Stat("foo")
58+
assert.NoError(t, err)
59+
assert.True(t, dir.IsDir())
60+
require.NoError(t, os.RemoveAll("foo"))
61+
62+
dir, err = os.Stat("bar/baz")
63+
assert.NoError(t, err)
64+
assert.True(t, dir.IsDir())
65+
require.NoError(t, os.RemoveAll("bar"))
66+
}
67+
5268
func runMainWithArgs(t *testing.T, staticConfigFile, customConfigFile string) (string, error) {
5369
cli, err := products.Bin("go-java-launcher")
5470
require.NoError(t, err)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
configType: java
2+
configVersion: 1
3+
mainClass: Main
4+
classpath:
5+
- ./test_resources/
6+
jvmOpts:
7+
- '-Xmx4M'
8+
args:
9+
- arg1
10+
dirs:
11+
- foo
12+
- bar/baz

launcher/main/main.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,32 @@ func main() {
4141
customConfigFile = os.Args[2]
4242
}
4343

44-
cmd, err := launchlib.CompileCmdFromConfigFiles(staticConfigFile, customConfigFile)
44+
// Read configuration
45+
staticConfig, err := launchlib.GetStaticConfigFromFile(staticConfigFile)
46+
if err != nil {
47+
fmt.Println("Failed to read static config file", err)
48+
panic(err)
49+
}
50+
customConfig, err := launchlib.GetCustomConfigFromFile(customConfigFile)
51+
if err != nil {
52+
fmt.Println("Failed to read custom config file", err)
53+
panic(err)
54+
}
55+
56+
// Create configured directories
57+
if err := launchlib.MkDirs(staticConfig.Dirs); err != nil {
58+
fmt.Println("Failed to create directories", err)
59+
panic(err)
60+
}
61+
62+
// Compile command
63+
cmd, err := launchlib.CompileCmdFromConfig(&staticConfig, &customConfig)
4564
if err != nil {
4665
fmt.Println("Failed to assemble executable metadata", cmd, err)
4766
panic(err)
4867
}
4968

69+
// Execute command
5070
execErr := syscall.Exec(cmd.Path, cmd.Args, cmd.Env)
5171
if execErr != nil {
5272
if os.IsNotExist(execErr) {

launchlib/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type StaticLauncherConfig struct {
4141
Env map[string]string `yaml:"env"`
4242
Executable string `yaml:"executable,omitempty"`
4343
Args []string `yaml:"args"`
44+
Dirs []string `yaml:"dirs"`
4445
}
4546

4647
type CustomLauncherConfig struct {

launchlib/launcher.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ func CompileCmdFromConfig(staticConfig *StaticLauncherConfig, customConfig *Cust
9191
return createCmd(executable, args, env)
9292
}
9393

94+
func MkDirs(dirs []string) error {
95+
isDirMatcher := regexp.MustCompile(`^[A-Za-z0-9]+(/[A-Za-z0-9]+)*$`).MatchString
96+
for _, dir := range dirs {
97+
if !isDirMatcher(dir) {
98+
return fmt.Errorf("Cannot create directory with non [A-Za-z0-9] characters: %s", dir)
99+
}
100+
101+
fmt.Printf("Creating directory: %s\n", dir)
102+
if err := os.MkdirAll(dir, 0700); err != nil {
103+
return err
104+
}
105+
}
106+
return nil
107+
}
108+
94109
// Returns true iff the given path is safe to be passed to exec(): must not contain funky characters and be a valid file.
95110
func verifyPathIsSafeForExec(execPath string) (string, error) {
96111
if unsafe, err := regexp.MatchString(ExecPathBlackListRegex, execPath); err != nil {

launchlib/launcher_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,23 @@ func TestKeysAreNotExpanded(t *testing.T) {
112112
t.Errorf("Expected %%CWD%% to exist in map and not be expanded, but it didn't")
113113
}
114114
}
115+
116+
func TestMkdirChecksDirectorySyntax(t *testing.T) {
117+
err := MkDirs([]string{"abc/def1"})
118+
assert.NoError(t, err)
119+
120+
err = MkDirs([]string{"abc"})
121+
assert.NoError(t, err)
122+
123+
require.NoError(t, os.RemoveAll("abc"))
124+
125+
badCases := []string{
126+
"^&*",
127+
"abc//def",
128+
"abc/../def",
129+
}
130+
for _, dir := range badCases {
131+
err = MkDirs([]string{dir})
132+
assert.EqualError(t, err, "Cannot create directory with non [A-Za-z0-9] characters: "+dir)
133+
}
134+
}

0 commit comments

Comments
 (0)