Fix bug with spaces in docker run command

This commit is contained in:
Joseph Garrone 2024-08-18 20:56:25 +02:00
parent 225ced989c
commit 602de2e407

View File

@ -334,23 +334,25 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
}); });
} catch {} } catch {}
const SPACE_PLACEHOLDER = "SPACE_PLACEHOLDER_xKLmdPd";
const dockerRunArgs: string[] = [ const dockerRunArgs: string[] = [
`-p ${cliCommandOptions.port}:8080`, `-p${SPACE_PLACEHOLDER}${cliCommandOptions.port}:8080`,
`--name ${CONTAINER_NAME}`, `--name${SPACE_PLACEHOLDER}${CONTAINER_NAME}`,
`-e KEYCLOAK_ADMIN=admin`, `-e${SPACE_PLACEHOLDER}KEYCLOAK_ADMIN=admin`,
`-e KEYCLOAK_ADMIN_PASSWORD=admin`, `-e${SPACE_PLACEHOLDER}KEYCLOAK_ADMIN_PASSWORD=admin`,
...(realmJsonFilePath === undefined ...(realmJsonFilePath === undefined
? [] ? []
: [ : [
`-v ".${pathSep}${pathRelative(process.cwd(), realmJsonFilePath)}":/opt/keycloak/data/import/myrealm-realm.json` `-v${SPACE_PLACEHOLDER}".${pathSep}${pathRelative(process.cwd(), realmJsonFilePath)}":/opt/keycloak/data/import/myrealm-realm.json`
]), ]),
`-v "./${pathRelative(process.cwd(), jarFilePath_cacheDir)}":/opt/keycloak/providers/keycloak-theme.jar`, `-v${SPACE_PLACEHOLDER}"./${pathRelative(process.cwd(), jarFilePath_cacheDir)}":/opt/keycloak/providers/keycloak-theme.jar`,
...extensionJarFilePaths.map( ...extensionJarFilePaths.map(
jarFilePath => jarFilePath =>
`-v ".${pathSep}${pathRelative(process.cwd(), jarFilePath)}":/opt/keycloak/providers/${pathBasename(jarFilePath)}` `-v${SPACE_PLACEHOLDER}".${pathSep}${pathRelative(process.cwd(), jarFilePath)}":/opt/keycloak/providers/${pathBasename(jarFilePath)}`
), ),
...(keycloakMajorVersionNumber <= 20 ...(keycloakMajorVersionNumber <= 20
? ["-e JAVA_OPTS=-Dkeycloak.profile=preview"] ? [`-e${SPACE_PLACEHOLDER}JAVA_OPTS=-Dkeycloak.profile=preview`]
: []), : []),
...[ ...[
...buildContext.themeNames, ...buildContext.themeNames,
@ -374,7 +376,7 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
})) }))
.map( .map(
({ localDirPath, containerDirPath }) => ({ localDirPath, containerDirPath }) =>
`-v ".${pathSep}${pathRelative(process.cwd(), localDirPath)}":${containerDirPath}:rw` `-v${SPACE_PLACEHOLDER}".${pathSep}${pathRelative(process.cwd(), localDirPath)}":${containerDirPath}:rw`
), ),
...buildContext.environmentVariables ...buildContext.environmentVariables
.map(({ name }) => ({ name, envValue: process.env[name] })) .map(({ name }) => ({ name, envValue: process.env[name] }))
@ -384,32 +386,35 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
.filter(exclude(undefined)) .filter(exclude(undefined))
.map( .map(
({ name, envValue }) => ({ name, envValue }) =>
`--env ${name}='${envValue.replace(/'/g, "'\\''")}'` `--env${SPACE_PLACEHOLDER}${name}='${envValue.replace(/'/g, "'\\''")}'`
), ),
...buildContext.startKeycloakOptions.dockerExtraArgs, ...buildContext.startKeycloakOptions.dockerExtraArgs.join(SPACE_PLACEHOLDER),
`${buildContext.startKeycloakOptions.dockerImage?.reference ?? "quay.io/keycloak/keycloak"}:${dockerImageTag}`, `${buildContext.startKeycloakOptions.dockerImage?.reference ?? "quay.io/keycloak/keycloak"}:${dockerImageTag}`,
"start-dev", "start-dev",
...(21 <= keycloakMajorVersionNumber && keycloakMajorVersionNumber < 24 ...(21 <= keycloakMajorVersionNumber && keycloakMajorVersionNumber < 24
? ["--features=declarative-user-profile"] ? ["--features=declarative-user-profile"]
: []), : []),
...(realmJsonFilePath === undefined ? [] : ["--import-realm"]), ...(realmJsonFilePath === undefined ? [] : ["--import-realm"]),
...buildContext.startKeycloakOptions.keycloakExtraArgs ...buildContext.startKeycloakOptions.keycloakExtraArgs.join(SPACE_PLACEHOLDER)
]; ];
console.log( console.log(
chalk.blue( chalk.blue(
[ [
`$ docker run \\`, `$ docker run \\`,
...dockerRunArgs.map( ...dockerRunArgs
(line, i, arr) => ` ${line}${arr.length - 1 === i ? "" : " \\"}` .map(arg => arg.replace(new RegExp(SPACE_PLACEHOLDER, "g"), " "))
) .map(
(line, i, arr) =>
` ${line}${arr.length - 1 === i ? "" : " \\"}`
)
].join("\n") ].join("\n")
) )
); );
const child = child_process.spawn( const child = child_process.spawn(
"docker", "docker",
["run", ...dockerRunArgs.map(line => line.split(" ")).flat()], ["run", ...dockerRunArgs.map(line => line.split(SPACE_PLACEHOLDER)).flat()],
{ shell: true } { shell: true }
); );