Skip to content

Commit

Permalink
feat: implement login/logout UX
Browse files Browse the repository at this point in the history
implemnt login UI based on https://github.com/notaryproject/notation/blob/main/specs/commandline/login.md
fix:
1. added `Login Succeeded` message when login succeeded
2. added username parameter validation
3. removed --plain-http global flag

Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Oct 26, 2022
1 parent f747031 commit 5027651
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cmd/notation/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
flagUsername = &pflag.Flag{
Name: "username",
Shorthand: "u",
Usage: "username for registry operations (if not specified, defaults to $NOTATION_USERNAME)",
Usage: "username for registry operations (default to $NOTATION_USERNAME if not specified)",
}
setflagUsername = func(fs *pflag.FlagSet, p *string) {
fs.StringVarP(p, flagUsername.Name, flagUsername.Shorthand, "", flagUsername.Usage)
Expand All @@ -25,15 +25,15 @@ var (
flagPassword = &pflag.Flag{
Name: "password",
Shorthand: "p",
Usage: "password for registry operations (if not specified, defaults to $NOTATION_PASSWORD)",
Usage: "password for registry operations (default to $NOTATION_PASSWORD if not specified)",
}
setFlagPassword = func(fs *pflag.FlagSet, p *string) {
fs.StringVarP(p, flagPassword.Name, flagPassword.Shorthand, "", flagPassword.Usage)
}

flagPlainHTTP = &pflag.Flag{
Name: "plain-http",
Usage: "Registry access via plain HTTP",
Usage: "registry access via plain HTTP",
DefValue: "false",
}
setFlagPlainHTTP = func(fs *pflag.FlagSet, p *bool) {
Expand Down
16 changes: 13 additions & 3 deletions cmd/notation/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func loginCommand(opts *loginOpts) *cobra.Command {
opts = &loginOpts{}
}
command := &cobra.Command{
Use: "login [options] [server]",
Use: "login [flags] <server>",
Short: "Provides credentials for authenticated registry operations",
Long: `notation login [options] [server]
Long: `Log in to an OCI registry
Example - Login with provided username and password:
notation login -u <user> -p <password> registry.example.com
Expand All @@ -44,13 +44,18 @@ Example - Login using $NOTATION_USERNAME $NOTATION_PASSWORD variables:
if err := readPassword(opts); err != nil {
return err
}

// check username
if opts.Username == "" {
return errors.New("username was not set.")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return runLogin(cmd, opts)
},
}
command.Flags().BoolVar(&opts.passwordStdin, "password-stdin", false, "Take the password from stdin")
command.Flags().BoolVar(&opts.passwordStdin, "password-stdin", false, "take the password from stdin")
opts.ApplyFlags(command.Flags())
return command
}
Expand All @@ -67,6 +72,7 @@ func runLogin(cmd *cobra.Command, opts *loginOpts) error {
if err != nil {
return fmt.Errorf("could not get the credentials store: %v", err)
}

// init creds
creds := newCredentialFromInput(
opts.Username,
Expand All @@ -75,6 +81,10 @@ func runLogin(cmd *cobra.Command, opts *loginOpts) error {
if err = nativeStore.Store(serverAddress, creds); err != nil {
return fmt.Errorf("failed to store credentials: %v", err)
}

if err == nil {
fmt.Println("Login Succeeded")
}
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/notation/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ func logoutCommand(opts *logoutOpts) *cobra.Command {
opts = &logoutOpts{}
}
return &cobra.Command{
Use: "logout [server]",
Use: "logout [flags] [server]",
Short: "Log out the specified registry hostname",
Long: "Log out from an OCI registry",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("no hostname specified")
Expand Down
1 change: 0 additions & 1 deletion cmd/notation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func main() {
pluginCommand(),
loginCommand(nil),
logoutCommand(nil))
cmd.PersistentFlags().Bool(flagPlainHTTP.Name, false, flagPlainHTTP.Usage)
if err := cmd.Execute(); err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 0 additions & 2 deletions specs/commandline/logout.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ Usage:
notation logout [flags] [server]
Flags:
--all log out from all logged in registries
-h, --help help for logout
--plain-http registry access via plain HTTP
```

## Usage
Expand Down

0 comments on commit 5027651

Please sign in to comment.