| 55 | } |
| 56 | |
| 57 | func tokenRun(opts *TokenOptions) error { |
| 58 | cfg, err := opts.Config() |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | authCfg := cfg.Authentication() |
| 63 | |
| 64 | hostname := opts.Hostname |
| 65 | if hostname == "" { |
| 66 | hostname, _ = authCfg.DefaultHost() |
| 67 | } |
| 68 | |
| 69 | var val string |
| 70 | // If this conditional logic ends up being duplicated anywhere, |
| 71 | // we should consider making a factory function that returns the correct |
| 72 | // behavior. For now, keeping it all inline is simplest. |
| 73 | if opts.SecureStorage { |
| 74 | if opts.Username == "" { |
| 75 | val, _ = authCfg.TokenFromKeyring(hostname) |
| 76 | } else { |
| 77 | val, _ = authCfg.TokenFromKeyringForUser(hostname, opts.Username) |
| 78 | } |
| 79 | } else { |
| 80 | if opts.Username == "" { |
| 81 | val, _ = authCfg.ActiveToken(hostname) |
| 82 | } else { |
| 83 | val, _, _ = authCfg.TokenForUser(hostname, opts.Username) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if val == "" { |
| 88 | errMsg := fmt.Sprintf("no oauth token found for %s", hostname) |
| 89 | if opts.Username != "" { |
| 90 | errMsg += fmt.Sprintf(" account %s", opts.Username) |
| 91 | } |
| 92 | return errors.New(errMsg) |
| 93 | } |
| 94 | |
| 95 | if val != "" { |
| 96 | fmt.Fprintf(opts.IO.Out, "%s\n", val) |
| 97 | } |
| 98 | |
| 99 | return nil |
| 100 | } |