{"size": 2666, "ext": "js", "max_stars_repo_path": "src/utils/docker/buildDockerCmd.js", "max_stars_repo_name": "keg-hub/keg-cli", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/utils/docker/buildDockerCmd.js", "max_issues_repo_name": "keg-hub/keg-cli", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/utils/docker/buildDockerCmd.js", "max_forks_repo_name": "keg-hub/keg-cli", "max_forks_repo_forks_event_min_datetime": "2021-12-02T02:09:11.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-02T02:09:11.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 28.9782608696, "max_line_length": 81, "alphanum_fraction": 0.6875468867}
const { buildTags } = require('./tags/buildTags')
const { getBuildArgs } = require('./getBuildArgs')
const { getBuildLabels } = require('./getBuildLabels')
const { addBuildPlatform } = require('./addBuildPlatform')
const { getDockerCmdArgs } = require('./getDockerCmdArgs')

/**
 * Creates a docker run command as a string. Adds any needed volume mounts
 * @param {Object} args - Arguments passed to the task
 * @param {Object} args.globalConfig - Global config object for the keg-cli
 * @param {Object} args.params - Data to build the docker command
 * @param {string} args.params.dockerCmd - docker command being built
 * @param {string} args.params.context - Context of the image to be built
 * @param {Array} args.params.tags - Tag names for the image being built
 *
 * @returns {string} - Built docker build command
 */
const createBuildCmd = async (args, dockerCmd) => {
  const { globalConfig, params } = args
  const { location, buildX } = params

  // Add the build platform for the image
  dockerCmd = buildX
    ? addBuildPlatform(dockerCmd, params)
    : dockerCmd

  // Add any build tags
  dockerCmd = await buildTags(args, params, dockerCmd)

  // Add any build labels
  dockerCmd = getBuildLabels(args, dockerCmd)

  // Add the build args for the github key and tap git url
  dockerCmd = await getBuildArgs(globalConfig, params, dockerCmd)

  // Add the location last. This is the location the container will be built from
  return location ? `${dockerCmd} ${location}` : dockerCmd
}

/**
 * Builds a docker command to be passed to the command line
 * @param {Object} args - Arguments passed to the task
 * @param {Object} args.globalConfig - Global config object for the keg-cli
 * @param {Object} params - Data to build the docker command
 * @param {Object} params.cmd - The docker command to run
 * @param {Object} params.context - Name of the docker container to run
 * @param {Object} params.env - Environment to run the container in
 * @param {Object} params.envs - Environment Vars to pass to the docker command
 *
 * @returns {string} - Built docker command
 */
const buildDockerCmd = args => {
  const { params={} } = args
  const {
    buildArgs,
    cmd,
    docker='',
    env,
    envs,
    execCmd,
    from,
    tagGit,
    image,
    location,
    mounts,
    context,
    platform,
    tagPackage,
    pull,
    tags,
    tap,
    tagVariable,
    version,
    ...dockerOpts
  } = params

  // Get the default docker arguments
  const dockerCmd = getDockerCmdArgs({
    cmd,
    context,
    args: dockerOpts,
    dockerCmd: `${docker}`.trim()
  })

  return createBuildCmd(args, dockerCmd)

}

module.exports = {
  buildDockerCmd
}
