MCPcopy Create free account
hub / github.com/cli/cli / Do

Method Do

internal/config/migration/multi_account.go:95–145  ·  view source on GitHub ↗
(c *config.Config)

Source from the content-addressed store, hash-verified

93}
94
95func (m MultiAccountDeprecated) Do(c *config.Config) error {
96 hostnames, err := c.Keys(hostsKey)
97 // [github.com, github.localhost]
98 // We wouldn't expect to have a hosts key when this is the first time anyone
99 // is logging in with the CLI.
100 if _, ok := errors.AsType[*config.KeyNotFoundError](err); ok {
101 return nil
102 }
103 if err != nil {
104 return CowardlyRefusalError{errors.New("couldn't get hosts configuration")}
105 }
106
107 // If there are no hosts then it doesn't matter whether we migrate or not,
108 // so lets avoid any confusion and say there's no migration required.
109 if len(hostnames) == 0 {
110 return nil
111 }
112
113 // Otherwise let's get to the business of migrating!
114 for _, hostname := range hostnames {
115 tokenSource, err := getToken(c, hostname)
116 // If no token existed for this host we'll remove the entry from the hosts file
117 // by deleting it and moving on to the next one.
118 if errors.Is(err, noTokenError) {
119 // The only error that can be returned here is the key not existing, which
120 // we know can't be true.
121 _ = c.Remove(append(hostsKey, hostname))
122 continue
123 }
124 // For any other error we'll error out
125 if err != nil {
126 return CowardlyRefusalError{fmt.Errorf("couldn't find oauth token for %q: %w", hostname, err)}
127 }
128
129 username, err := getUsername(c, hostname, tokenSource.token, m.Transport)
130 if err != nil {
131 issueURL := "https://github.com/cli/cli/issues/8441"
132 return CowardlyRefusalError{fmt.Errorf("couldn't get user name for %q please visit %s for help: %w", hostname, issueURL, err)}
133 }
134
135 if err := migrateConfig(c, hostname, username); err != nil {
136 return CowardlyRefusalError{fmt.Errorf("couldn't migrate config for %q: %w", hostname, err)}
137 }
138
139 if err := migrateToken(hostname, username, tokenSource); err != nil {
140 return CowardlyRefusalError{fmt.Errorf("couldn't migrate oauth token for %q: %w", hostname, err)}
141 }
142 }
143
144 return nil
145}
146
147func getToken(c *config.Config, hostname string) (tokenSource, error) {
148 if token, _ := c.Get(append(hostsKey, hostname, "oauth_token")); token != "" {

Calls 6

getTokenFunction · 0.85
getUsernameFunction · 0.85
migrateConfigFunction · 0.85
migrateTokenFunction · 0.85
RemoveMethod · 0.65
ErrorfMethod · 0.65