| 100 | } |
| 101 | |
| 102 | func logoutRun(opts *LogoutOptions) error { |
| 103 | hostname := opts.Hostname |
| 104 | username := opts.Username |
| 105 | |
| 106 | cfg, err := opts.Config() |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | authCfg := cfg.Authentication() |
| 111 | |
| 112 | knownHosts := authCfg.Hosts() |
| 113 | if len(knownHosts) == 0 { |
| 114 | return fmt.Errorf("not logged in to any hosts") |
| 115 | } |
| 116 | |
| 117 | if hostname != "" { |
| 118 | if !slices.Contains(knownHosts, hostname) { |
| 119 | return fmt.Errorf("not logged in to %s", hostname) |
| 120 | } |
| 121 | |
| 122 | if username != "" { |
| 123 | knownUsers := cfg.Authentication().UsersForHost(hostname) |
| 124 | if !slices.Contains(knownUsers, username) { |
| 125 | return fmt.Errorf("not logged in to %s account %s", hostname, username) |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | type hostUser struct { |
| 131 | host string |
| 132 | user string |
| 133 | } |
| 134 | var candidates []hostUser |
| 135 | |
| 136 | for _, host := range knownHosts { |
| 137 | if hostname != "" && host != hostname { |
| 138 | continue |
| 139 | } |
| 140 | knownUsers := cfg.Authentication().UsersForHost(host) |
| 141 | for _, user := range knownUsers { |
| 142 | if username != "" && user != username { |
| 143 | continue |
| 144 | } |
| 145 | candidates = append(candidates, hostUser{host: host, user: user}) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if len(candidates) == 0 { |
| 150 | return errors.New("no accounts matched that criteria") |
| 151 | } else if len(candidates) == 1 { |
| 152 | hostname = candidates[0].host |
| 153 | username = candidates[0].user |
| 154 | } else if !opts.IO.CanPrompt() { |
| 155 | return errors.New("unable to determine which account to log out of, please specify `--hostname` and `--user`") |
| 156 | } else { |
| 157 | prompts := make([]string, len(candidates)) |
| 158 | for i, c := range candidates { |
| 159 | prompts[i] = fmt.Sprintf("%s (%s)", c.user, c.host) |