16
16
package cmd
17
17
18
18
import (
19
+ "encoding/json"
19
20
"errors"
20
21
"fmt"
21
22
"net/url"
@@ -54,6 +55,29 @@ func (item *ProfileListItem) Render(highlight bool) string {
54
55
return ItemOuter .Render (render )
55
56
}
56
57
58
+ // Add an output flag to specify the output format.
59
+ var outputFormat string
60
+
61
+ // Initialize flags
62
+ 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)" )
67
+ }
68
+ func outputResult (v interface {}) error {
69
+ if outputFormat == "json" {
70
+ jsonData , err := json .MarshalIndent (v , "" , " " )
71
+ if err != nil {
72
+ return err
73
+ }
74
+ fmt .Println (string (jsonData ))
75
+ } else {
76
+ fmt .Println (v )
77
+ }
78
+ return nil
79
+ }
80
+
57
81
var AddProfileCmd = & cobra.Command {
58
82
Use : "add profile-name url <username?> <password?>" ,
59
83
Example : " pb profile add local_parseable http://0.0.0.0:8000 admin admin" ,
@@ -122,6 +146,11 @@ var AddProfileCmd = &cobra.Command{
122
146
}
123
147
fmt .Printf ("Added profile %s\n " , StyleBold .Render (name ))
124
148
149
+ if outputFormat == "json" {
150
+ return outputResult (profile )
151
+ }
152
+ fmt .Printf ("Profile %s added successfully\n " , name )
153
+
125
154
return nil
126
155
},
127
156
}
@@ -145,10 +174,10 @@ var RemoveProfileCmd = &cobra.Command{
145
174
if len (fileConfig .Profiles ) == 0 {
146
175
fileConfig .DefaultProfile = ""
147
176
}
148
- err = config . WriteConfigToFile ( fileConfig )
149
- if err != nil {
150
- fmt . Printf ( "delete profile %s failed \n , err: %v \n " , StyleBold . Render ( name ), err )
151
- return err
177
+
178
+ config . WriteConfigToFile ( fileConfig )
179
+ if outputFormat == "json" {
180
+ return outputResult ( fmt . Sprintf ( "Deleted profile %s" , name ))
152
181
}
153
182
fmt .Printf ("Deleted profile %s\n " , StyleBold .Render (name ))
154
183
} else {
@@ -200,6 +229,9 @@ var DefaultProfileCmd = &cobra.Command{
200
229
}
201
230
202
231
config .WriteConfigToFile (fileConfig )
232
+ if outputFormat == "json" {
233
+ return outputResult (fmt .Sprintf ("%s is now set as default profile" , name ))
234
+ }
203
235
fmt .Printf ("%s is now set as default profile\n " , StyleBold .Render (name ))
204
236
return nil
205
237
},
@@ -219,6 +251,10 @@ var ListProfileCmd = &cobra.Command{
219
251
println ()
220
252
}
221
253
254
+ if outputFormat == "json" {
255
+ return outputResult (fileConfig .Profiles )
256
+ }
257
+
222
258
row := 0
223
259
for key , value := range fileConfig .Profiles {
224
260
item := ProfileListItem {key , value .URL , value .Username }
0 commit comments