Last active
March 5, 2021 08:45
-
-
Save developer-guy/a448728dff9ad9b7946fd83660f25e4d to your computer and use it in GitHub Desktop.
Login to the Dockerhub using golang exec and pty, also, Fix for “cannot perform an interactive login from a non TTY device”
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "io" | |
| "io/ioutil" | |
| "log" | |
| "os" | |
| "os/exec" | |
| "github.com/creack/pty" | |
| ) | |
| func main() { | |
| executablePath, err := exec.LookPath("docker") | |
| if err != nil { | |
| log.Fatalf("could not found docker, err %v", err) | |
| } | |
| cmd := exec.Command(executablePath, "login", "docker.io") | |
| ptmx, err := pty.Start(cmd) | |
| if err != nil { | |
| panic(err) | |
| } | |
| // Make sure to close the pty at the end. | |
| defer func() { _ = ptmx.Close() }() // Best effort. | |
| id := os.Getenv("DOCKER_USERNAME") | |
| password := os.Getenv("DOCKER_USERNAME") | |
| go func() { | |
| ptmx.Write([]byte(id)) | |
| ptmx.Write([]byte(password)) | |
| }() | |
| var execOut bytes.Buffer | |
| io.Copy(&execOut, ptmx) | |
| count := len(id) + len(password) | |
| io.CopyN(ioutil.Discard, &execOut, int64(count)) | |
| log.Println(execOut.String()) | |
| } |
Author
developer-guy
commented
Mar 5, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment