|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "os/exec" |
| 8 | + "runtime" |
| 9 | + "time" |
| 10 | + |
| 11 | + drycc "github.com/drycc/controller-sdk-go" |
| 12 | + "github.com/drycc/controller-sdk-go/api" |
| 13 | + "github.com/drycc/controller-sdk-go/auth" |
| 14 | + "github.com/drycc/controller-sdk-go/tokens" |
| 15 | + "github.com/drycc/workflow-cli/settings" |
| 16 | +) |
| 17 | + |
| 18 | +func (d *DryccCmd) TokensList(results int) error { |
| 19 | + s, err := settings.Load(d.ConfigFile) |
| 20 | + |
| 21 | + if err != nil { |
| 22 | + return err |
| 23 | + } |
| 24 | + |
| 25 | + if results == defaultLimit { |
| 26 | + results = s.Limit |
| 27 | + } |
| 28 | + |
| 29 | + tokens, _, err := tokens.List(s.Client, results) |
| 30 | + if d.checkAPICompatibility(s.Client, err) != nil { |
| 31 | + return err |
| 32 | + } |
| 33 | + table := d.getDefaultFormatTable([]string{"UUID", "OWNER", "ALIAS", "KEY", "CREATE", "UPDATED"}) |
| 34 | + for _, token := range tokens { |
| 35 | + table.Append([]string{ |
| 36 | + token.UUID, |
| 37 | + token.Owner, |
| 38 | + safeGetString(token.Alias), |
| 39 | + token.Key, |
| 40 | + token.Created, |
| 41 | + token.Updated, |
| 42 | + }) |
| 43 | + } |
| 44 | + table.Render() |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func (d *DryccCmd) TokensAdd(c *drycc.Client, username, password, alias, confirm string, render bool) (*api.AuthTokenResponse, error) { |
| 49 | + if c == nil { |
| 50 | + s, err := settings.Load(d.ConfigFile) |
| 51 | + |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + c = s.Client |
| 56 | + } |
| 57 | + |
| 58 | + if confirm == "" { |
| 59 | + d.Printf(` ! WARNING: Make sure to copy your token now. |
| 60 | + ! You won't be able to see it again, please confirm whether to continue. |
| 61 | + ! To proceed, type "yes" ! |
| 62 | +
|
| 63 | +> `) |
| 64 | + |
| 65 | + fmt.Scanln(&confirm) |
| 66 | + } |
| 67 | + |
| 68 | + if confirm != "yes" { |
| 69 | + return nil, fmt.Errorf("cancel the creation of %s's token, aborting", username) |
| 70 | + } |
| 71 | + |
| 72 | + key, err := auth.Login(c, username, password) |
| 73 | + if d.checkAPICompatibility(c, err) != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + if username == "" || password == "" { |
| 80 | + fmt.Printf("Opening browser to %s\n", key) |
| 81 | + d.Print("Waiting for login... ") |
| 82 | + err = d.openBrower(key) |
| 83 | + if err != nil { |
| 84 | + d.Print("Cannot open browser, please visit the website in yourself") |
| 85 | + } |
| 86 | + u, err := url.Parse(key) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + key = u.Query()["key"][0] |
| 91 | + } |
| 92 | + quit := progress(d.WOut) |
| 93 | + token, err := d.doToken(c, key, alias) |
| 94 | + quit <- true |
| 95 | + <-quit |
| 96 | + if render { |
| 97 | + table := d.getDefaultFormatTable([]string{"USERNAME", "TOKEN"}) |
| 98 | + table.Append([]string{token.Username, token.Token}) |
| 99 | + table.Render() |
| 100 | + } |
| 101 | + return token, err |
| 102 | +} |
| 103 | + |
| 104 | +func (d *DryccCmd) TokensRemove(id, confirm string) error { |
| 105 | + s, err := settings.Load(d.ConfigFile) |
| 106 | + |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + if confirm == "" { |
| 112 | + d.Printf(` ! WARNING: You cannot undo this action. |
| 113 | + ! Any using this token will no longer be able to access the Controller API. |
| 114 | + ! To proceed, type "yes" ! |
| 115 | +
|
| 116 | +> `) |
| 117 | + |
| 118 | + fmt.Scanln(&confirm) |
| 119 | + } |
| 120 | + |
| 121 | + if confirm != "yes" { |
| 122 | + d.Println("skip") |
| 123 | + return nil |
| 124 | + } |
| 125 | + |
| 126 | + err = tokens.Delete(s.Client, id) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + d.Println("done") |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +func (d *DryccCmd) openBrower(URL string) error { |
| 135 | + var commands = map[string]string{ |
| 136 | + "windows": "start", |
| 137 | + "darwin": "open", |
| 138 | + "linux": "xdg-open", |
| 139 | + } |
| 140 | + run, ok := commands[runtime.GOOS] |
| 141 | + if !ok { |
| 142 | + return errors.New("warning: Cannot open browser") |
| 143 | + } |
| 144 | + cmd := exec.Command(run, URL) |
| 145 | + err := cmd.Start() |
| 146 | + if err != nil { |
| 147 | + return errors.New("warning: Cannot open browser") |
| 148 | + } |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func (d *DryccCmd) doToken(c *drycc.Client, key, alias string) (*api.AuthTokenResponse, error) { |
| 154 | + var token api.AuthTokenResponse |
| 155 | + for i := 0; i <= 120; i++ { |
| 156 | + token, _ = auth.Token(c, key, alias) |
| 157 | + time.Sleep(time.Duration(5) * time.Second) |
| 158 | + if token.Token != "" && token.Username != "" { |
| 159 | + break |
| 160 | + } |
| 161 | + } |
| 162 | + if token.Token == "" || token.Token == "fail" { |
| 163 | + return nil, errors.New("logged fail") |
| 164 | + } |
| 165 | + return &token, nil |
| 166 | +} |
0 commit comments