17
17
package main
18
18
19
19
import (
20
+ "bufio"
20
21
"context"
21
22
"errors"
22
23
"fmt"
23
24
"io"
24
25
"net/http"
25
26
"net/url"
27
+ "os"
26
28
"strings"
29
+ "syscall"
27
30
28
31
"github.com/containerd/containerd/errdefs"
29
32
"github.com/containerd/containerd/remotes/docker"
@@ -35,6 +38,7 @@ import (
35
38
"github.com/docker/docker/api/types"
36
39
"github.com/docker/docker/registry"
37
40
"golang.org/x/net/context/ctxhttp"
41
+ "golang.org/x/term"
38
42
39
43
"github.com/sirupsen/logrus"
40
44
"github.com/spf13/cobra"
@@ -308,6 +312,15 @@ func ConfigureAuthentication(authConfig *types.AuthConfig, options *loginOptions
308
312
options .username = authConfig .Username
309
313
}
310
314
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
+
311
324
if options .username == "" {
312
325
return fmt .Errorf ("error: Username is Required" )
313
326
}
@@ -324,11 +337,29 @@ func ConfigureAuthentication(authConfig *types.AuthConfig, options *loginOptions
324
337
}
325
338
326
339
if options .password == "" {
327
- return fmt .Errorf ("password is Required" )
340
+ return fmt .Errorf ("error: Password is Required" )
328
341
}
329
342
330
343
authConfig .Username = options .username
331
344
authConfig .Password = options .password
332
345
333
346
return nil
334
347
}
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