mirror of
https://github.com/docker/docker-credential-helpers.git
synced 2026-06-13 16:01:28 +05:30
4c9fc240ed
Don't set Env if not set; the default is already handled if it's nil; from the documentation: https://pkg.go.dev/os/exec@go1.20.4#Cmd.Env // If Env is nil, the new process uses the current process's // environment. Use `os/exec/Cmd.Environ()` instead of `os.Environ()`, which was added in go1.19, and handles additional environment variables, such as `PWD` on POSIX systems, and `SYSTEMROOT` on Windows. https://pkg.go.dev/os/exec@go1.20.4#Cmd.Environ Also remove a redundant `fmt.Sprintf()`, as we're only concatenating strings. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package client
|
|
|
|
import (
|
|
"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...)
|
|
if env != nil {
|
|
for k, v := range *env {
|
|
programCmd.Env = append(programCmd.Environ(), 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
|
|
}
|