Skip to content

Commit

Permalink
Merge 9590c6a into 03cebfe
Browse files Browse the repository at this point in the history
  • Loading branch information
nexdrew committed Nov 1, 2019
2 parents 03cebfe + 9590c6a commit 646675a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 12 additions & 5 deletions index.js
Expand Up @@ -124,7 +124,7 @@ function toRunCommand (inspectObj, name) {
return policy.Name === 'on-failure' ? policy.Name + ':' + policy.MaximumRetryCount : policy.Name
})
}
if (isCompatible('--add-host', modes)) rc = appendArray(rc, '--add-host', hostcfg.ExtraHosts) // do not use in container net mode
if (isCompatible('--add-host', modes)) rc = appendArray(rc, '--add-host', hostcfg.ExtraHosts)

const cfg = inspectObj.Config || {}

Expand All @@ -134,7 +134,7 @@ function toRunCommand (inspectObj, name) {
if (cfg.ExposedPorts && isCompatible('--expose', modes)) {
rc = appendObjectKeys(rc, '--expose', cfg.ExposedPorts)
}
rc = appendArray(rc, '-e', cfg.Env, (env) => '\'' + env.replace(/'/g, '\'\\\'\'') + '\'')
rc = appendArray(rc, '-e', cfg.Env, quote)
rc = appendConfigBooleans(rc, cfg)
if (cfg.Entrypoint) rc = appendJoinedArray(rc, '--entrypoint', cfg.Entrypoint, ' ')

Expand Down Expand Up @@ -178,6 +178,10 @@ function isCompatible (flag, modes) {
}
}

function quote (str) {
return '\'' + str.replace(/'/g, '\'\\\'\'') + '\''
}

function appendConfigBooleans (str, cfg) {
const stdin = cfg.AttachStdin === true
const stdout = cfg.AttachStdout === true
Expand All @@ -197,9 +201,12 @@ function appendBoolean (str, bool, key, val) {

function appendJoinedArray (str, key, array, join) {
if (!Array.isArray(array)) return str
return append(str, key, array.join(join), (joined) => {
return key ? '"' + joined + '"' : joined
})

// --entrypoint "tini -- /docker-entrypoint.sh"
if (key) return append(str, key, array.join(join), joined => '"' + joined + '"')

// 'sh' '-c' '(a -a) && (b -b)'
return append(str, key, array.map(quote).join(join))
}

function appendObjectKeys (str, key, obj, transformer) {
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/inspect-one-two.json
Expand Up @@ -362,7 +362,9 @@
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/hello"
"sh",
"-c",
"(a -a) \u0026\u0026 (b -b)"
],
"Image": "hello-world",
"Volumes": null,
Expand Down
4 changes: 2 additions & 2 deletions test/test-cli.js
Expand Up @@ -41,7 +41,7 @@ const expectedOneTwo = '\n' +
'-e \'special_char_env_var3=abc()123\' ' +
'-d ' +
'--entrypoint "tini -- /docker-entrypoint.sh" ' +
'project_service /etc/npme/start.sh -g' +
'project_service \'/etc/npme/start.sh\' \'-g\'' +
'\n\n' +
'docker run ' +
'--name hello ' +
Expand All @@ -54,7 +54,7 @@ const expectedOneTwo = '\n' +
'-e \'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\' ' +
'-a stdout -a stderr ' +
'-t -i ' +
'hello-world /hello' +
'hello-world \'sh\' \'-c\' \'(a -a) && (b -b)\'' +
'\n\n'

test('cli works for docker inspect happy path', (t) => {
Expand Down

0 comments on commit 646675a

Please sign in to comment.