@@ -33,8 +33,8 @@ import (
3333 "github.com/codefresh-io/cli-v2/pkg/store"
3434 "github.com/codefresh-io/cli-v2/pkg/util"
3535
36- "github.com/argoproj-labs/argocd-autopilot/pkg/git"
37- autoPilotUtil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
36+ apgit "github.com/argoproj-labs/argocd-autopilot/pkg/git"
37+ aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
3838 "github.com/manifoldco/promptui"
3939 "github.com/spf13/cobra"
4040 "github.com/spf13/pflag"
@@ -115,7 +115,7 @@ func askUserIfToInstallDemoResources(cmd *cobra.Command, sampleInstall *bool) er
115115 return nil
116116}
117117
118- func ensureRepo (cmd * cobra.Command , runtimeName string , cloneOpts * git .CloneOptions , fromAPI bool ) error {
118+ func ensureRepo (cmd * cobra.Command , runtimeName string , cloneOpts * apgit .CloneOptions , fromAPI bool ) error {
119119 ctx := cmd .Context ()
120120 if cloneOpts .Repo != "" {
121121 return nil
@@ -151,7 +151,7 @@ func getRepoFromUserInput(cmd *cobra.Command) error {
151151 repoPrompt := promptui.Prompt {
152152 Label : "Repository URL" ,
153153 Validate : func (value string ) error {
154- host , orgRepo , _ , _ , _ , _ , _ := autoPilotUtil .ParseGitUrl (value )
154+ host , orgRepo , _ , _ , _ , _ , _ := aputil .ParseGitUrl (value )
155155 if host != "" && orgRepo != "" {
156156 return nil
157157 }
@@ -279,57 +279,50 @@ func getIngressClassFromUserSelect(ingressClassNames []string) (string, error) {
279279 return result , nil
280280}
281281
282- func inferProviderFromRepo (opts * git.CloneOptions ) {
283- if opts .Provider != "" {
284- return
285- }
286-
287- if strings .Contains (opts .Repo , "github.com" ) {
288- opts .Provider = "github"
289- }
290- if strings .Contains (opts .Repo , "gitlab.com" ) {
291- opts .Provider = "gitlab"
292- }
293- }
294-
295- func ensureGitToken (cmd * cobra.Command , cloneOpts * git.CloneOptions , verify bool ) error {
296- errMessage := "Value stored in environment variable GIT_TOKEN is invalid; enter a valid runtime token"
282+ // ensureGitToken gets the runtime token from the user (if !silent), and verifys it witht he provider (if available)
283+ func ensureGitToken (cmd * cobra.Command , gitProvider cfgit.Provider , cloneOpts * apgit.CloneOptions ) error {
284+ ctx := cmd .Context ()
285+ errMessage := "Value stored in environment variable GIT_TOKEN is invalid; enter a valid runtime token: %w"
297286 if cloneOpts .Auth .Password == "" && ! store .Get ().Silent {
298287 err := getGitTokenFromUserInput (cmd )
299- errMessage = "Invalid runtime token; enter a valid token"
288+ errMessage = "Invalid runtime token; enter a valid token: %w "
300289 if err != nil {
301290 return err
302291 }
303292 }
304293
305- if verify {
306- err := cfgit .VerifyToken (cmd . Context (), cloneOpts . Provider , cloneOpts .Auth .Password , cfgit . RuntimeToken )
294+ if gitProvider != nil {
295+ err := gitProvider .VerifyToken (ctx , cfgit . RuntimeToken , cloneOpts .Auth .Password )
307296 if err != nil {
308297 // in case when we get invalid value from env variable TOKEN we clean
309298 cloneOpts .Auth .Password = ""
310- return fmt .Errorf (errMessage )
299+ return fmt .Errorf (errMessage , err )
311300 }
312- }
313-
314- if cloneOpts .Auth .Password == "" {
301+ } else if cloneOpts .Auth .Password == "" {
315302 return fmt .Errorf ("must provide a git token using --git-token" )
316303 }
317304
318305 return nil
319306}
320307
321- func ensureGitPAT (cmd * cobra.Command , opts * RuntimeInstallOptions ) error {
308+ // ensureGitPAT verifys the user's Personal Access Token (if it is different from the Runtime Token)
309+ func ensureGitPAT (ctx context.Context , opts * RuntimeInstallOptions ) error {
322310 if opts .GitIntegrationRegistrationOpts .Token == "" {
323311 opts .GitIntegrationRegistrationOpts .Token = opts .InsCloneOpts .Auth .Password
324- currentUser , err := cfConfig .NewClient ().Users ().GetCurrent (cmd . Context () )
312+ currentUser , err := cfConfig .NewClient ().Users ().GetCurrent (ctx )
325313 if err != nil {
326314 return fmt .Errorf ("failed to retrieve username from platform: %w" , err )
327315 }
328316
329- log .G (cmd .Context ()).Infof ("Personal git token was not provided. Using runtime git token to register user: \" %s\" . You may replace your personal git token at any time from the UI in the user settings" , currentUser .Name )
317+ log .G (ctx ).Infof ("Personal git token was not provided. Using runtime git token to register user: \" %s\" . You may replace your personal git token at any time from the UI in the user settings" , currentUser .Name )
318+ return nil
319+ }
320+
321+ if opts .gitProvider != nil {
322+ return opts .gitProvider .VerifyToken (ctx , cfgit .PersonalToken , opts .InsCloneOpts .Auth .Password )
330323 }
331324
332- return cfgit . VerifyToken ( cmd . Context (), opts . InsCloneOpts . Provider , opts . GitIntegrationRegistrationOpts . Token , cfgit . PersonalToken )
325+ return nil
333326}
334327
335328func getGitTokenFromUserInput (cmd * cobra.Command ) error {
0 commit comments