| 23 | } |
| 24 | |
| 25 | func NewCmdToken(f *cmdutil.Factory, runF func(*TokenOptions) error) *cobra.Command { |
| 26 | opts := &TokenOptions{ |
| 27 | IO: f.IOStreams, |
| 28 | Config: f.Config, |
| 29 | } |
| 30 | |
| 31 | cmd := &cobra.Command{ |
| 32 | Use: "token", |
| 33 | Short: "Print the authentication token gh uses for a hostname and account", |
| 34 | Long: heredoc.Docf(` |
| 35 | This command outputs the authentication token for an account on a given GitHub host. |
| 36 | |
| 37 | Without the %[1]s--hostname%[1]s flag, the default host is chosen. |
| 38 | |
| 39 | Without the %[1]s--user%[1]s flag, the active account for the host is chosen. |
| 40 | |
| 41 | If the token is an expired or near-expiry token that supports refreshing, it is refreshed |
| 42 | automatically before being printed, unless %[1]s--no-refresh%[1]s is given. |
| 43 | `, "`"), |
| 44 | Args: cobra.ExactArgs(0), |
| 45 | RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | if runF != nil { |
| 47 | return runF(opts) |
| 48 | } |
| 49 | |
| 50 | return tokenRun(opts) |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The hostname of the GitHub instance authenticated with") |
| 55 | cmd.Flags().StringVarP(&opts.Username, "user", "u", "", "The account to output the token for") |
| 56 | cmd.Flags().BoolVarP(&opts.SecureStorage, "secure-storage", "", false, "Search only secure credential store for authentication token") |
| 57 | _ = cmd.Flags().MarkHidden("secure-storage") |
| 58 | cmd.Flags().BoolVarP(&opts.NoRefresh, "no-refresh", "", false, "Do not automatically refresh an expired token") |
| 59 | |
| 60 | return cmd |
| 61 | } |
| 62 | |
| 63 | func tokenRun(opts *TokenOptions) error { |
| 64 | cfg, err := opts.Config() |