Skip to content

Commit c933f8e

Browse files
committed
enhance login prompt
Signed-off-by: fahed dorgaa <fahed.dorgaa@gmail.com>
1 parent 2f0cf75 commit c933f8e

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

cmd/nerdctl/login.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ func ConfigureAuthentification(authConfig *types.AuthConfig, options *loginOptio
308308
options.username = authConfig.Username
309309
}
310310

311+
if options.username == "" {
312+
fmt.Print("Enter Username: ")
313+
username, err := readUsername()
314+
if err != nil {
315+
return err
316+
}
317+
options.username = username
318+
}
319+
311320
if options.username == "" {
312321
return fmt.Errorf("error: Username is Required")
313322
}
@@ -324,7 +333,7 @@ func ConfigureAuthentification(authConfig *types.AuthConfig, options *loginOptio
324333
}
325334

326335
if options.password == "" {
327-
return fmt.Errorf("password is Required")
336+
return fmt.Errorf("error: Password is Required")
328337
}
329338

330339
authConfig.Username = options.username

cmd/nerdctl/login_unix.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
package main
2121

2222
import (
23+
"bufio"
2324
"fmt"
2425
"os"
26+
"strings"
2527
"syscall"
2628

2729
"golang.org/x/term"
@@ -46,3 +48,26 @@ func readPassword() (string, error) {
4648

4749
return string(bytePassword), nil
4850
}
51+
52+
func readUsername() (string, error) {
53+
var fd *os.File
54+
if term.IsTerminal(syscall.Stdin) {
55+
fd = os.Stdin
56+
} else {
57+
tty, err := os.Open("/dev/tty")
58+
if err != nil {
59+
return "", fmt.Errorf("error allocating terminal: %w", err)
60+
}
61+
defer tty.Close()
62+
fd = tty
63+
}
64+
65+
reader := bufio.NewReader(fd)
66+
username, err := reader.ReadString('\n')
67+
if err != nil {
68+
return "", fmt.Errorf("error reading username: %w", err)
69+
}
70+
username = strings.TrimSpace(username)
71+
72+
return username, nil
73+
}

cmd/nerdctl/login_windows.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
package main
1818

1919
import (
20+
"bufio"
2021
"fmt"
22+
"os"
23+
"strings"
2124
"syscall"
2225

2326
"golang.org/x/term"
@@ -37,3 +40,21 @@ func readPassword() (string, error) {
3740

3841
return string(bytePassword), nil
3942
}
43+
44+
func readUsername() (string, error) {
45+
var fd *os.File
46+
if term.IsTerminal(int(syscall.Stdin)) {
47+
fd = os.Stdin
48+
} else {
49+
return "", fmt.Errorf("error allocating terminal")
50+
}
51+
52+
reader := bufio.NewReader(fd)
53+
username, err := reader.ReadString('\n')
54+
if err != nil {
55+
return "", fmt.Errorf("error reading username: %w", err)
56+
}
57+
username = strings.TrimSpace(username)
58+
59+
return username, nil
60+
}

0 commit comments

Comments
 (0)