@@ -17,7 +17,6 @@ import (
1717
1818var accountTypes = []string {"DeepSource (deepsource.com)" , "Enterprise Server" }
1919
20- // LoginOptions hold the metadata related to login operation
2120type LoginOptions struct {
2221 AuthTimedOut bool
2322 TokenExpired bool
@@ -28,12 +27,10 @@ type LoginOptions struct {
2827 deps * cmddeps.Deps
2928}
3029
31- // NewCmdLogin handles the login functionality for the CLI
3230func NewCmdLogin () * cobra.Command {
3331 return NewCmdLoginWithDeps (nil )
3432}
3533
36- // NewCmdLoginWithDeps creates the login command with injectable dependencies.
3734func NewCmdLoginWithDeps (deps * cmddeps.Deps ) * cobra.Command {
3835 doc := heredoc .Docf (`
3936 Log in to DeepSource using the CLI.
@@ -66,7 +63,6 @@ func NewCmdLoginWithDeps(deps *cmddeps.Deps) *cobra.Command {
6663 },
6764 }
6865
69- // --host flag (--hostname kept as deprecated alias)
7066 cmd .Flags ().StringVar (& opts .HostName , "host" , "" , "Authenticate with a specific DeepSource instance" )
7167 cmd .Flags ().StringVar (& opts .HostName , "hostname" , "" , "Authenticate with a specific DeepSource instance" )
7268 _ = cmd .Flags ().MarkDeprecated ("hostname" , "use --host instead" )
@@ -76,7 +72,6 @@ func NewCmdLoginWithDeps(deps *cmddeps.Deps) *cobra.Command {
7672 return cmd
7773}
7874
79- // Run executes the auth command and starts the login flow if not already authenticated
8075func (opts * LoginOptions ) Run () (err error ) {
8176 var cfgMgr * config.Manager
8277 if opts .deps != nil && opts .deps .ConfigMgr != nil {
@@ -92,51 +87,33 @@ func (opts *LoginOptions) Run() (err error) {
9287 } else {
9388 svc = authsvc .NewService (cfgMgr )
9489 }
95- // Fetch config (errors are non-fatal: a zero config just means "not logged in")
9690 cfg , err := svc .LoadConfig ()
9791 if err != nil {
98- cfg = & config.CLIConfig {}
92+ cfg = config .NewDefault ()
9993 }
10094 opts .User = cfg .User
10195 opts .TokenExpired = cfg .IsExpired ()
10296
103- // Default host so that verifyTokenWithServer can reach the server
104- // even when no config file exists yet.
105- if cfg .Host == "" {
106- cfg .Host = config .DefaultHostName
107- }
97+ opts .verifyTokenWithServer (cfg , svc )
10898
109- // If local says valid, verify against the server
110- opts .verifyTokenWithServer (cfg , cfgMgr )
111-
112- // Login using the interactive mode
11399 if opts .Interactive {
114100 err = opts .handleInteractiveLogin ()
115101 if err != nil {
116102 return err
117103 }
118104 }
119105
120- // Checking if the user passed a hostname. If yes, storing it in the config
121- // Else using the default hostname (deepsource.com)
122106 if opts .HostName != "" {
123107 cfg .Host = opts .HostName
124108 } else {
125109 cfg .Host = config .DefaultHostName
126110 }
127111
128- // If PAT is passed, start the login flow through PAT (skip interactive prompts)
129112 if opts .PAT != "" {
130113 return opts .startPATLoginFlow (svc , cfg , opts .PAT )
131114 }
132115
133- // Before starting the login workflow, check here for two conditions:
134- // Condition 1 : If the token has expired, display a message about it and re-authenticate user
135- // Condition 2 : If the token has not expired,does the user want to re-authenticate?
136-
137- // Checking for condition 1
138116 if ! opts .TokenExpired {
139- // The user is already logged in, confirm re-authentication.
140117 var msg string
141118 if opts .User != "" {
142119 msg = fmt .Sprintf ("You're already logged into DeepSource as %s. Do you want to re-authenticate?" , opts .User )
@@ -147,31 +124,19 @@ func (opts *LoginOptions) Run() (err error) {
147124 if err != nil {
148125 return fmt .Errorf ("Error in fetching response. Please try again." )
149126 }
150- // If the response is No, it implies that the user doesn't want to re-authenticate
151- // In this case, just exit
152127 if ! response {
153128 return nil
154129 }
155130 }
156131
157- // Condition 2
158- // `startLoginFlow` implements the authentication flow for the CLI
159132 return opts .startLoginFlow (svc , cfg )
160133}
161134
162- func (opts * LoginOptions ) verifyTokenWithServer (cfg * config.CLIConfig , cfgMgr * config.Manager ) {
163- if opts .TokenExpired || cfg .Token == "" || cfg .Host == "" {
164- return
165- }
166- client , err := deepsource .New (deepsource.ClientOpts {
167- Token : cfg .Token ,
168- HostName : cfg .Host ,
169- OnTokenRefreshed : cfgMgr .TokenRefreshCallback (),
170- })
171- if err != nil {
135+ func (opts * LoginOptions ) verifyTokenWithServer (cfg * config.CLIConfig , svc * authsvc.Service ) {
136+ if opts .TokenExpired || cfg .Token == "" {
172137 return
173138 }
174- viewer , err := client .GetViewer (context .Background ())
139+ viewer , err := svc .GetViewer (context .Background (), cfg )
175140 if err != nil {
176141 opts .TokenExpired = true
177142 return
@@ -180,23 +145,20 @@ func (opts *LoginOptions) verifyTokenWithServer(cfg *config.CLIConfig, cfgMgr *c
180145 if cfg .User == "" && viewer .Email != "" {
181146 cfg .User = viewer .Email
182147 opts .User = viewer .Email
183- _ = cfgMgr . Write (cfg )
148+ _ = svc . SaveConfig (cfg )
184149 }
185150}
186151
187152func (opts * LoginOptions ) handleInteractiveLogin () error {
188- // Prompt messages and help texts
189153 loginPromptMessage := "Which account do you want to login into?"
190154 loginPromptHelpText := "Select the type of account you want to authenticate"
191155 hostPromptMessage := "Please enter the hostname:"
192156 hostPromptHelpText := "The hostname of the DeepSource instance to authenticate with"
193157
194- // Display prompt to user
195158 loginType , err := prompt .SelectFromOptions (loginPromptMessage , loginPromptHelpText , accountTypes )
196159 if err != nil {
197160 return err
198161 }
199- // Prompt the user for hostname only in the case of on-premise
200162 if loginType == "Enterprise Server" {
201163 opts .HostName , err = prompt .GetSingleLineInput (hostPromptMessage , hostPromptHelpText )
202164 if err != nil {
0 commit comments