-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contd: read environment variable from .stratup.toml if it exists and …
…ensure /etc/resolv.conf (#318) * contd: read environment variable from .stratup.toml if it exists * sort list before comparing * contd: parse startup.toml and update container data fixes #319 we extract 3 things from statup.toml: - entrypoint - working dir - enviroment variable
- Loading branch information
Showing
6 changed files
with
223 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package container | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type startup struct { | ||
Entries map[string]entry `toml:"startup"` | ||
} | ||
|
||
type entry struct { | ||
Name string | ||
Args args | ||
} | ||
|
||
type args struct { | ||
Name string | ||
Dir string | ||
Env map[string]string | ||
} | ||
|
||
func (e entry) Entrypoint() string { | ||
if e.Name == "core.system" || | ||
e.Name == "core.base" && e.Args.Name != "" { | ||
return e.Args.Name | ||
} | ||
return "" | ||
} | ||
|
||
func (e entry) WorkingDir() string { | ||
return e.Args.Dir | ||
} | ||
|
||
func (e entry) Envs() []string { | ||
envs := make([]string, 0, len(e.Args.Env)) | ||
for k, v := range e.Args.Env { | ||
envs = append(envs, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
return envs | ||
} | ||
|
||
// mergeEnvs merge a into b | ||
// all the key from a will endup in b | ||
// if a key is present in both, key from a are kept | ||
func mergeEnvs(a, b []string) []string { | ||
m := make(map[string]string, len(a)+len(b)) | ||
|
||
for _, s := range b { | ||
ss := strings.SplitN(s, "=", 2) | ||
m[ss[0]] = ss[1] | ||
} | ||
for _, s := range a { | ||
ss := strings.SplitN(s, "=", 2) | ||
m[ss[0]] = ss[1] | ||
} | ||
|
||
result := make([]string, 0, len(m)) | ||
for k, v := range m { | ||
result = append(result, fmt.Sprintf("%s=%s", k, v)) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package container | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var _startup = `[startup] | ||
[startup.entry] | ||
name = "core.system" | ||
[startup.entry.args] | ||
name = "/start" | ||
dir = "/data" | ||
[startup.entry.args.env] | ||
DIFFICULTY = "easy" | ||
LEVEL = "world" | ||
SERVER_PORT = "25565" | ||
` | ||
|
||
func TestParseStartup(t *testing.T) { | ||
r := strings.NewReader(_startup) | ||
e := startup{} | ||
_, err := toml.DecodeReader(r, &e) | ||
require.NoError(t, err) | ||
|
||
entry, ok := e.Entries["entry"] | ||
require.True(t, ok) | ||
assert.Equal(t, "core.system", entry.Name) | ||
assert.Equal(t, "/start", entry.Args.Name) | ||
assert.Equal(t, "/data", entry.Args.Dir) | ||
assert.Equal(t, map[string]string{ | ||
"DIFFICULTY": "easy", | ||
"LEVEL": "world", | ||
"SERVER_PORT": "25565", | ||
}, entry.Args.Env) | ||
} | ||
|
||
func TestStartupEntrypoint(t *testing.T) { | ||
r := strings.NewReader(_startup) | ||
e := startup{} | ||
_, err := toml.DecodeReader(r, &e) | ||
require.NoError(t, err) | ||
|
||
entry, ok := e.Entries["entry"] | ||
require.True(t, ok) | ||
assert.Equal(t, entry.Entrypoint(), "/start") | ||
} | ||
|
||
func TestStartupEnvs(t *testing.T) { | ||
r := strings.NewReader(_startup) | ||
e := startup{} | ||
_, err := toml.DecodeReader(r, &e) | ||
require.NoError(t, err) | ||
|
||
entry, ok := e.Entries["entry"] | ||
require.True(t, ok) | ||
actual := entry.Envs() | ||
sort.Strings(actual) | ||
expected := []string{ | ||
"DIFFICULTY=easy", | ||
"LEVEL=world", | ||
"SERVER_PORT=25565", | ||
} | ||
assert.Equal(t, expected, actual) | ||
} | ||
|
||
func TestStartupWorkingDir(t *testing.T) { | ||
r := strings.NewReader(_startup) | ||
e := startup{} | ||
_, err := toml.DecodeReader(r, &e) | ||
require.NoError(t, err) | ||
|
||
entry, ok := e.Entries["entry"] | ||
require.True(t, ok) | ||
assert.Equal(t, entry.WorkingDir(), "/data") | ||
} | ||
|
||
func TestMergeEnvs(t *testing.T) { | ||
actual := mergeEnvs( | ||
[]string{"FOO=BAR", "HELLO=WORLD"}, | ||
[]string{"HELLO=HELLO"}, | ||
) | ||
|
||
expected := []string{"FOO=BAR", "HELLO=WORLD"} | ||
sort.Strings(actual) | ||
assert.Equal(t, expected, actual) | ||
} |