Skip to content

Commit 5a999f4

Browse files
committed
Follow-up to enhance login prompt
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent c933f8e commit 5a999f4

File tree

3 files changed

+22
-46
lines changed

3 files changed

+22
-46
lines changed

cmd/nerdctl/login.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
package main
1818

1919
import (
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"
@@ -341,3 +345,21 @@ func ConfigureAuthentification(authConfig *types.AuthConfig, options *loginOptio
341345

342346
return nil
343347
}
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+
}

cmd/nerdctl/login_unix.go

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

2222
import (
23-
"bufio"
2423
"fmt"
2524
"os"
26-
"strings"
2725
"syscall"
2826

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

4947
return string(bytePassword), nil
5048
}
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: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
package main
1818

1919
import (
20-
"bufio"
2120
"fmt"
22-
"os"
23-
"strings"
2421
"syscall"
2522

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

4138
return string(bytePassword), nil
4239
}
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)