|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/viper" |
| 9 | + "golang.org/x/term" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var configureCmd = &cobra.Command{ |
| 15 | + Use: "configure", |
| 16 | + Short: "Creates the configuration file needed to interact with the DNS API.", |
| 17 | + Long: `Creates the configuration file needed to interact with the DNS API.`, |
| 18 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 19 | + // For the configure command, we cannot have the authentication token and project id set as required flags. |
| 20 | + // As that would prevent the configure command from running when no config file was written yet. As cobra does |
| 21 | + // not provide an inverse for MarkPersistentFlagRequired(), we set the required annotation on the root command |
| 22 | + // ourselves. |
| 23 | + _ = rootCmd.PersistentFlags().SetAnnotation("authentication-token", cobra.BashCompOneRequiredFlag, []string{"false"}) |
| 24 | + _ = rootCmd.PersistentFlags().SetAnnotation("project-id", cobra.BashCompOneRequiredFlag, []string{"false"}) |
| 25 | + }, |
| 26 | + Run: func(cmd *cobra.Command, args []string) { |
| 27 | + fmt.Printf("Authentication Token [%s]: ", authenticationToken) |
| 28 | + input, err := readLongString() |
| 29 | + if err != nil { |
| 30 | + fmt.Printf("ERROR: Failed to read user input: %v\n", err) |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + authToken := authenticationToken |
| 35 | + input = strings.TrimSpace(input) |
| 36 | + if input != "" { |
| 37 | + authToken = input |
| 38 | + } |
| 39 | + viper.Set("authentication-token", authToken) |
| 40 | + |
| 41 | + projectId, err := parseClaimFromJWT(authToken, "stackit/project/project.id") |
| 42 | + if err != nil { |
| 43 | + fmt.Printf("ERROR: Failed to parse the Authentication Token: %v\n", err) |
| 44 | + return |
| 45 | + } |
| 46 | + viper.Set("project-id", projectId) |
| 47 | + |
| 48 | + // Viper has a known issue with WriteConfig running into an error in case the config file does not exist. Therefore, |
| 49 | + // we first try SafeWriteConfig which only works in cases where the config file does not exist. If that |
| 50 | + // fails, the config file is probably already there, and we use WriteConfig. |
| 51 | + if err := viper.SafeWriteConfig(); err != nil { |
| 52 | + if err := viper.WriteConfig(); err != nil { |
| 53 | + fmt.Printf("ERROR: Failed to write config: %v\n", err) |
| 54 | + return |
| 55 | + } |
| 56 | + } |
| 57 | + fmt.Println("Configuration successfully written") |
| 58 | + }, |
| 59 | +} |
| 60 | + |
| 61 | +func init() { |
| 62 | + rootCmd.AddCommand(configureCmd) |
| 63 | +} |
| 64 | + |
| 65 | +// readLongString provides a way to read more than 1024 characters from the terminal by switching the terminal into |
| 66 | +// raw mode. Otherwise, long strings like the authentication token would be truncated to 1024 characters because of |
| 67 | +// canonical input mode for terminals. |
| 68 | +func readLongString() (string, error) { |
| 69 | + termStateBackup, _ := term.MakeRaw(int(os.Stdin.Fd())) |
| 70 | + defer term.Restore(int(os.Stdin.Fd()), termStateBackup) // nolint:errcheck |
| 71 | + |
| 72 | + result := "" |
| 73 | + characters := make([]byte, 1024) |
| 74 | + for { |
| 75 | + n, err := os.Stdin.Read(characters) |
| 76 | + if err != nil { |
| 77 | + return result, err |
| 78 | + } |
| 79 | + for i := 0; i < n; i++ { |
| 80 | + if characters[i] == 0x03 || // Ctrl+C |
| 81 | + characters[i] == 0x0d { // Enter |
| 82 | + fmt.Print("\r\n") |
| 83 | + return result, nil |
| 84 | + } |
| 85 | + fmt.Printf("%s", string(characters[i])) |
| 86 | + result = result + string(characters[i]) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments