|
| 1 | +/* |
| 2 | + * Copyright 2020 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/lithammer/fuzzysearch/fuzzy" |
| 25 | + "github.com/manifoldco/promptui" |
| 26 | + "github.com/netflix/weep/pkg/creds" |
| 27 | +) |
| 28 | + |
| 29 | +// InteractiveRolePrompt will present the user with a fuzzy-searchable list of roles if |
| 30 | +// - We are currently attached to an interactive tty |
| 31 | +// - The user has not disabled them through the WEEP_DISABLE_INTERACTIVE_PROMPTS option |
| 32 | +func InteractiveRolePrompt(args []string, region string, client *creds.Client) (string, error) { |
| 33 | + // If a role was provided, just use that |
| 34 | + if len(args) > 0 { |
| 35 | + return args[0], nil |
| 36 | + } |
| 37 | + |
| 38 | + if !isRunningInTerminal() { |
| 39 | + return "", fmt.Errorf("no role provided, and cannot prompt for input") |
| 40 | + } |
| 41 | + |
| 42 | + if os.Getenv("WEEP_DISABLE_INTERACTIVE_PROMPTS") == "1" { |
| 43 | + return "", fmt.Errorf("no role provided, and interactive prompts are disabled") |
| 44 | + } |
| 45 | + |
| 46 | + // If a client was not provided, create one using the provided region |
| 47 | + if client == nil { |
| 48 | + var err error |
| 49 | + client, err = creds.GetClient(region) |
| 50 | + if err != nil { |
| 51 | + return "", err |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Retrieve the list of roles |
| 56 | + roles, err := client.Roles() |
| 57 | + if err != nil { |
| 58 | + return "", err |
| 59 | + } |
| 60 | + |
| 61 | + // Prompt the user |
| 62 | + prompt := promptui.Select{ |
| 63 | + Label: "Select Role", |
| 64 | + Items: roles, |
| 65 | + Size: 16, |
| 66 | + Searcher: func(input string, index int) bool { |
| 67 | + return fuzzy.MatchNormalized(strings.ToLower(input), strings.ToLower(roles[index])) |
| 68 | + }, |
| 69 | + StartInSearchMode: true, |
| 70 | + } |
| 71 | + |
| 72 | + _, role, err := prompt.Run() |
| 73 | + if err != nil { |
| 74 | + return "", err |
| 75 | + } |
| 76 | + |
| 77 | + return role, nil |
| 78 | +} |
| 79 | + |
| 80 | +func isRunningInTerminal() bool { |
| 81 | + fileInfo, _ := os.Stdout.Stat() |
| 82 | + return (fileInfo.Mode() & os.ModeCharDevice) != 0 |
| 83 | +} |
0 commit comments