diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2018-02-07 14:07:54 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-07 14:07:54 -0500 |
commit | 588c9bb5de08f0c3058a18b401ac87e38cd011be (patch) | |
tree | e633be56c5ee9bdbcbe3b14c562e10f34b5cdf63 /pkg/util | |
parent | 6716581545f7936ab585b125e2cfc02d2d802f9f (diff) | |
parent | bb37c11651b9a01ff9b5191eb5072cdf0db83a51 (diff) | |
download | podman-588c9bb5de08f0c3058a18b401ac87e38cd011be.tar.gz podman-588c9bb5de08f0c3058a18b401ac87e38cd011be.tar.bz2 podman-588c9bb5de08f0c3058a18b401ac87e38cd011be.zip |
Merge pull request #289 from TomSweeneyRedHat/dev/tsweeney/username
Change un/pwd handling to match Buildah's
Diffstat (limited to 'pkg/util')
-rw-r--r-- | pkg/util/utils.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/pkg/util/utils.go b/pkg/util/utils.go new file mode 100644 index 000000000..9a93021e4 --- /dev/null +++ b/pkg/util/utils.go @@ -0,0 +1,46 @@ +package util + +import ( + "fmt" + "strings" + + "github.com/containers/image/types" + "github.com/pkg/errors" + "golang.org/x/crypto/ssh/terminal" +) + +// Helper function to determine the username/password passed +// in the creds string. It could be either or both. +func parseCreds(creds string) (string, string) { + if creds == "" { + return "", "" + } + up := strings.SplitN(creds, ":", 2) + if len(up) == 1 { + return up[0], "" + } + return up[0], up[1] +} + +// ParseRegistryCreds takes a credentials string in the form USERNAME:PASSWORD +// and returns a DockerAuthConfig +func ParseRegistryCreds(creds string) (*types.DockerAuthConfig, error) { + username, password := parseCreds(creds) + if username == "" { + fmt.Print("Username: ") + fmt.Scanln(&username) + } + if password == "" { + fmt.Print("Password: ") + termPassword, err := terminal.ReadPassword(0) + if err != nil { + return nil, errors.Wrapf(err, "could not read password from terminal") + } + password = string(termPassword) + } + + return &types.DockerAuthConfig{ + Username: username, + Password: password, + }, nil +} |