cmdline quoting

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 76ab2a9c82
commit 46aed94077
2 changed files with 17 additions and 4 deletions

View File

@ -73,9 +73,10 @@ func LXCConfig(rd io.ReadSeeker, wr io.Writer) error {
} }
func docker2lxc(d dockerConfig) lxcConfig { func docker2lxc(d dockerConfig) lxcConfig {
// cmd/entrypoint logic is copied from lxc-oci template // cmd/entrypoint logic is copied from lxc-oci template and adopted
ep := strings.Join(d.Config.Entrypoint, " ") // for simple double-argument quoting.
cmd := strings.Join(d.Config.Cmd, " ") ep := quoted(d.Config.Entrypoint)
cmd := quoted(d.Config.Cmd)
if len(ep) == 0 { if len(ep) == 0 {
ep = cmd ep = cmd
if len(ep) == 0 { if len(ep) == 0 {
@ -154,3 +155,15 @@ func parseJSON(rd io.ReadSeeker, offsets map[string]offsetSize, fname string, c
} }
return nil return nil
} }
func quoted(cmds []string) string {
ret := make([]string, len(cmds))
for i, cmd := range cmds {
if cmd == "" || strings.ContainsRune(cmd, ' ') {
ret[i] = `"` + cmd + `"`
} else {
ret[i] = cmd
}
}
return strings.Join(ret, " ")
}

View File

@ -48,7 +48,7 @@ func TestLXCConfig(t *testing.T) {
want: strings.Join([]string{ want: strings.Join([]string{
`lxc.include = LXC_TEMPLATE_CONFIG/common.conf`, `lxc.include = LXC_TEMPLATE_CONFIG/common.conf`,
`lxc.architecture = amd64`, `lxc.architecture = amd64`,
`lxc.execute.cmd = '/entrypoint.sh /bin/sh -c echo foo'`, `lxc.execute.cmd = '/entrypoint.sh /bin/sh -c "echo foo"'`,
`lxc.init.cwd = /x`, `lxc.init.cwd = /x`,
`lxc.environment = LONGNAME="Foo Bar"`, `lxc.environment = LONGNAME="Foo Bar"`,
`lxc.environment = SHELL=/bin/tcsh`, `lxc.environment = SHELL=/bin/tcsh`,