...

Source file src/github.com/docker/docker-credential-helpers/client/command.go

Documentation: github.com/docker/docker-credential-helpers/client

     1  package client
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"os/exec"
     7  )
     8  
     9  // Program is an interface to execute external programs.
    10  type Program interface {
    11  	Output() ([]byte, error)
    12  	Input(in io.Reader)
    13  }
    14  
    15  // ProgramFunc is a type of function that initializes programs based on arguments.
    16  type ProgramFunc func(args ...string) Program
    17  
    18  // NewShellProgramFunc creates programs that are executed in a Shell.
    19  func NewShellProgramFunc(name string) ProgramFunc {
    20  	return NewShellProgramFuncWithEnv(name, nil)
    21  }
    22  
    23  // NewShellProgramFuncWithEnv creates programs that are executed in a Shell with environment variables
    24  func NewShellProgramFuncWithEnv(name string, env *map[string]string) ProgramFunc {
    25  	return func(args ...string) Program {
    26  		return &Shell{cmd: createProgramCmdRedirectErr(name, args, env)}
    27  	}
    28  }
    29  
    30  func createProgramCmdRedirectErr(commandName string, args []string, env *map[string]string) *exec.Cmd {
    31  	programCmd := exec.Command(commandName, args...)
    32  	if env != nil {
    33  		for k, v := range *env {
    34  			programCmd.Env = append(programCmd.Environ(), k+"="+v)
    35  		}
    36  	}
    37  	programCmd.Stderr = os.Stderr
    38  	return programCmd
    39  }
    40  
    41  // Shell invokes shell commands to talk with a remote credentials-helper.
    42  type Shell struct {
    43  	cmd *exec.Cmd
    44  }
    45  
    46  // Output returns responses from the remote credentials-helper.
    47  func (s *Shell) Output() ([]byte, error) {
    48  	return s.cmd.Output()
    49  }
    50  
    51  // Input sets the input to send to a remote credentials-helper.
    52  func (s *Shell) Input(in io.Reader) {
    53  	s.cmd.Stdin = in
    54  }
    55  

View as plain text