1717package main
1818
1919import (
20+ "bufio"
2021 "context"
2122 "errors"
2223 "fmt"
2324 "io"
2425 "net/http"
2526 "net/url"
27+ "os"
2628 "strings"
29+ "syscall"
2730
2831 "github.com/containerd/containerd/errdefs"
2932 "github.com/containerd/containerd/remotes/docker"
@@ -35,6 +38,7 @@ import (
3538 "github.com/docker/docker/api/types"
3639 "github.com/docker/docker/registry"
3740 "golang.org/x/net/context/ctxhttp"
41+ "golang.org/x/term"
3842
3943 "github.com/sirupsen/logrus"
4044 "github.com/spf13/cobra"
@@ -308,6 +312,15 @@ func ConfigureAuthentication(authConfig *types.AuthConfig, options *loginOptions
308312 options .username = authConfig .Username
309313 }
310314
315+ if options .username == "" {
316+ fmt .Print ("Enter Username: " )
317+ username , err := readUsername ()
318+ if err != nil {
319+ return err
320+ }
321+ options .username = username
322+ }
323+
311324 if options .username == "" {
312325 return fmt .Errorf ("error: Username is Required" )
313326 }
@@ -324,11 +337,29 @@ func ConfigureAuthentication(authConfig *types.AuthConfig, options *loginOptions
324337 }
325338
326339 if options .password == "" {
327- return fmt .Errorf ("password is Required" )
340+ return fmt .Errorf ("error: Password is Required" )
328341 }
329342
330343 authConfig .Username = options .username
331344 authConfig .Password = options .password
332345
333346 return nil
334347}
348+
349+ func readUsername () (string , error ) {
350+ var fd * os.File
351+ if term .IsTerminal (int (syscall .Stdin )) {
352+ fd = os .Stdin
353+ } else {
354+ return "" , fmt .Errorf ("stdin is not a terminal (Hint: use `nerdctl login --username=USERNAME --password-stdin`)" )
355+ }
356+
357+ reader := bufio .NewReader (fd )
358+ username , err := reader .ReadString ('\n' )
359+ if err != nil {
360+ return "" , fmt .Errorf ("error reading username: %w" , err )
361+ }
362+ username = strings .TrimSpace (username )
363+
364+ return username , nil
365+ }
0 commit comments