|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + "github.com/charmbracelet/lipgloss" |
| 8 | + "github.com/major-technology/cli/clients/git" |
| 9 | + mjrToken "github.com/major-technology/cli/clients/token" |
| 10 | + "github.com/major-technology/cli/cmd/user" |
| 11 | + "github.com/major-technology/cli/errors" |
| 12 | + "github.com/major-technology/cli/singletons" |
| 13 | + "github.com/major-technology/cli/utils" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +// linkCmd represents the link command (hidden - used by install script) |
| 18 | +var linkCmd = &cobra.Command{ |
| 19 | + Use: "link [application-id]", |
| 20 | + Short: "Link and run an application locally", |
| 21 | + Long: `Links an application by ID - logs in if needed, clones the repository, and starts the development server.`, |
| 22 | + Args: cobra.ExactArgs(1), |
| 23 | + Hidden: true, |
| 24 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 25 | + return runLink(cmd, args[0]) |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +func init() { |
| 30 | + Cmd.AddCommand(linkCmd) |
| 31 | +} |
| 32 | + |
| 33 | +func runLink(cmd *cobra.Command, applicationID string) error { |
| 34 | + // Step 1: Check if user is logged in, login if not |
| 35 | + _, err := mjrToken.GetToken() |
| 36 | + if err != nil { |
| 37 | + if err := user.RunLoginForLink(cmd); err != nil { |
| 38 | + return errors.WrapError("failed to login", err) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Step 2: Fetch application info using the application ID |
| 43 | + cmd.Println("Fetching application info...") |
| 44 | + apiClient := singletons.GetAPIClient() |
| 45 | + |
| 46 | + appInfo, err := apiClient.GetApplicationForLink(applicationID) |
| 47 | + if err != nil { |
| 48 | + return errors.WrapError("failed to get application info", err) |
| 49 | + } |
| 50 | + |
| 51 | + cmd.Printf("Found application: %s\n", appInfo.Name) |
| 52 | + |
| 53 | + // Step 3: Clone the repository |
| 54 | + desiredDir := sanitizeDirName(appInfo.Name) |
| 55 | + workingDir := desiredDir |
| 56 | + |
| 57 | + // Check if directory exists |
| 58 | + if _, err := os.Stat(desiredDir); err == nil { |
| 59 | + cmd.Printf("Directory '%s' already exists. Pulling latest changes...\n", workingDir) |
| 60 | + if gitErr := git.Pull(workingDir); gitErr != nil { |
| 61 | + if isGitAuthError(gitErr) { |
| 62 | + // Ensure repository access |
| 63 | + if err := utils.EnsureRepositoryAccess(cmd, applicationID, appInfo.CloneURLSSH, appInfo.CloneURLHTTPS); err != nil { |
| 64 | + return errors.WrapError("failed to ensure repository access", err) |
| 65 | + } |
| 66 | + // Retry |
| 67 | + if err := git.Pull(workingDir); err != nil { |
| 68 | + return errors.ErrorGitRepositoryAccessFailed |
| 69 | + } |
| 70 | + } else { |
| 71 | + return errors.ErrorGitCloneFailed |
| 72 | + } |
| 73 | + } |
| 74 | + } else { |
| 75 | + cmd.Printf("Cloning repository to '%s'...\n", workingDir) |
| 76 | + _, gitErr := cloneRepository(appInfo.CloneURLSSH, appInfo.CloneURLHTTPS, workingDir) |
| 77 | + if gitErr != nil { |
| 78 | + if isGitAuthError(gitErr) { |
| 79 | + // Ensure repository access |
| 80 | + if err := utils.EnsureRepositoryAccess(cmd, applicationID, appInfo.CloneURLSSH, appInfo.CloneURLHTTPS); err != nil { |
| 81 | + return errors.WrapError("failed to ensure repository access", err) |
| 82 | + } |
| 83 | + // Retry with retries |
| 84 | + gitErr = pullOrCloneWithRetries(cmd, workingDir, appInfo.CloneURLSSH, appInfo.CloneURLHTTPS) |
| 85 | + if gitErr != nil { |
| 86 | + return errors.ErrorGitRepositoryAccessFailed |
| 87 | + } |
| 88 | + } else { |
| 89 | + return errors.ErrorGitCloneFailed |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + cmd.Println("✓ Repository ready") |
| 95 | + |
| 96 | + // Step 4: Generate .env file |
| 97 | + cmd.Println("Generating .env file...") |
| 98 | + envFilePath, _, err := generateEnvFile(workingDir) |
| 99 | + if err != nil { |
| 100 | + return errors.WrapError("failed to generate .env file", err) |
| 101 | + } |
| 102 | + cmd.Printf("✓ Generated .env file at: %s\n", envFilePath) |
| 103 | + |
| 104 | + // Step 5: Print success and run start |
| 105 | + printLinkSuccessMessage(cmd, workingDir, appInfo.Name) |
| 106 | + |
| 107 | + // Step 6: Run pnpm install and pnpm dev in the target directory |
| 108 | + return RunStartInDir(cmd, workingDir) |
| 109 | +} |
| 110 | + |
| 111 | +func printLinkSuccessMessage(cmd *cobra.Command, dir, appName string) { |
| 112 | + // Define styles |
| 113 | + successStyle := lipgloss.NewStyle(). |
| 114 | + Bold(true). |
| 115 | + Foreground(lipgloss.Color("10")). // Green |
| 116 | + MarginTop(1). |
| 117 | + MarginBottom(1) |
| 118 | + |
| 119 | + boxStyle := lipgloss.NewStyle(). |
| 120 | + Border(lipgloss.RoundedBorder()). |
| 121 | + BorderForeground(lipgloss.Color("12")). // Blue |
| 122 | + Padding(1, 2). |
| 123 | + MarginTop(1). |
| 124 | + MarginBottom(1) |
| 125 | + |
| 126 | + pathStyle := lipgloss.NewStyle(). |
| 127 | + Bold(true). |
| 128 | + Foreground(lipgloss.Color("14")) // Cyan |
| 129 | + |
| 130 | + // Build the message |
| 131 | + successMsg := successStyle.Render(fmt.Sprintf("🎉 Successfully linked %s!", appName)) |
| 132 | + |
| 133 | + content := fmt.Sprintf("Application cloned to: %s", pathStyle.Render(dir)) |
| 134 | + |
| 135 | + box := boxStyle.Render(content) |
| 136 | + |
| 137 | + // Print everything |
| 138 | + cmd.Println(successMsg) |
| 139 | + cmd.Println(box) |
| 140 | +} |
0 commit comments