44 "fmt"
55 "os"
66 "path/filepath"
7+ "regexp"
78 "strings"
89
910 "github.com/AlecAivazis/survey/v2"
@@ -17,19 +18,21 @@ import (
1718 "github.com/pluralsh/plural-cli/pkg/utils/git"
1819
1920 "github.com/mitchellh/go-homedir"
21+ giturls "github.com/whilp/git-urls"
2022)
2123
2224type Context struct {
23- Provider providerapi.Provider
24- Manifest * manifest.ProjectManifest
25- Config * config.Config
26- Cloud bool
27- RepoUrl string
28- StacksIdentity string
29- Delims * delims
30- ImportCluster * string
31- CloudCluster string
32- dir string
25+ Provider providerapi.Provider
26+ Manifest * manifest.ProjectManifest
27+ Config * config.Config
28+ Cloud bool
29+ RepoUrl string
30+ StacksIdentity string
31+ Delims * delims
32+ ImportCluster * string
33+ CloudCluster string
34+ dir string
35+ ignorePreflights bool
3336}
3437
3538type delims struct {
@@ -50,23 +53,27 @@ func (ctx *Context) changeDelims() {
5053 ctx .Delims = & delims {"[[" , "]]" }
5154}
5255
56+ func (ctx * Context ) IgnorePreflights (ignore bool ) {
57+ ctx .ignorePreflights = ignore
58+ }
59+
5360func (ctx * Context ) SetImportCluster (id string ) {
5461 ctx .ImportCluster = lo .ToPtr (id )
5562}
5663
5764func (ctx * Context ) Backfill () error {
5865 context , err := manifest .FetchContext ()
5966 if err != nil {
60- return backfillConsoleContext (ctx .Manifest )
67+ return ctx . backfillConsoleContext (ctx .Manifest )
6168 }
6269
6370 console , ok := context .Configuration ["console" ]
6471 if ! ok {
65- return backfillConsoleContext (ctx .Manifest )
72+ return ctx . backfillConsoleContext (ctx .Manifest )
6673 }
6774
6875 if _ , ok = console ["private_key" ]; ! ok {
69- return backfillConsoleContext (ctx .Manifest )
76+ return ctx . backfillConsoleContext (ctx .Manifest )
7077 }
7178
7279 if v , ok := console ["repo_url" ]; ok {
@@ -103,7 +110,7 @@ func Build(cloud bool) (*Context, error) {
103110 }, nil
104111}
105112
106- func backfillConsoleContext (_ * manifest.ProjectManifest ) error {
113+ func ( context * Context ) backfillConsoleContext (_ * manifest.ProjectManifest ) error {
107114 path := manifest .ContextPath ()
108115 ctx , err := manifest .FetchContext ()
109116 if err != nil {
@@ -115,7 +122,17 @@ func backfillConsoleContext(_ *manifest.ProjectManifest) error {
115122 console = map [string ]interface {}{}
116123 }
117124
118- utils .Highlight ("It looks like you cloned this repo before running plural up, we just need you to generate and give us a deploy key to continue\n " )
125+ utils .Highlight ("It looks like you cloned this repo before running plural up, we just need to ensure authentication is setup correctly to continue\n " )
126+
127+ url , err := git .GetURL ()
128+ if err != nil {
129+ return err
130+ }
131+
132+ if strings .HasPrefix (url , "http" ) {
133+ return fmt .Errorf ("found non-ssh upstream url %s, please reclone the repo with SSH and retry" , url )
134+ }
135+
119136 utils .Highlight ("If you want, you can use `plural crypto ssh-keygen` to generate a keypair to use as a deploy key as well\n \n " )
120137
121138 files , err := filepath .Glob (filepath .Join (os .Getenv ("HOME" ), ".ssh" , "*" ))
@@ -144,17 +161,10 @@ func backfillConsoleContext(_ *manifest.ProjectManifest) error {
144161 return err
145162 }
146163
147- url , err := git .GetURL ()
148- if err != nil {
149- return err
150- }
151-
152- if strings .HasPrefix (url , "http" ) {
153- return fmt .Errorf ("found non-ssh upstream url %s, please reclone the repo with SSH and retry" , url )
154- }
155-
156- if err := verifySSHKey (contents , url ); err != nil {
157- return fmt .Errorf ("ssh key not valid for url %s, error: %w" , url , err )
164+ if ! context .ignorePreflights {
165+ if err := verifySSHKey (contents , url ); err != nil {
166+ return fmt .Errorf ("ssh key not valid for url %s, error: %w. If you want to bypass this check, you can use the --ignore-preflights flag" , url , err )
167+ }
158168 }
159169
160170 console ["repo_url" ] = url
@@ -174,9 +184,30 @@ func verifySSHKey(key, url string) error {
174184 return
175185 }
176186 }(dir )
177- auth , _ := git .SSHAuth ("git" , key , "" )
187+
188+ auth , _ := git .SSHAuth (getGitUsername (url ), key , "" )
178189 if _ , err := git .Clone (auth , url , dir ); err != nil {
179190 return err
180191 }
181192 return nil
182193}
194+
195+ var (
196+ scpSyntax = regexp .MustCompile (`^([a-zA-Z0-9-._~]+@)?([a-zA-Z0-9._-]+):([a-zA-Z0-9./._-]+)(?:\?||$)(.*)$` )
197+ )
198+
199+ func getGitUsername (url string ) string {
200+ match := scpSyntax .FindAllStringSubmatch (url , - 1 )
201+ if len (match ) > 0 {
202+ if match [0 ][1 ] != "" {
203+ return strings .TrimRight (match [0 ][1 ], "@" )
204+ }
205+ }
206+
207+ uname := "git"
208+ parsedUrl , err := giturls .Parse (url )
209+ if err == nil {
210+ uname = parsedUrl .User .Username ()
211+ }
212+ return uname
213+ }
0 commit comments