|
5 | 5 | "os" |
6 | 6 | "path/filepath" |
7 | 7 |
|
| 8 | + "github.com/charmbracelet/bubbles/key" |
8 | 9 | "github.com/charmbracelet/huh" |
9 | 10 | "github.com/charmbracelet/lipgloss" |
10 | 11 | "github.com/major-technology/cli/clients/api" |
@@ -134,6 +135,13 @@ func runCreate(cobraCmd *cobra.Command) error { |
134 | 135 | return fmt.Errorf("failed to ensure repository access: %w", err) |
135 | 136 | } |
136 | 137 |
|
| 138 | + // Select resources for the application |
| 139 | + cobraCmd.Println("\nSelecting resources for your application...") |
| 140 | + if err := selectApplicationResources(cobraCmd, orgID, createResp.ApplicationID); err != nil { |
| 141 | + errorStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("9")) // Red |
| 142 | + cobraCmd.Println(errorStyle.Render("Failed to configure resources. Please run 'major app resources' to configure them later.")) |
| 143 | + } |
| 144 | + |
137 | 145 | // Push to the new remote |
138 | 146 | cobraCmd.Println("\nPushing to new repository...") |
139 | 147 | if err := git.Push(tempDir); err != nil { |
@@ -242,3 +250,80 @@ func printSuccessMessage(cobraCmd *cobra.Command, appName string) { |
242 | 250 | cobraCmd.Println(cdInstruction) |
243 | 251 | cobraCmd.Println(box) |
244 | 252 | } |
| 253 | + |
| 254 | +// selectApplicationResources prompts the user to select resources for the application |
| 255 | +func selectApplicationResources(cobraCmd *cobra.Command, orgID, appID string) error { |
| 256 | + // Get the API client |
| 257 | + apiClient := singletons.GetAPIClient() |
| 258 | + |
| 259 | + // Fetch available resources |
| 260 | + resourcesResp, err := apiClient.GetResources(orgID) |
| 261 | + if ok := api.CheckErr(cobraCmd, err); !ok { |
| 262 | + return err |
| 263 | + } |
| 264 | + |
| 265 | + // Check if there are any resources available |
| 266 | + if len(resourcesResp.Resources) == 0 { |
| 267 | + cobraCmd.Println("No resources available in this organization.") |
| 268 | + return nil |
| 269 | + } |
| 270 | + |
| 271 | + // Create options for the multiselect |
| 272 | + options := make([]huh.Option[string], len(resourcesResp.Resources)) |
| 273 | + for i, resource := range resourcesResp.Resources { |
| 274 | + // Format: "Name - Description" |
| 275 | + label := resource.Name |
| 276 | + if resource.Description != "" { |
| 277 | + label = fmt.Sprintf("%s - %s", resource.Name, resource.Description) |
| 278 | + } |
| 279 | + options[i] = huh.NewOption(label, resource.ID) |
| 280 | + } |
| 281 | + |
| 282 | + // Create custom keymap where 'n' submits instead of enter |
| 283 | + customKeyMap := huh.NewDefaultKeyMap() |
| 284 | + customKeyMap.MultiSelect.Toggle = key.NewBinding( |
| 285 | + key.WithKeys(" ", "enter"), |
| 286 | + key.WithHelp("space/enter", "toggle"), |
| 287 | + ) |
| 288 | + customKeyMap.MultiSelect.Submit = key.NewBinding( |
| 289 | + key.WithKeys("n"), |
| 290 | + key.WithHelp("n", "continue"), |
| 291 | + ) |
| 292 | + // Disable the default next/prev behavior on enter |
| 293 | + customKeyMap.MultiSelect.Next = key.NewBinding( |
| 294 | + key.WithKeys("tab"), |
| 295 | + key.WithHelp("tab", "next field"), |
| 296 | + ) |
| 297 | + |
| 298 | + // Prompt user to select resources |
| 299 | + var selectedResourceIDs []string |
| 300 | + form := huh.NewForm( |
| 301 | + huh.NewGroup( |
| 302 | + huh.NewMultiSelect[string](). |
| 303 | + Title("Select resources for your application"). |
| 304 | + Description("Use space/enter to select, 'n' to continue"). |
| 305 | + Options(options...). |
| 306 | + Value(&selectedResourceIDs), |
| 307 | + ), |
| 308 | + ).WithKeyMap(customKeyMap) |
| 309 | + |
| 310 | + if err := form.Run(); err != nil { |
| 311 | + return fmt.Errorf("failed to collect resource selection: %w", err) |
| 312 | + } |
| 313 | + |
| 314 | + // If no resources selected, just return |
| 315 | + if len(selectedResourceIDs) == 0 { |
| 316 | + cobraCmd.Println("No resources selected.") |
| 317 | + return nil |
| 318 | + } |
| 319 | + |
| 320 | + // Save the selected resources |
| 321 | + cobraCmd.Printf("Saving %d selected resource(s)...\n", len(selectedResourceIDs)) |
| 322 | + _, err = apiClient.SaveApplicationResources(orgID, appID, selectedResourceIDs) |
| 323 | + if ok := api.CheckErr(cobraCmd, err); !ok { |
| 324 | + return err |
| 325 | + } |
| 326 | + |
| 327 | + cobraCmd.Printf("✓ Resources configured successfully\n") |
| 328 | + return nil |
| 329 | +} |
0 commit comments