Repository: moby/moby
File: daemon/builder/dockerfile/internals.go (Go)

Code before:
	}
}

func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
	cfgCopy := *runConfig
	cfgCopy.Cmd = copyStringSlice(runConfig.Cmd)
	cfgCopy.Env = copyStringSlice(runConfig.Env)
	cfgCopy.Entrypoint = copyStringSlice(runConfig.Entrypoint)
	cfgCopy.OnBuild = copyStringSlice(runConfig.OnBuild)
	cfgCopy.Shell = copyStringSlice(runConfig.Shell)

	if cfgCopy.Volumes != nil {
		cfgCopy.Volumes = make(map[string]struct{}, len(runConfig.Volumes))
		for k, v := range runConfig.Volumes {
			cfgCopy.Volumes[k] = v
		}
	}

	if cfgCopy.ExposedPorts != nil {
		cfgCopy.ExposedPorts = make(container.PortSet, len(runConfig.ExposedPorts))
		for k, v := range runConfig.ExposedPorts {
			cfgCopy.ExposedPorts[k] = v
		}
	}

	if cfgCopy.Labels != nil {
		cfgCopy.Labels = make(map[string]string, len(runConfig.Labels))
		for k, v := range runConfig.Labels {
			cfgCopy.Labels[k] = v
		}
	}

	for _, modifier := range modifiers {
		modifier(&cfgCopy)
	}
	return &cfgCopy
}

func copyStringSlice(orig []string) []string {
	if orig == nil {
		return nil
	}
	return append([]string{}, orig...)
}

// getShell is a helper function which gets the right shell for prefixing the
// shell-form of RUN, ENTRYPOINT and CMD instructions
func getShell(c *container.Config, os string) []string {
	if len(c.Shell) == 0 {
		return append([]string{}, defaultShellForOS(os)[:]...)
	}

Review comment (suggestion):
For those wondering; `nil` check shouldn't be needed because both `slices.Clone` and `maps.Clone` check for `nil` and returns it as-is.

Code after:
			Test: []string{"NONE"},
		}
	}
}

func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
	cfgCopy := *runConfig
	cfgCopy.Cmd = slices.Clone(runConfig.Cmd)
	cfgCopy.Env = slices.Clone(runConfig.Env)
	cfgCopy.Entrypoint = slices.Clone(runConfig.Entrypoint)
	cfgCopy.OnBuild = slices.Clone(runConfig.OnBuild)
	cfgCopy.Shell = slices.Clone(runConfig.Shell)
	cfgCopy.Volumes = maps.Clone(runConfig.Volumes)
	cfgCopy.ExposedPorts = maps.Clone(runConfig.ExposedPorts)
	cfgCopy.Labels = maps.Clone(runConfig.Labels)

	for _, modifier := range modifiers {
		modifier(&cfgCopy)
	}
	return &cfgCopy
}

// getShell is a helper function which gets the right shell for prefixing the
// shell-form of RUN, ENTRYPOINT and CMD instructions
func getShell(c *container.Config, os string) []string {
	if len(c.Shell) == 0 {
		return append([]string{}, defaultShellForOS(os)[:]...)
	}
	return append([]string{}, c.Shell[:]...)
}

func (b *Builder) probeCache(dispatchState *dispatchState, runConfig *container.Config) (bool, error) {
	cachedID, err := b.imageProber.Probe(dispatchState.imageID, runConfig, b.getPlatform(dispatchState))
	if cachedID == "" || err != nil {
		return false, err
	}
	_, _ = fmt.Fprintln(b.Stdout, " ---> Using cache")

	dispatchState.imageID = cachedID
	return true, nil
}

var defaultLogConfig = container.LogConfig{Type: "none"}

func (b *Builder) probeAndCreate(ctx context.Context, dispatchState *dispatchState, runConfig *container.Config) (string, error) {
	if hit, err := b.probeCache(dispatchState, runConfig); err != nil || hit {
		return "", err
	}
	return b.create(ctx, runConfig)
}