From 8f04937a8fcc23d665b462fa572f1177f6705d64 Mon Sep 17 00:00:00 2001 From: Jason Bao Date: Tue, 4 Nov 2025 17:57:57 -0800 Subject: [PATCH] Pull finished --- clients/api/structs.go | 2 ++ cmd/app/create.go | 9 --------- cmd/app/helper.go | 35 +++++++++++++++++++++++++++++++++++ cmd/app/pull.go | 10 +++++++++- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/clients/api/structs.go b/clients/api/structs.go index f7c19ce..647c303 100644 --- a/clients/api/structs.go +++ b/clients/api/structs.go @@ -115,6 +115,8 @@ type ApplicationItem struct { ID string `json:"id"` Name string `json:"name"` GithubRepositoryName string `json:"githubRepositoryName"` + CloneURLSSH string `json:"cloneUrlSsh"` + CloneURLHTTPS string `json:"cloneUrlHttps"` } // GetOrganizationApplicationsRequest represents the request body for POST /organizations/applications diff --git a/cmd/app/create.go b/cmd/app/create.go index dec2f0d..db2d4c7 100644 --- a/cmd/app/create.go +++ b/cmd/app/create.go @@ -3,7 +3,6 @@ package app import ( "fmt" "os" - "os/exec" "path/filepath" "github.com/charmbracelet/huh" @@ -168,11 +167,3 @@ func runCreate(cobraCmd *cobra.Command) error { return nil } - -// canUseSSH checks if SSH is available and configured for git -func canUseSSH() bool { - // Check if ssh-agent is running and has keys - cmd := exec.Command("ssh-add", "-l") - err := cmd.Run() - return err == nil -} diff --git a/cmd/app/helper.go b/cmd/app/helper.go index ee40b9a..65edfbd 100644 --- a/cmd/app/helper.go +++ b/cmd/app/helper.go @@ -2,6 +2,7 @@ package app import ( "fmt" + "os/exec" "github.com/major-technology/cli/clients/git" "github.com/major-technology/cli/singletons" @@ -45,3 +46,37 @@ func getApplicationIDFromDir(dir string) (string, error) { return appResp.ApplicationID, nil } + +// canUseSSH checks if SSH is available and configured for git +func canUseSSH() bool { + // Check if ssh-agent is running and has keys + cmd := exec.Command("ssh-add", "-l") + err := cmd.Run() + return err == nil +} + +// cloneRepository clones a repository using SSH or HTTPS based on availability +// Returns the clone method used ("SSH" or "HTTPS") and any error +func cloneRepository(sshURL, httpsURL, targetDir string) (string, error) { + // Determine which clone URL to use + useSSH := false + if canUseSSH() && sshURL != "" { + useSSH = true + } else if httpsURL == "" { + return "", fmt.Errorf("no valid clone method available") + } + + cloneURL := httpsURL + cloneMethod := "HTTPS" + if useSSH { + cloneURL = sshURL + cloneMethod = "SSH" + } + + // Clone the repository + if err := git.Clone(cloneURL, targetDir); err != nil { + return "", fmt.Errorf("failed to clone repository using %s: %w", cloneMethod, err) + } + + return cloneMethod, nil +} diff --git a/cmd/app/pull.go b/cmd/app/pull.go index 1695480..511e8a4 100644 --- a/cmd/app/pull.go +++ b/cmd/app/pull.go @@ -71,7 +71,15 @@ func runPull(cmd *cobra.Command) error { cmd.Println("Successfully pulled latest changes") } else if os.IsNotExist(err) { - return fmt.Errorf("directory '%s' does not exist. Please clone the repository first", targetDir) + // Directory doesn't exist, clone it + cmd.Printf("Directory '%s' does not exist. Cloning repository...\n", targetDir) + + cloneMethod, err := cloneRepository(selectedApp.CloneURLSSH, selectedApp.CloneURLHTTPS, targetDir) + if err != nil { + return fmt.Errorf("failed to clone repository: %w", err) + } + + cmd.Printf("✓ Successfully cloned repository using %s\n", cloneMethod) } else { return fmt.Errorf("failed to check directory: %w", err) }