@@ -20,14 +20,12 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"net/url"
23
- "os"
24
23
"pb/pkg/config"
25
24
"pb/pkg/model/credential"
26
25
"pb/pkg/model/defaultprofile"
26
+ "time"
27
27
28
28
tea "github.com/charmbracelet/bubbletea"
29
- "github.com/charmbracelet/lipgloss"
30
- "github.com/muesli/termenv"
31
29
"github.com/spf13/cobra"
32
30
)
33
31
@@ -60,11 +58,12 @@ var outputFormat string
60
58
61
59
// Initialize flags
62
60
func init () {
63
- AddProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "text " , "Output format (text|json)" )
64
- RemoveProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "text " , "Output format (text|json)" )
65
- DefaultProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "text " , "Output format (text|json)" )
66
- ListProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "text " , "Output format (text|json)" )
61
+ AddProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "" , "Output format (text|json)" )
62
+ RemoveProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "" , "Output format (text|json)" )
63
+ DefaultProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "" , "Output format (text|json)" )
64
+ ListProfileCmd .Flags ().StringVarP (& outputFormat , "output" , "o" , "" , "Output format (text|json)" )
67
65
}
66
+
68
67
func outputResult (v interface {}) error {
69
68
if outputFormat == "json" {
70
69
jsonData , err := json .MarshalIndent (v , "" , " " )
@@ -89,68 +88,67 @@ var AddProfileCmd = &cobra.Command{
89
88
}
90
89
return cobra .MaximumNArgs (4 )(cmd , args )
91
90
},
92
- RunE : func (_ * cobra.Command , args []string ) error {
91
+ RunE : func (cmd * cobra.Command , args []string ) error {
92
+ if cmd .Annotations == nil {
93
+ cmd .Annotations = make (map [string ]string )
94
+ }
95
+ startTime := time .Now ()
96
+ var commandError error
97
+
98
+ // Parsing input and handling errors
93
99
name := args [0 ]
94
100
url , err := url .Parse (args [1 ])
95
101
if err != nil {
96
- return err
102
+ commandError = fmt .Errorf ("error parsing URL: %s" , err )
103
+ cmd .Annotations ["error" ] = commandError .Error ()
104
+ return commandError
97
105
}
98
106
99
- var username string
100
- var password string
101
-
107
+ var username , password string
102
108
if len (args ) < 4 {
103
109
_m , err := tea .NewProgram (credential .New ()).Run ()
104
110
if err != nil {
105
- fmt .Printf ("Alas, there's been an error: %v" , err )
106
- os .Exit (1 )
111
+ commandError = fmt .Errorf ("error reading credentials: %s" , err )
112
+ cmd .Annotations ["error" ] = commandError .Error ()
113
+ return commandError
107
114
}
108
115
m := _m .(credential.Model )
109
-
110
116
username , password = m .Values ()
111
117
} else {
112
118
username = args [2 ]
113
119
password = args [3 ]
114
120
}
115
121
116
- profile := config.Profile {
117
- URL : url .String (),
118
- Username : username ,
119
- Password : password ,
120
- }
121
-
122
+ profile := config.Profile {URL : url .String (), Username : username , Password : password }
122
123
fileConfig , err := config .ReadConfigFromFile ()
123
124
if err != nil {
124
- // create new file
125
125
newConfig := config.Config {
126
- Profiles : map [string ]config.Profile {
127
- name : profile ,
128
- },
126
+ Profiles : map [string ]config.Profile {name : profile },
129
127
DefaultProfile : name ,
130
128
}
131
129
err = config .WriteConfigToFile (& newConfig )
132
- return err
133
- }
134
- if fileConfig .Profiles == nil {
135
- fileConfig .Profiles = make (map [string ]config.Profile )
136
- }
137
- fileConfig .Profiles [name ] = profile
138
- if fileConfig .DefaultProfile == "" {
139
- fileConfig .DefaultProfile = name
130
+ commandError = err
131
+ } else {
132
+ if fileConfig .Profiles == nil {
133
+ fileConfig .Profiles = make (map [string ]config.Profile )
134
+ }
135
+ fileConfig .Profiles [name ] = profile
136
+ if fileConfig .DefaultProfile == "" {
137
+ fileConfig .DefaultProfile = name
138
+ }
139
+ commandError = config .WriteConfigToFile (fileConfig )
140
140
}
141
141
142
- err = config . WriteConfigToFile ( fileConfig )
143
- if err != nil {
144
- fmt . Printf ( "add profile %s failed \n , err: %v \n " , StyleBold . Render ( name ), err )
145
- return err
142
+ cmd . Annotations [ "executionTime" ] = time . Since ( startTime ). String ( )
143
+ if commandError != nil {
144
+ cmd . Annotations [ "error" ] = commandError . Error ( )
145
+ return commandError
146
146
}
147
- fmt .Printf ("Added profile %s\n " , StyleBold .Render (name ))
148
147
149
148
if outputFormat == "json" {
150
149
return outputResult (profile )
151
150
}
152
151
fmt .Printf ("Profile %s added successfully\n " , name )
153
-
154
152
return nil
155
153
},
156
154
}
@@ -161,29 +159,43 @@ var RemoveProfileCmd = &cobra.Command{
161
159
Example : " pb profile remove local_parseable" ,
162
160
Args : cobra .ExactArgs (1 ),
163
161
Short : "Delete a profile" ,
164
- RunE : func (_ * cobra.Command , args []string ) error {
162
+ RunE : func (cmd * cobra.Command , args []string ) error {
163
+ if cmd .Annotations == nil {
164
+ cmd .Annotations = make (map [string ]string )
165
+ }
166
+ startTime := time .Now ()
167
+
165
168
name := args [0 ]
166
169
fileConfig , err := config .ReadConfigFromFile ()
167
170
if err != nil {
168
- return nil
171
+ cmd .Annotations ["error" ] = fmt .Sprintf ("error reading config: %s" , err )
172
+ return err
169
173
}
170
174
171
175
_ , exists := fileConfig .Profiles [name ]
172
- if exists {
173
- delete (fileConfig .Profiles , name )
174
- if len (fileConfig .Profiles ) == 0 {
175
- fileConfig .DefaultProfile = ""
176
- }
176
+ if ! exists {
177
+ msg := fmt .Sprintf ("No profile found with the name: %s" , name )
178
+ cmd .Annotations ["error" ] = msg
179
+ fmt .Println (msg )
180
+ return nil
181
+ }
177
182
178
- config .WriteConfigToFile (fileConfig )
179
- if outputFormat == "json" {
180
- return outputResult (fmt .Sprintf ("Deleted profile %s" , name ))
181
- }
182
- fmt .Printf ("Deleted profile %s\n " , StyleBold .Render (name ))
183
- } else {
184
- fmt .Printf ("No profile found with the name: %s" , StyleBold .Render (name ))
183
+ delete (fileConfig .Profiles , name )
184
+ if len (fileConfig .Profiles ) == 0 {
185
+ fileConfig .DefaultProfile = ""
185
186
}
186
187
188
+ commandError := config .WriteConfigToFile (fileConfig )
189
+ cmd .Annotations ["executionTime" ] = time .Since (startTime ).String ()
190
+ if commandError != nil {
191
+ cmd .Annotations ["error" ] = commandError .Error ()
192
+ return commandError
193
+ }
194
+
195
+ if outputFormat == "json" {
196
+ return outputResult (fmt .Sprintf ("Deleted profile %s" , name ))
197
+ }
198
+ fmt .Printf ("Deleted profile %s\n " , name )
187
199
return nil
188
200
},
189
201
}
@@ -193,46 +205,54 @@ var DefaultProfileCmd = &cobra.Command{
193
205
Args : cobra .MaximumNArgs (1 ),
194
206
Short : "Set default profile to use with all commands" ,
195
207
Example : " pb profile default local_parseable" ,
196
- RunE : func (_ * cobra.Command , args []string ) error {
197
- var name string
208
+ RunE : func (cmd * cobra.Command , args []string ) error {
209
+ if cmd .Annotations == nil {
210
+ cmd .Annotations = make (map [string ]string )
211
+ }
212
+ startTime := time .Now ()
198
213
199
214
fileConfig , err := config .ReadConfigFromFile ()
200
215
if err != nil {
201
- return nil
216
+ cmd .Annotations ["error" ] = fmt .Sprintf ("error reading config: %s" , err )
217
+ return err
202
218
}
203
219
220
+ var name string
204
221
if len (args ) > 0 {
205
222
name = args [0 ]
206
223
} else {
207
224
model := defaultprofile .New (fileConfig .Profiles )
208
225
_m , err := tea .NewProgram (model ).Run ()
209
226
if err != nil {
210
- fmt .Printf ( "Alas, there's been an error : %v " , err )
211
- os . Exit ( 1 )
227
+ cmd . Annotations [ "error" ] = fmt .Sprintf ( "error selecting default profile : %s " , err )
228
+ return err
212
229
}
213
230
m := _m .(defaultprofile.Model )
214
- termenv .DefaultOutput ().ClearLines (lipgloss .Height (model .View ()) - 1 )
215
- if m .Success {
216
- name = m .Choice
217
- } else {
231
+ if ! m .Success {
218
232
return nil
219
233
}
234
+ name = m .Choice
220
235
}
221
236
222
237
_ , exists := fileConfig .Profiles [name ]
223
- if exists {
224
- fileConfig .DefaultProfile = name
225
- } else {
226
- name = lipgloss .NewStyle ().Bold (true ).Render (name )
227
- err := fmt .Sprintf ("profile %s does not exist" , StyleBold .Render (name ))
228
- return errors .New (err )
238
+ if ! exists {
239
+ commandError := fmt .Sprintf ("profile %s does not exist" , name )
240
+ cmd .Annotations ["error" ] = commandError
241
+ return errors .New (commandError )
242
+ }
243
+
244
+ fileConfig .DefaultProfile = name
245
+ commandError := config .WriteConfigToFile (fileConfig )
246
+ cmd .Annotations ["executionTime" ] = time .Since (startTime ).String ()
247
+ if commandError != nil {
248
+ cmd .Annotations ["error" ] = commandError .Error ()
249
+ return commandError
229
250
}
230
251
231
- config .WriteConfigToFile (fileConfig )
232
252
if outputFormat == "json" {
233
253
return outputResult (fmt .Sprintf ("%s is now set as default profile" , name ))
234
254
}
235
- fmt .Printf ("%s is now set as default profile\n " , StyleBold . Render ( name ) )
255
+ fmt .Printf ("%s is now set as default profile\n " , name )
236
256
return nil
237
257
},
238
258
}
@@ -241,27 +261,34 @@ var ListProfileCmd = &cobra.Command{
241
261
Use : "list profiles" ,
242
262
Short : "List all added profiles" ,
243
263
Example : " pb profile list" ,
244
- RunE : func (_ * cobra.Command , _ []string ) error {
245
- fileConfig , err := config .ReadConfigFromFile ()
246
- if err != nil {
247
- return nil
264
+ RunE : func (cmd * cobra.Command , _ []string ) error {
265
+ if cmd .Annotations == nil {
266
+ cmd .Annotations = make (map [string ]string )
248
267
}
268
+ startTime := time .Now ()
249
269
250
- if len (fileConfig .Profiles ) != 0 {
251
- println ()
270
+ fileConfig , err := config .ReadConfigFromFile ()
271
+ if err != nil {
272
+ cmd .Annotations ["error" ] = fmt .Sprintf ("error reading config: %s" , err )
273
+ return err
252
274
}
253
275
254
276
if outputFormat == "json" {
255
- return outputResult (fileConfig .Profiles )
277
+ commandError := outputResult (fileConfig .Profiles )
278
+ cmd .Annotations ["executionTime" ] = time .Since (startTime ).String ()
279
+ if commandError != nil {
280
+ cmd .Annotations ["error" ] = commandError .Error ()
281
+ return commandError
282
+ }
283
+ return nil
256
284
}
257
285
258
- row := 0
259
286
for key , value := range fileConfig .Profiles {
260
287
item := ProfileListItem {key , value .URL , value .Username }
261
288
fmt .Println (item .Render (fileConfig .DefaultProfile == key ))
262
- row ++
263
- fmt .Println ()
289
+ fmt .Println () // Add a blank line after each profile
264
290
}
291
+ cmd .Annotations ["executionTime" ] = time .Since (startTime ).String ()
265
292
return nil
266
293
},
267
294
}
0 commit comments