Skip to content

Commit e01a128

Browse files
committed
🎨 Add functions to create flags
1 parent 95f3b04 commit e01a128

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

cmd/helper.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import "github.com/spf13/cobra"
44

55
// This file defines a few functions that avoid code repetition for the commands declaration
66

7-
func commandModifiesConfiguration(cmd *cobra.Command) {
7+
func addFlag_commandModifiesConfiguration(cmd *cobra.Command) {
88
cmd.Flags().StringP("config", "c", "", "Path to the configuration database")
99
}
10+
11+
// Set the flags for a command that prints data
12+
//
13+
// It helps specify the output format
14+
func addFlag_commandPrintsData(cmd *cobra.Command) {
15+
cmd.Flags().String("format", "pretty", "Output format (pretty, json, csv, plain)")
16+
cmd.Flags().Bool("json", false, "Output format as JSON")
17+
cmd.Flags().Bool("csv", false, "Output format as CSV")
18+
cmd.Flags().Bool("plain", false, "Output format as plain text")
19+
}

cmd/plugins.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ func init() {
1818
// Because the command modifies the configuration, we add the flag
1919
// so that the user can specify which conf database to use
2020
// rather than using the default one
21-
commandModifiesConfiguration(pluginsCmd)
21+
addFlag_commandModifiesConfiguration(pluginsCmd)
2222
}

cmd/query.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,5 @@ If no query is provided, the command will launch an interactive input.`,
1919

2020
func init() {
2121
rootCmd.AddCommand(queryCmd)
22-
queryCmd.Flags().String("format", "pretty", "Output format (pretty, json, csv, plain)")
23-
queryCmd.Flags().Bool("json", false, "Output format as JSON")
24-
queryCmd.Flags().Bool("csv", false, "Output format as CSV")
25-
queryCmd.Flags().Bool("plain", false, "Output format as plain text")
22+
addFlag_commandPrintsData(queryCmd)
2623
}

0 commit comments

Comments
 (0)