Skip to content

Commit e2cb9cf

Browse files
authored
Pull finished (#23)
1 parent b25dca7 commit e2cb9cf

4 files changed

Lines changed: 46 additions & 10 deletions

File tree

clients/api/structs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ type ApplicationItem struct {
115115
ID string `json:"id"`
116116
Name string `json:"name"`
117117
GithubRepositoryName string `json:"githubRepositoryName"`
118+
CloneURLSSH string `json:"cloneUrlSsh"`
119+
CloneURLHTTPS string `json:"cloneUrlHttps"`
118120
}
119121

120122
// GetOrganizationApplicationsRequest represents the request body for POST /organizations/applications

cmd/app/create.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package app
33
import (
44
"fmt"
55
"os"
6-
"os/exec"
76
"path/filepath"
87

98
"github.com/charmbracelet/huh"
@@ -168,11 +167,3 @@ func runCreate(cobraCmd *cobra.Command) error {
168167

169168
return nil
170169
}
171-
172-
// canUseSSH checks if SSH is available and configured for git
173-
func canUseSSH() bool {
174-
// Check if ssh-agent is running and has keys
175-
cmd := exec.Command("ssh-add", "-l")
176-
err := cmd.Run()
177-
return err == nil
178-
}

cmd/app/helper.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package app
22

33
import (
44
"fmt"
5+
"os/exec"
56

67
"github.com/major-technology/cli/clients/git"
78
"github.com/major-technology/cli/singletons"
@@ -45,3 +46,37 @@ func getApplicationIDFromDir(dir string) (string, error) {
4546

4647
return appResp.ApplicationID, nil
4748
}
49+
50+
// canUseSSH checks if SSH is available and configured for git
51+
func canUseSSH() bool {
52+
// Check if ssh-agent is running and has keys
53+
cmd := exec.Command("ssh-add", "-l")
54+
err := cmd.Run()
55+
return err == nil
56+
}
57+
58+
// cloneRepository clones a repository using SSH or HTTPS based on availability
59+
// Returns the clone method used ("SSH" or "HTTPS") and any error
60+
func cloneRepository(sshURL, httpsURL, targetDir string) (string, error) {
61+
// Determine which clone URL to use
62+
useSSH := false
63+
if canUseSSH() && sshURL != "" {
64+
useSSH = true
65+
} else if httpsURL == "" {
66+
return "", fmt.Errorf("no valid clone method available")
67+
}
68+
69+
cloneURL := httpsURL
70+
cloneMethod := "HTTPS"
71+
if useSSH {
72+
cloneURL = sshURL
73+
cloneMethod = "SSH"
74+
}
75+
76+
// Clone the repository
77+
if err := git.Clone(cloneURL, targetDir); err != nil {
78+
return "", fmt.Errorf("failed to clone repository using %s: %w", cloneMethod, err)
79+
}
80+
81+
return cloneMethod, nil
82+
}

cmd/app/pull.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ func runPull(cmd *cobra.Command) error {
7171

7272
cmd.Println("Successfully pulled latest changes")
7373
} else if os.IsNotExist(err) {
74-
return fmt.Errorf("directory '%s' does not exist. Please clone the repository first", targetDir)
74+
// Directory doesn't exist, clone it
75+
cmd.Printf("Directory '%s' does not exist. Cloning repository...\n", targetDir)
76+
77+
cloneMethod, err := cloneRepository(selectedApp.CloneURLSSH, selectedApp.CloneURLHTTPS, targetDir)
78+
if err != nil {
79+
return fmt.Errorf("failed to clone repository: %w", err)
80+
}
81+
82+
cmd.Printf("✓ Successfully cloned repository using %s\n", cloneMethod)
7583
} else {
7684
return fmt.Errorf("failed to check directory: %w", err)
7785
}

0 commit comments

Comments
 (0)