Skip to content

Commit

Permalink
added password-stdin flag for login
Browse files Browse the repository at this point in the history
Signed-off-by: ZiyanK <ziyankarmali786@gmail.com>
  • Loading branch information
ZiyanK committed Feb 12, 2024
1 parent b95a9b2 commit e0eba7a
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cmd/harbor/root/login.go
@@ -1,8 +1,10 @@
package root

import (
"bufio"
"context"
"fmt"
"os"

"github.com/goharbor/go-client/pkg/harbor"
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/user"
Expand All @@ -15,6 +17,7 @@ type loginOptions struct {
serverAddress string
username string
password string
passwordStdin bool
}

// LoginCommand creates a new `harbor login` command
Expand All @@ -28,7 +31,7 @@ func LoginCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.serverAddress = args[0]
return runLogin(opts)
return runLogin(cmd, opts)
},
}

Expand All @@ -40,14 +43,21 @@ func LoginCommand() *cobra.Command {
panic(err)
}
flags.StringVarP(&opts.password, "password", "p", "", "Password")
if err := cmd.MarkFlagRequired("password"); err != nil {
panic(err)
}
flags.BoolVarP(&opts.passwordStdin, "password-stdin", "", false, "Read password from stdin")

return cmd
}

func runLogin(opts loginOptions) error {
func runLogin(cmd *cobra.Command, opts loginOptions) error {
passwordStdin, _ := cmd.Flags().GetBool("password-stdin")
if passwordStdin {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter password: ")
password, _ := reader.ReadString('\n')
password = password[:len(password)-1]
opts.password = password
}

clientConfig := &harbor.ClientSetConfig{
URL: opts.serverAddress,
Username: opts.username,
Expand Down

0 comments on commit e0eba7a

Please sign in to comment.