runConfigInit implements the config init command
(cmd *cobra.Command)
| 73 | |
| 74 | // runConfigInit implements the config init command |
| 75 | func runConfigInit(cmd *cobra.Command) error { |
| 76 | outputPath, _ := cmd.Flags().GetString("output") |
| 77 | force, _ := cmd.Flags().GetBool("force") |
| 78 | |
| 79 | // Use default config path if not specified |
| 80 | if outputPath == "" { |
| 81 | outputPath = config.GetDefaultConfigPath() |
| 82 | } |
| 83 | |
| 84 | // Check if file already exists |
| 85 | if _, err := os.Stat(outputPath); err == nil && !force { |
| 86 | fmt.Printf("Configuration file already exists at: %s\n\n", outputPath) |
| 87 | fmt.Println("What would you like to do?") |
| 88 | fmt.Println("1. View current config (recommended)") |
| 89 | fmt.Println("2. Overwrite with fresh defaults") |
| 90 | fmt.Println("3. Cancel") |
| 91 | fmt.Print("\nChoose an option [1/2/3]: ") |
| 92 | |
| 93 | reader := bufio.NewReader(os.Stdin) |
| 94 | response, err := reader.ReadString('\n') |
| 95 | if err != nil { |
| 96 | return fmt.Errorf("failed to read input: %w", err) |
| 97 | } |
| 98 | |
| 99 | response = strings.TrimSpace(response) |
| 100 | switch response { |
| 101 | case "1", "": |
| 102 | // Show current config |
| 103 | fmt.Println("\nCurrent configuration:") |
| 104 | fmt.Println("─────────────────────") |
| 105 | |
| 106 | // Load and display config directly |
| 107 | configPath, _ := cmd.Flags().GetString("config") |
| 108 | cfg, err := config.LoadCLIConfig(configPath) |
| 109 | if err != nil { |
| 110 | return fmt.Errorf("failed to load configuration: %w", err) |
| 111 | } |
| 112 | |
| 113 | // Show config source information |
| 114 | configSource := getConfigSource(configPath) |
| 115 | fmt.Printf("# Configuration loaded from: %s\n\n", configSource) |
| 116 | |
| 117 | // Display config in YAML format |
| 118 | encoder := yaml.NewEncoder(os.Stdout) |
| 119 | encoder.SetIndent(2) |
| 120 | defer encoder.Close() |
| 121 | if err := encoder.Encode(cfg); err != nil { |
| 122 | return fmt.Errorf("failed to display config: %w", err) |
| 123 | } |
| 124 | |
| 125 | fmt.Printf("\nTo edit: %s\n", outputPath) |
| 126 | return nil |
| 127 | case "2": |
| 128 | // Continue with overwrite |
| 129 | fmt.Println("Overwriting existing config file...") |
| 130 | case "3": |
| 131 | fmt.Println("Operation cancelled.") |
| 132 | return nil |
no test coverage detected