|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/charmbracelet/huh" |
| 8 | + "github.com/major-technology/cli/clients/api" |
| 9 | + "github.com/major-technology/cli/clients/git" |
| 10 | + "github.com/major-technology/cli/singletons" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// deployCmd represents the deploy command |
| 15 | +var deployCmd = &cobra.Command{ |
| 16 | + Use: "deploy", |
| 17 | + Short: "Deploy a new version of the application", |
| 18 | + Long: `Creates a new version by committing and pushing changes, then deploying to the platform.`, |
| 19 | + Run: func(cobraCmd *cobra.Command, args []string) { |
| 20 | + cobra.CheckErr(runDeploy(cobraCmd)) |
| 21 | + }, |
| 22 | +} |
| 23 | + |
| 24 | +func runDeploy(cobraCmd *cobra.Command) error { |
| 25 | + // Check if we're in a git repository |
| 26 | + if !git.IsGitRepository() { |
| 27 | + return fmt.Errorf("not in a git repository") |
| 28 | + } |
| 29 | + |
| 30 | + // Check for uncommitted changes |
| 31 | + hasChanges, err := git.HasUncommittedChanges() |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to check for uncommitted changes: %w", err) |
| 34 | + } |
| 35 | + |
| 36 | + if hasChanges { |
| 37 | + cobraCmd.Println("📝 Uncommitted changes detected") |
| 38 | + |
| 39 | + // Prompt for commit message |
| 40 | + var commitMessage string |
| 41 | + form := huh.NewForm( |
| 42 | + huh.NewGroup( |
| 43 | + huh.NewText(). |
| 44 | + Title("Commit Message"). |
| 45 | + Description("Enter a commit message for your changes"). |
| 46 | + Value(&commitMessage). |
| 47 | + Validate(func(s string) error { |
| 48 | + if strings.TrimSpace(s) == "" { |
| 49 | + return fmt.Errorf("commit message is required") |
| 50 | + } |
| 51 | + return nil |
| 52 | + }), |
| 53 | + ), |
| 54 | + ) |
| 55 | + |
| 56 | + if err := form.Run(); err != nil { |
| 57 | + return fmt.Errorf("failed to collect commit message: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + // Stage all changes |
| 61 | + cobraCmd.Println("\nStaging changes...") |
| 62 | + if err := git.Add(); err != nil { |
| 63 | + return fmt.Errorf("failed to stage changes: %w", err) |
| 64 | + } |
| 65 | + cobraCmd.Println("✓ Changes staged") |
| 66 | + |
| 67 | + // Commit changes |
| 68 | + cobraCmd.Println("Committing changes...") |
| 69 | + if err := git.Commit(commitMessage); err != nil { |
| 70 | + return fmt.Errorf("failed to commit changes: %w", err) |
| 71 | + } |
| 72 | + cobraCmd.Println("✓ Changes committed") |
| 73 | + |
| 74 | + // Push to remote |
| 75 | + cobraCmd.Println("Pushing to remote...") |
| 76 | + if err := git.PushToMain(); err != nil { |
| 77 | + return fmt.Errorf("failed to push changes: %w", err) |
| 78 | + } |
| 79 | + cobraCmd.Println("✓ Changes pushed to remote") |
| 80 | + } else { |
| 81 | + cobraCmd.Println("✓ No uncommitted changes") |
| 82 | + } |
| 83 | + |
| 84 | + // Get application ID |
| 85 | + cobraCmd.Println("\nDeploying new version...") |
| 86 | + applicationID, err := getApplicationID() |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to get application ID: %w", err) |
| 89 | + } |
| 90 | + |
| 91 | + // Call API to create new version |
| 92 | + apiClient := singletons.GetAPIClient() |
| 93 | + resp, err := apiClient.CreateApplicationVersion(applicationID) |
| 94 | + if ok := api.CheckErr(cobraCmd, err); !ok { |
| 95 | + return err |
| 96 | + } |
| 97 | + |
| 98 | + cobraCmd.Printf("\n✓ Version deployed successfully!\n") |
| 99 | + cobraCmd.Printf(" Version ID: %s\n", resp.VersionID) |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
0 commit comments