Skip to content

Commit

Permalink
kd: add "credential use" command
Browse files Browse the repository at this point in the history
  • Loading branch information
rjeczalik committed Jan 6, 2017
1 parent 5c59309 commit 5424131
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
28 changes: 26 additions & 2 deletions go/src/koding/klientctl/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -252,14 +253,37 @@ func CredentialDescribe(c *cli.Context, log logging.Logger, _ string) (int, erro
return 0, nil
}

func CredentialUse(c *cli.Context, _ logging.Logger, _ string) (int, error) {
if c.NArg() != 1 {
cli.ShowCommandHelp(c, "use")
return 1, errors.New("missing argument")
}

arg := c.Args().Get(0)

if err := credential.Use(arg); err != nil {
return 1, errors.New("error changing default credential: " + err.Error())
}

return 0, nil
}

func printCreds(creds []stack.CredentialItem) {
w := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
defer w.Flush()

fmt.Fprintln(w, "ID\tTITLE\tTEAM\tPROVIDER")
used := credential.Used()

fmt.Fprintln(w, "ID\tTITLE\tTEAM\tPROVIDER\tUSED")

for _, cred := range creds {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", cred.Identifier, cred.Title, cred.Team, cred.Provider)
isused := "-"

if ident, ok := used[cred.Provider]; ok && cred.Identifier == ident {
isused = "default"
}

fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", cred.Identifier, cred.Title, cred.Team, cred.Provider, isused)
}
}

Expand Down
2 changes: 1 addition & 1 deletion go/src/koding/klientctl/endpoint/credential/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Client struct {
func (c *Client) List(opts *ListOptions) (stack.Credentials, error) {
c.init()

var req *stack.CredentialListRequest
var req = &stack.CredentialListRequest{}
var resp stack.CredentialListResponse

if opts != nil {
Expand Down
4 changes: 4 additions & 0 deletions go/src/koding/klientctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ func run(args []string) {
Usage: "Specify credential title.",
},
},
}, {
Name: "use",
Usage: "Change default credential per provider.",
Action: ctlcli.ExitErrAction(CredentialUse, log, "use"),
}, {
Name: "describe",
Usage: "Describe credential documents.",
Expand Down

0 comments on commit 5424131

Please sign in to comment.