Skip to content

Commit 6e021d2

Browse files
authored
chore: standardise outputs across commands (#66)
fixes #65
1 parent 9124c85 commit 6e021d2

File tree

5 files changed

+337
-229
lines changed

5 files changed

+337
-229
lines changed

cmd/profile.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package cmd
1717

1818
import (
19+
"encoding/json"
1920
"errors"
2021
"fmt"
2122
"net/url"
@@ -54,6 +55,29 @@ func (item *ProfileListItem) Render(highlight bool) string {
5455
return ItemOuter.Render(render)
5556
}
5657

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+
5781
var AddProfileCmd = &cobra.Command{
5882
Use: "add profile-name url <username?> <password?>",
5983
Example: " pb profile add local_parseable http://0.0.0.0:8000 admin admin",
@@ -122,6 +146,11 @@ var AddProfileCmd = &cobra.Command{
122146
}
123147
fmt.Printf("Added profile %s\n", StyleBold.Render(name))
124148

149+
if outputFormat == "json" {
150+
return outputResult(profile)
151+
}
152+
fmt.Printf("Profile %s added successfully\n", name)
153+
125154
return nil
126155
},
127156
}
@@ -145,10 +174,10 @@ var RemoveProfileCmd = &cobra.Command{
145174
if len(fileConfig.Profiles) == 0 {
146175
fileConfig.DefaultProfile = ""
147176
}
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))
152181
}
153182
fmt.Printf("Deleted profile %s\n", StyleBold.Render(name))
154183
} else {
@@ -200,6 +229,9 @@ var DefaultProfileCmd = &cobra.Command{
200229
}
201230

202231
config.WriteConfigToFile(fileConfig)
232+
if outputFormat == "json" {
233+
return outputResult(fmt.Sprintf("%s is now set as default profile", name))
234+
}
203235
fmt.Printf("%s is now set as default profile\n", StyleBold.Render(name))
204236
return nil
205237
},
@@ -219,6 +251,10 @@ var ListProfileCmd = &cobra.Command{
219251
println()
220252
}
221253

254+
if outputFormat == "json" {
255+
return outputResult(fileConfig.Profiles)
256+
}
257+
222258
row := 0
223259
for key, value := range fileConfig.Profiles {
224260
item := ProfileListItem{key, value.URL, value.Username}

0 commit comments

Comments
 (0)