@@ -37,7 +37,12 @@ func init() {
37
37
}
38
38
39
39
func login () * cobra.Command {
40
- return & cobra.Command {
40
+ var (
41
+ email string
42
+ username string
43
+ password string
44
+ )
45
+ cmd := & cobra.Command {
41
46
Use : "login <url>" ,
42
47
Short : "Authenticate with a Coder deployment" ,
43
48
Args : cobra .ExactArgs (1 ),
@@ -66,71 +71,77 @@ func login() *cobra.Command {
66
71
return xerrors .Errorf ("has initial user: %w" , err )
67
72
}
68
73
if ! hasInitialUser {
69
- if ! isTTY (cmd ) {
70
- return xerrors .New ("the initial user cannot be created in non-interactive mode. use the API" )
71
- }
72
74
_ , _ = fmt .Fprintf (cmd .OutOrStdout (), caret + "Your Coder deployment hasn't been set up!\n " )
73
75
74
- _ , err := cliui .Prompt (cmd , cliui.PromptOptions {
75
- Text : "Would you like to create the first user?" ,
76
- Default : "yes" ,
77
- IsConfirm : true ,
78
- })
79
- if errors .Is (err , cliui .Canceled ) {
80
- return nil
81
- }
82
- if err != nil {
83
- return err
84
- }
85
- currentUser , err := user .Current ()
86
- if err != nil {
87
- return xerrors .Errorf ("get current user: %w" , err )
88
- }
89
- username , err := cliui .Prompt (cmd , cliui.PromptOptions {
90
- Text : "What " + cliui .Styles .Field .Render ("username" ) + " would you like?" ,
91
- Default : currentUser .Username ,
92
- })
93
- if errors .Is (err , cliui .Canceled ) {
94
- return nil
95
- }
96
- if err != nil {
97
- return xerrors .Errorf ("pick username prompt: %w" , err )
98
- }
99
-
100
- email , err := cliui .Prompt (cmd , cliui.PromptOptions {
101
- Text : "What's your " + cliui .Styles .Field .Render ("email" ) + "?" ,
102
- Validate : func (s string ) error {
103
- err := validator .New ().Var (s , "email" )
104
- if err != nil {
105
- return xerrors .New ("That's not a valid email address!" )
106
- }
76
+ if username == "" {
77
+ if ! isTTY (cmd ) {
78
+ return xerrors .New ("the initial user cannot be created in non-interactive mode. use the API" )
79
+ }
80
+ _ , err := cliui .Prompt (cmd , cliui.PromptOptions {
81
+ Text : "Would you like to create the first user?" ,
82
+ Default : "yes" ,
83
+ IsConfirm : true ,
84
+ })
85
+ if errors .Is (err , cliui .Canceled ) {
86
+ return nil
87
+ }
88
+ if err != nil {
107
89
return err
108
- },
109
- })
110
- if err != nil {
111
- return xerrors .Errorf ("specify email prompt: %w" , err )
90
+ }
91
+ currentUser , err := user .Current ()
92
+ if err != nil {
93
+ return xerrors .Errorf ("get current user: %w" , err )
94
+ }
95
+ username , err = cliui .Prompt (cmd , cliui.PromptOptions {
96
+ Text : "What " + cliui .Styles .Field .Render ("username" ) + " would you like?" ,
97
+ Default : currentUser .Username ,
98
+ })
99
+ if errors .Is (err , cliui .Canceled ) {
100
+ return nil
101
+ }
102
+ if err != nil {
103
+ return xerrors .Errorf ("pick username prompt: %w" , err )
104
+ }
112
105
}
113
106
114
- password , err := cliui .Prompt (cmd , cliui.PromptOptions {
115
- Text : "Enter a " + cliui .Styles .Field .Render ("password" ) + ":" ,
116
- Secret : true ,
117
- Validate : cliui .ValidateNotEmpty ,
118
- })
119
- if err != nil {
120
- return xerrors .Errorf ("specify password prompt: %w" , err )
107
+ if email == "" {
108
+ email , err = cliui .Prompt (cmd , cliui.PromptOptions {
109
+ Text : "What's your " + cliui .Styles .Field .Render ("email" ) + "?" ,
110
+ Validate : func (s string ) error {
111
+ err := validator .New ().Var (s , "email" )
112
+ if err != nil {
113
+ return xerrors .New ("That's not a valid email address!" )
114
+ }
115
+ return err
116
+ },
117
+ })
118
+ if err != nil {
119
+ return xerrors .Errorf ("specify email prompt: %w" , err )
120
+ }
121
121
}
122
- _ , err = cliui .Prompt (cmd , cliui.PromptOptions {
123
- Text : "Confirm " + cliui .Styles .Field .Render ("password" ) + ":" ,
124
- Secret : true ,
125
- Validate : func (s string ) error {
126
- if s != password {
127
- return xerrors .Errorf ("Passwords do not match" )
128
- }
129
- return nil
130
- },
131
- })
132
- if err != nil {
133
- return xerrors .Errorf ("confirm password prompt: %w" , err )
122
+
123
+ if password == "" {
124
+ password , err = cliui .Prompt (cmd , cliui.PromptOptions {
125
+ Text : "Enter a " + cliui .Styles .Field .Render ("password" ) + ":" ,
126
+ Secret : true ,
127
+ Validate : cliui .ValidateNotEmpty ,
128
+ })
129
+ if err != nil {
130
+ return xerrors .Errorf ("specify password prompt: %w" , err )
131
+ }
132
+ _ , err = cliui .Prompt (cmd , cliui.PromptOptions {
133
+ Text : "Confirm " + cliui .Styles .Field .Render ("password" ) + ":" ,
134
+ Secret : true ,
135
+ Validate : func (s string ) error {
136
+ if s != password {
137
+ return xerrors .Errorf ("Passwords do not match" )
138
+ }
139
+ return nil
140
+ },
141
+ })
142
+ if err != nil {
143
+ return xerrors .Errorf ("confirm password prompt: %w" , err )
144
+ }
134
145
}
135
146
136
147
_ , err = client .CreateFirstUser (cmd .Context (), codersdk.CreateFirstUserRequest {
@@ -219,6 +230,10 @@ func login() *cobra.Command {
219
230
return nil
220
231
},
221
232
}
233
+ cmd .Flags ().StringVarP (& email , "email" , "e" , "" , "Specifies an email address to authenticate with." )
234
+ cmd .Flags ().StringVarP (& username , "username" , "u" , "" , "Specifies a username to authenticate with." )
235
+ cmd .Flags ().StringVarP (& password , "password" , "p" , "" , "Specifies a password to authenticate with." )
236
+ return cmd
222
237
}
223
238
224
239
// isWSL determines if coder-cli is running within Windows Subsystem for Linux
0 commit comments