Repository: threefoldtech/zos
Pull request #603 (2020-03-22 13:25:02)

Support correct .startup.toml format
the real .startup.toml format supports args
which must be appeneded in a way that works with
shlex parsing


codecov[bot]: # [Codecov](https://codecov.io/gh/threefoldtech/zos/pull/603?src=pr&el=h1) Report
> Merging [#603](https://codecov.io/gh/threefoldtech/zos/pull/603?src=pr&el=desc) into [master](https://codecov.io/gh/threefoldtech/zos/commit/205e8bf586b372ca8bd529c1be872045c665569e&el=desc) will **increase** coverage by `0.09%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/threefoldtech/zos/pull/603/graphs/tree.svg?width=650&height=150&src=pr&token=f7Vmq1OAEm)](https://codecov.io/gh/threefoldtech/zos/pull/603?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #603      +/-   ##
==========================================
+ Coverage   35.90%   36.00%   +0.09%     
==========================================
  Files          71       71              
  Lines        5289     5297       +8     
==========================================
+ Hits         1899     1907       +8     
  Misses       3118     3118              
  Partials      272      272              
```


| [Impacted Files](https://codecov.io/gh/threefoldtech/zos/pull/603?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/container/startup.go](https://codecov.io/gh/threefoldtech/zos/pull/603/diff?src=pr&el=tree#diff-cGtnL2NvbnRhaW5lci9zdGFydHVwLmdv) | `96.87% <100.00%> (+1.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/threefoldtech/zos/pull/603?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/threefoldtech/zos/pull/603?src=pr&el=footer). Last update [205e8bf...5487fef](https://codecov.io/gh/threefoldtech/zos/pull/603?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/container/startup.go b/pkg/container/startup.go
index fa85f45b..ddff1b96 100644
--- a/pkg/container/startup.go
+++ b/pkg/container/startup.go
@@ -17,14 +17,26 @@ type entry struct {
 type args struct {
 	Name string
 	Dir  string
+	Args []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
+		var buf strings.Builder
+		buf.WriteString(e.Args.Name)
+		for _, arg := range e.Args.Args {
+			buf.WriteRune(' ')
+			arg = strings.Replace(arg, "\"", "\\\"", -1)
+			buf.WriteRune('"')
+			buf.WriteString(arg)
+			buf.WriteRune('"')
+		}
+
+		return buf.String()
 	}
+
 	return ""
 }
 


Test patch:
diff --git a/pkg/container/startup_test.go b/pkg/container/startup_test.go
index 3313c358..6ebc6768 100644
--- a/pkg/container/startup_test.go
+++ b/pkg/container/startup_test.go
@@ -5,6 +5,7 @@ import (
 	"strings"
 	"testing"
 
+	"github.com/google/shlex"
 	"github.com/stretchr/testify/assert"
 
 	"github.com/BurntSushi/toml"
@@ -19,6 +20,11 @@ name = "core.system"
 [startup.entry.args]
 name = "/start"
 dir = "/data"
+args = [
+	"extra",
+	"argument with spaces",
+	"with \"skip\"",
+]
 
 [startup.entry.args.env]
 DIFFICULTY = "easy"
@@ -37,6 +43,8 @@ func TestParseStartup(t *testing.T) {
 	assert.Equal(t, "core.system", entry.Name)
 	assert.Equal(t, "/start", entry.Args.Name)
 	assert.Equal(t, "/data", entry.Args.Dir)
+	assert.Equal(t, []string{"extra", "argument with spaces", "with \"skip\""}, entry.Args.Args)
+
 	assert.Equal(t, map[string]string{
 		"DIFFICULTY":  "easy",
 		"LEVEL":       "world",
@@ -52,7 +60,12 @@ func TestStartupEntrypoint(t *testing.T) {
 
 	entry, ok := e.Entries["entry"]
 	require.True(t, ok)
-	assert.Equal(t, entry.Entrypoint(), "/start")
+	assert.Equal(t, entry.Entrypoint(), `/start "extra" "argument with spaces" "with \"skip\""`)
+
+	parts, err := shlex.Split(entry.Entrypoint())
+	require.NoError(t, err)
+
+	assert.Equal(t, []string{"/start", "extra", "argument with spaces", "with \"skip\""}, parts)
 }
 
 func TestStartupEnvs(t *testing.T) {