@@ -2,6 +2,7 @@ package app
22
33import (
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+ }
0 commit comments