@@ -20,14 +20,12 @@ import (
2020 "errors"
2121 "fmt"
2222 "net/url"
23- "os"
2423 "pb/pkg/config"
2524 "pb/pkg/model/credential"
2625 "pb/pkg/model/defaultprofile"
26+ "time"
2727
2828 tea "github.com/charmbracelet/bubbletea"
29- "github.com/charmbracelet/lipgloss"
30- "github.com/muesli/termenv"
3129 "github.com/spf13/cobra"
3230)
3331
@@ -60,11 +58,12 @@ var outputFormat string
6058
6159// Initialize flags
6260func 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)" )
6765}
66+
6867func outputResult (v interface {}) error {
6968 if outputFormat == "json" {
7069 jsonData , err := json .MarshalIndent (v , "" , " " )
@@ -89,68 +88,67 @@ var AddProfileCmd = &cobra.Command{
8988 }
9089 return cobra .MaximumNArgs (4 )(cmd , args )
9190 },
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
9399 name := args [0 ]
94100 url , err := url .Parse (args [1 ])
95101 if err != nil {
96- return err
102+ commandError = fmt .Errorf ("error parsing URL: %s" , err )
103+ cmd .Annotations ["error" ] = commandError .Error ()
104+ return commandError
97105 }
98106
99- var username string
100- var password string
101-
107+ var username , password string
102108 if len (args ) < 4 {
103109 _m , err := tea .NewProgram (credential .New ()).Run ()
104110 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
107114 }
108115 m := _m .(credential.Model )
109-
110116 username , password = m .Values ()
111117 } else {
112118 username = args [2 ]
113119 password = args [3 ]
114120 }
115121
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 }
122123 fileConfig , err := config .ReadConfigFromFile ()
123124 if err != nil {
124- // create new file
125125 newConfig := config.Config {
126- Profiles : map [string ]config.Profile {
127- name : profile ,
128- },
126+ Profiles : map [string ]config.Profile {name : profile },
129127 DefaultProfile : name ,
130128 }
131129 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 )
140140 }
141141
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
146146 }
147- fmt .Printf ("Added profile %s\n " , StyleBold .Render (name ))
148147
149148 if outputFormat == "json" {
150149 return outputResult (profile )
151150 }
152151 fmt .Printf ("Profile %s added successfully\n " , name )
153-
154152 return nil
155153 },
156154}
@@ -161,29 +159,43 @@ var RemoveProfileCmd = &cobra.Command{
161159 Example : " pb profile remove local_parseable" ,
162160 Args : cobra .ExactArgs (1 ),
163161 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+
165168 name := args [0 ]
166169 fileConfig , err := config .ReadConfigFromFile ()
167170 if err != nil {
168- return nil
171+ cmd .Annotations ["error" ] = fmt .Sprintf ("error reading config: %s" , err )
172+ return err
169173 }
170174
171175 _ , 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+ }
177182
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 = ""
185186 }
186187
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 )
187199 return nil
188200 },
189201}
@@ -193,46 +205,54 @@ var DefaultProfileCmd = &cobra.Command{
193205 Args : cobra .MaximumNArgs (1 ),
194206 Short : "Set default profile to use with all commands" ,
195207 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 ()
198213
199214 fileConfig , err := config .ReadConfigFromFile ()
200215 if err != nil {
201- return nil
216+ cmd .Annotations ["error" ] = fmt .Sprintf ("error reading config: %s" , err )
217+ return err
202218 }
203219
220+ var name string
204221 if len (args ) > 0 {
205222 name = args [0 ]
206223 } else {
207224 model := defaultprofile .New (fileConfig .Profiles )
208225 _m , err := tea .NewProgram (model ).Run ()
209226 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
212229 }
213230 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 {
218232 return nil
219233 }
234+ name = m .Choice
220235 }
221236
222237 _ , 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
229250 }
230251
231- config .WriteConfigToFile (fileConfig )
232252 if outputFormat == "json" {
233253 return outputResult (fmt .Sprintf ("%s is now set as default profile" , name ))
234254 }
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 )
236256 return nil
237257 },
238258}
@@ -241,27 +261,34 @@ var ListProfileCmd = &cobra.Command{
241261 Use : "list profiles" ,
242262 Short : "List all added profiles" ,
243263 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 )
248267 }
268+ startTime := time .Now ()
249269
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
252274 }
253275
254276 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
256284 }
257285
258- row := 0
259286 for key , value := range fileConfig .Profiles {
260287 item := ProfileListItem {key , value .URL , value .Username }
261288 fmt .Println (item .Render (fileConfig .DefaultProfile == key ))
262- row ++
263- fmt .Println ()
289+ fmt .Println () // Add a blank line after each profile
264290 }
291+ cmd .Annotations ["executionTime" ] = time .Since (startTime ).String ()
265292 return nil
266293 },
267294}
0 commit comments