func()

in command-runner/internal/containers/finch/finch_run.go [205:257]


func (cr *finchContainer) create(capAdd []string, capDrop []string) common.Executor {
	return func(ctx context.Context) error {
		if cr.id != "" {
			return nil
		}
		input := cr.input

		flags := []string{"--tty", "--workdir", input.WorkingDir, "--name", input.Name}
		if input.Privileged {
			flags = append(flags, "--privileged")
		}
		for src, dst := range input.Mounts {
			flags = append(flags, "--mount", fmt.Sprintf("type=volume,src=%s,dst=%s", src, dst))
		}
		for _, bind := range input.Binds {
			flags = append(flags, "--volume", bind)
		}
		if len(capAdd) != 0 {
			flags = append(flags, "--cap-add")
			flags = append(flags, capAdd...)
		}
		if len(capDrop) != 0 {
			flags = append(flags, "--cap-drop")
			flags = append(flags, capDrop...)
		}
		for _, e := range input.Env {
			flags = append(flags, "--env", e)
		}
		if len(input.Entrypoint) != 0 {
			flags = append(flags, "--entrypoint")
			flags = append(flags, input.Entrypoint...)
		}

		args := []string{"create"}
		args = append(args, flags...)
		args = append(args, input.Image)
		if len(input.Cmd) != 0 {
			args = append(args, input.Cmd...)
		}
		rout, rerr, err := cr.f.RunWithoutStdio(ctx, args...)
		if err != nil {
			return fmt.Errorf("failed to create container: '%w'\n%s\n%s", err, rout, rerr)
		}

		id := strings.TrimRight(string(rout), "\r\n")

		log.Ctx(ctx).Printf("Created container name=%s id=%v from image %v (platform: %s)", input.Name, id, input.Image, input.Platform)
		log.Ctx(ctx).Printf("ENV ==> %v", input.Env)

		cr.id = id
		return nil
	}
}