(target, name string, credentials []auth.Credential, choices []string)
| 200 | } |
| 201 | |
| 202 | func PromptCredential(target, name string, credentials []auth.Credential, choices []string) (auth.Credential, int, error) { |
| 203 | if len(credentials) == 0 && len(choices) == 0 { |
| 204 | return nil, 0, fmt.Errorf("no possible choice") |
| 205 | } |
| 206 | if len(credentials) == 0 && len(choices) == 1 { |
| 207 | return nil, 0, nil |
| 208 | } |
| 209 | |
| 210 | sort.Sort(auth.ById(credentials)) |
| 211 | |
| 212 | for { |
| 213 | _, _ = fmt.Fprintln(os.Stderr) |
| 214 | |
| 215 | offset := 0 |
| 216 | for i, choice := range choices { |
| 217 | _, _ = fmt.Fprintf(os.Stderr, "[%d]: %s\n", i+1, choice) |
| 218 | offset++ |
| 219 | } |
| 220 | |
| 221 | if len(credentials) > 0 { |
| 222 | _, _ = fmt.Fprintln(os.Stderr) |
| 223 | _, _ = fmt.Fprintf(os.Stderr, "Existing %s for %s:\n", name, target) |
| 224 | |
| 225 | for i, cred := range credentials { |
| 226 | meta := make([]string, 0, len(cred.Metadata())) |
| 227 | for k, v := range cred.Metadata() { |
| 228 | meta = append(meta, k+":"+v) |
| 229 | } |
| 230 | sort.Strings(meta) |
| 231 | metaFmt := strings.Join(meta, ",") |
| 232 | |
| 233 | fmt.Printf("[%d]: %s => (%s) (%s)\n", |
| 234 | i+1+offset, |
| 235 | colors.Cyan(cred.ID().Human()), |
| 236 | metaFmt, |
| 237 | cred.CreateTime().Format(time.RFC822), |
| 238 | ) |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | _, _ = fmt.Fprintln(os.Stderr) |
| 243 | _, _ = fmt.Fprintf(os.Stderr, "Select option: ") |
| 244 | |
| 245 | line, err := bufio.NewReader(os.Stdin).ReadString('\n') |
| 246 | _, _ = fmt.Fprintln(os.Stderr) |
| 247 | if err != nil { |
| 248 | return nil, 0, err |
| 249 | } |
| 250 | |
| 251 | line = strings.TrimSpace(line) |
| 252 | index, err := strconv.Atoi(line) |
| 253 | if err != nil || index < 1 || index > len(choices)+len(credentials) { |
| 254 | _, _ = fmt.Fprintln(os.Stderr, "invalid input") |
| 255 | continue |
| 256 | } |
| 257 | |
| 258 | switch { |
| 259 | case index <= len(choices): |
no test coverage detected