Skip to content

Commit 51072e7

Browse files
authored
feat: Add Git SSH preflight checks (#677)
* add Git SSH preflight checks * fix message format
1 parent f731483 commit 51072e7

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

pkg/client/plural.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66
"strings"
77

8-
"github.com/pluralsh/plural-cli/pkg/utils/git"
8+
gitutils "github.com/pluralsh/plural-cli/pkg/utils/git"
99
"github.com/pluralsh/polly/algorithms"
1010
"github.com/samber/lo"
1111
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -105,6 +105,11 @@ func (p *Plural) HandleInit(c *cli.Context) error {
105105
repo := ""
106106
p.InitPluralClient()
107107

108+
git, err := wkspace.Preflight()
109+
if err != nil && git {
110+
return err
111+
}
112+
108113
if utils.Exists("./workspace.yaml") {
109114
utils.Highlight("Found workspace.yaml, skipping init as this repo has already been initialized\n")
110115
utils.Highlight("Checking domain...\n")
@@ -118,7 +123,7 @@ func (p *Plural) HandleInit(c *cli.Context) error {
118123
}
119124
}
120125
utils.Highlight("Domain OK \n")
121-
branch, err := git.CurrentBranch()
126+
branch, err := gitutils.CurrentBranch()
122127
if err != nil {
123128
return err
124129
}
@@ -133,11 +138,6 @@ func (p *Plural) HandleInit(c *cli.Context) error {
133138
return nil
134139
}
135140

136-
git, err := wkspace.Preflight()
137-
if err != nil && git {
138-
return err
139-
}
140-
141141
if err := common.HandleLogin(c); err != nil {
142142
return err
143143
}

pkg/wkspace/validator.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55
"fmt"
66
"os"
77
"os/exec"
8+
"strings"
89

910
"github.com/pluralsh/plural-cli/pkg/executor"
1011
"github.com/pluralsh/plural-cli/pkg/utils"
12+
"github.com/pluralsh/plural-cli/pkg/utils/git"
1113
)
1214

1315
func Preflight() (bool, error) {
@@ -17,13 +19,14 @@ func Preflight() (bool, error) {
1719
return true, utils.HighlightError(fmt.Errorf("%s not installed", req))
1820
}
1921
}
20-
fmt.Print("\nTesting if git ssh is properly configured...\n")
22+
23+
fmt.Print("\nTesting if git ssh is properly configured...")
2124
if err := checkGitSSH(); err != nil {
2225
fmt.Printf("%s\n\n", err.Error())
2326
utils.Warn("Please ensure that you have ssh keys set up for git and that you've added them to your ssh agent. You can use `plural crypto ssh-keygen` to create your first ssh keys then upload the public key to your git provider.\n")
2427
return true, fmt.Errorf("git ssh is not properly configured")
2528
}
26-
fmt.Println(" \033[32m (\u2713) \033[0m") // (✔)
29+
fmt.Printf(" \033[32m (\u2713) \033[0m\n\n")
2730

2831
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
2932
if _, err := cmd.CombinedOutput(); err != nil {
@@ -40,6 +43,18 @@ func Preflight() (bool, error) {
4043
return true, utils.HighlightError(fmt.Errorf("repository has no remotes set, make sure that at least one remote is set"))
4144
}
4245

46+
url, err := git.GetURL()
47+
if err != nil {
48+
return true, err
49+
}
50+
51+
if strings.HasPrefix(url, "http") {
52+
utils.Error("Found non-ssh upstream url %s, please reclone the repo with SSH and retry.\n", url)
53+
utils.Warn("Please ensure that you have SSH keys set up for Git and that you've added them to your SSH agent.\n")
54+
utils.Warn("You can use `plural crypto ssh-keygen` to create your first SSH keys then upload the public key to your Git provider.\n")
55+
return true, fmt.Errorf("found non-ssh upstream")
56+
}
57+
4358
return true, nil
4459
}
4560

0 commit comments

Comments
 (0)