summaryrefslogtreecommitdiff
path: root/vendor/github.com/docker/docker-credential-helpers/client/command.go
blob: 8da3343065f68dff6a933cfaaa1130e2c7ca340d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package client

import (
	"fmt"
	"io"
	"os"
	"os/exec"
)

// Program is an interface to execute external programs.
type Program interface {
	Output() ([]byte, error)
	Input(in io.Reader)
}

// ProgramFunc is a type of function that initializes programs based on arguments.
type ProgramFunc func(args ...string) Program

// NewShellProgramFunc creates programs that are executed in a Shell.
func NewShellProgramFunc(name string) ProgramFunc {
	return NewShellProgramFuncWithEnv(name, nil)
}

// NewShellProgramFuncWithEnv creates programs that are executed in a Shell with environment variables
func NewShellProgramFuncWithEnv(name string, env *map[string]string) ProgramFunc {
	return func(args ...string) Program {
		return &Shell{cmd: createProgramCmdRedirectErr(name, args, env)}
	}
}

func createProgramCmdRedirectErr(commandName string, args []string, env *map[string]string) *exec.Cmd {
	programCmd := exec.Command(commandName, args...)
	programCmd.Env = os.Environ()
	if env != nil {
		for k, v := range *env {
			programCmd.Env = append(programCmd.Env, fmt.Sprintf("%s=%s", k, v))
		}
	}
	programCmd.Stderr = os.Stderr
	return programCmd
}

// Shell invokes shell commands to talk with a remote credentials helper.
type Shell struct {
	cmd *exec.Cmd
}

// Output returns responses from the remote credentials helper.
func (s *Shell) Output() ([]byte, error) {
	return s.cmd.Output()
}

// Input sets the input to send to a remote credentials helper.
func (s *Shell) Input(in io.Reader) {
	s.cmd.Stdin = in
}