Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clients/api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions cmd/app/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app
import (
"fmt"
"os"
"os/exec"
"path/filepath"

"github.com/charmbracelet/huh"
Expand Down Expand Up @@ -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
}
35 changes: 35 additions & 0 deletions cmd/app/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"os/exec"

"github.com/major-technology/cli/clients/git"
"github.com/major-technology/cli/singletons"
Expand Down Expand Up @@ -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
}
10 changes: 9 additions & 1 deletion cmd/app/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down