MCPcopy Index your code
hub / github.com/cli/cli / tokenRun

Function tokenRun

pkg/cmd/auth/token/token.go:57–100  ·  view source on GitHub ↗
(opts *TokenOptions)

Source from the content-addressed store, hash-verified

55}
56
57func 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}

Callers 3

TestTokenRunFunction · 0.85
NewCmdTokenFunction · 0.85

Calls 7

ConfigMethod · 0.65
AuthenticationMethod · 0.65
DefaultHostMethod · 0.65
TokenFromKeyringMethod · 0.65
ActiveTokenMethod · 0.65
TokenForUserMethod · 0.65

Tested by 2

TestTokenRunFunction · 0.68