Skip to content

Commit a8d279a

Browse files
authored
fix: add plain-http option for login cmd (#28)
Signed-off-by: chlins <[email protected]>
1 parent b469f83 commit a8d279a

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

cmd/login.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ var loginCmd = &cobra.Command{
4040
# login to docker hub:
4141
modctl login -u foo registry-1.docker.io
4242
43-
# login to insecure register:
44-
modctl login -u foo --insecure registry-insecure.io
43+
# login to registry served over http:
44+
modctl login -u foo --plain-http registry-insecure.io
4545
`,
4646
Args: cobra.ExactArgs(1),
4747
DisableAutoGenTag: true,
@@ -62,7 +62,7 @@ func init() {
6262
flags.StringVarP(&loginConfig.Username, "username", "u", "", "Username for login")
6363
flags.StringVarP(&loginConfig.Password, "password", "p", "", "Password for login")
6464
flags.BoolVar(&loginConfig.PasswordStdin, "password-stdin", true, "Take the password from stdin by default")
65-
flags.BoolVar(&loginConfig.Insecure, "insecure", false, "Allow insecure connections to registry")
65+
flags.BoolVar(&loginConfig.PlainHTTP, "plain-http", false, "Allow http connections to registry")
6666

6767
if err := viper.BindPFlags(flags); err != nil {
6868
panic(fmt.Errorf("bind cache login flags to viper: %w", err))
@@ -88,7 +88,13 @@ func runLogin(ctx context.Context, registry string) error {
8888
}
8989

9090
fmt.Println("\nLogging In...")
91-
if err := b.Login(ctx, registry, loginConfig.Username, loginConfig.Password, loginConfig.Insecure); err != nil {
91+
92+
opts := []backend.Option{}
93+
if loginConfig.PlainHTTP {
94+
opts = append(opts, backend.WithPlainHTTP())
95+
}
96+
97+
if err := b.Login(ctx, registry, loginConfig.Username, loginConfig.Password, opts...); err != nil {
9298
return err
9399
}
94100

pkg/backend/backend.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// Backend is the interface to represent the backend.
2626
type Backend interface {
2727
// Login logs into a registry.
28-
Login(ctx context.Context, registry, username, password string, insecure bool) error
28+
Login(ctx context.Context, registry, username, password string, opts ...Option) error
2929

3030
// Logout logs out from a registry.
3131
Logout(ctx context.Context, registry string) error

pkg/backend/login.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ package backend
1818

1919
import (
2020
"context"
21+
2122
"oras.land/oras-go/v2/registry/remote"
2223
"oras.land/oras-go/v2/registry/remote/auth"
2324
"oras.land/oras-go/v2/registry/remote/credentials"
2425
)
2526

2627
// Login logs into a registry.
27-
func (b *backend) Login(ctx context.Context, registry, username, password string, insecure bool) error {
28+
func (b *backend) Login(ctx context.Context, registry, username, password string, opts ...Option) error {
29+
// apply options.
30+
options := &Options{}
31+
for _, opt := range opts {
32+
opt(options)
33+
}
2834
// read credentials from docker store.
2935
store, err := credentials.NewStoreFromDocker(credentials.StoreOptions{AllowPlaintextPut: true})
3036
if err != nil {
@@ -35,7 +41,10 @@ func (b *backend) Login(ctx context.Context, registry, username, password string
3541
if err != nil {
3642
return err
3743
}
38-
reg.PlainHTTP = insecure
44+
45+
if options.plainHTTP {
46+
reg.PlainHTTP = true
47+
}
3948

4049
cred := auth.Credential{
4150
Username: username,

pkg/config/login.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ type Login struct {
2222
Username string
2323
Password string
2424
PasswordStdin bool
25-
Insecure bool
25+
PlainHTTP bool
2626
}
2727

2828
func NewLogin() *Login {
2929
return &Login{
3030
Username: "",
3131
Password: "",
3232
PasswordStdin: true,
33-
Insecure: false,
33+
PlainHTTP: false,
3434
}
3535
}
3636

0 commit comments

Comments
 (0)