Skip to content

Commit

Permalink
Fix some login issues (#881)
Browse files Browse the repository at this point in the history
NewCmdAuthLogin now requires you to pass in your usage string, so that
it is no longer hard-coded to "crane auth". By default, if you pass
nothing, it will use os.Args[0].

Also log the config file that we used.
  • Loading branch information
jonjohnsonjr authored Dec 22, 2020
1 parent 39d4b23 commit 91666fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
49 changes: 33 additions & 16 deletions cmd/crane/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
Expand All @@ -30,7 +31,7 @@ import (
)

// NewCmdAuth creates a new cobra.Command for the auth subcommand.
func NewCmdAuth() *cobra.Command {
func NewCmdAuth(argv ...string) *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Short: "Log in or access credentials",
Expand All @@ -39,19 +40,25 @@ func NewCmdAuth() *cobra.Command {
cmd.Usage()
},
}
cmd.AddCommand(NewCmdAuthGet(), NewCmdAuthLogin())
cmd.AddCommand(NewCmdAuthGet(argv...), NewCmdAuthLogin(argv...))
return cmd
}

// NewCmdAuthGet creates a new `crane auth get` command.
func NewCmdAuthGet() *cobra.Command {
func NewCmdAuthGet(argv ...string) *cobra.Command {
if len(argv) == 0 {
argv = []string{os.Args[0]}
}

eg := fmt.Sprintf(` # Read configured credentials for reg.example.com
echo "reg.example.com" | %s get
{"username":"AzureDiamond","password":"hunter2"}`, strings.Join(argv, " "))

return &cobra.Command{
Use: "get",
Short: "Implements a credential helper",
Example: ` # Read configured credentials for reg.example.com
echo "reg.example.com" | crane auth get
{"username":"AzureDiamond","password":"hunter2"}`,
Args: cobra.NoArgs,
Use: "get",
Short: "Implements a credential helper",
Example: eg,
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, args []string) {
b, err := ioutil.ReadAll(os.Stdin)
if err != nil {
Expand All @@ -77,15 +84,21 @@ func NewCmdAuthGet() *cobra.Command {
}

// NewCmdAuthLogin creates a new `crane auth login` command.
func NewCmdAuthLogin() *cobra.Command {
func NewCmdAuthLogin(argv ...string) *cobra.Command {
var opts loginOptions

if len(argv) == 0 {
argv = []string{os.Args[0]}
}

eg := fmt.Sprintf(` # Log in to reg.example.com
%s login reg.example.com -u AzureDiamond -p hunter2`, strings.Join(argv, " "))

cmd := &cobra.Command{
Use: "login [OPTIONS] [SERVER]",
Short: "Log in to a registry",
Example: ` # Log in to reg.example.com
crane auth login reg.example.com -u AzureDiamond -p hunter2`,
Args: cobra.ExactArgs(1),
Use: "login [OPTIONS] [SERVER]",
Short: "Log in to a registry",
Example: eg,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
reg, err := name.NewRegistry(args[0])
if err != nil {
Expand Down Expand Up @@ -145,5 +158,9 @@ func login(opts loginOptions) error {
return err
}

return cf.Save()
if err := cf.Save(); err != nil {
return err
}
log.Printf("logged in via %s", cf.Filename)
return nil
}
2 changes: 1 addition & 1 deletion cmd/crane/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func New(use, short string, options []crane.Option) *cobra.Command {
commands := []*cobra.Command{
NewCmdAppend(&options),
NewCmdBlob(&options),
NewCmdAuth(),
NewCmdAuth("crane", "auth"),
NewCmdCatalog(&options),
NewCmdConfig(&options),
NewCmdCopy(&options),
Expand Down
2 changes: 1 addition & 1 deletion cmd/gcrane/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
root := cmd.New(use, short, []crane.Option{crane.WithAuthFromKeychain(gcrane.Keychain)})

// Add or override commands.
gcraneCmds := []*cobra.Command{gcmd.NewCmdList(), gcmd.NewCmdGc(), gcmd.NewCmdCopy()}
gcraneCmds := []*cobra.Command{gcmd.NewCmdList(), gcmd.NewCmdGc(), gcmd.NewCmdCopy(), cmd.NewCmdAuth("gcrane", "auth")}

// Maintain a map of google-specific commands that we "override".
used := make(map[string]bool)
Expand Down

0 comments on commit 91666fb

Please sign in to comment.