-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option flag controlled by viper
- Loading branch information
Showing
647 changed files
with
104,477 additions
and
2,561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
/* | ||
Copyright © 2024 NAME HERE <EMAIL ADDRESS> | ||
*/ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// helloCmd represents the hello command | ||
var helloCmd = &cobra.Command{ | ||
Use: "hello", | ||
Short: "A hello world command.", | ||
Long: `A hello world command that prints out {"hello": "world"}`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Fprintf(cmd.OutOrStdout(), "{\"hello\": \"world\"}") | ||
}, | ||
} | ||
func newHelloCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "hello", | ||
Short: "A hello world command.", | ||
Long: `A hello world command that prints out {"hello": "world"}`, | ||
RunE: runHello, | ||
} | ||
|
||
// bind command-specific flags | ||
cmd.Flags().BoolP("informal", "i", false, "Make the greeting less formal") | ||
_ = viper.BindPFlag("informal", cmd.Flags().Lookup("informal")) | ||
|
||
func init() { | ||
rootCmd.AddCommand(helloCmd) | ||
return cmd | ||
} | ||
|
||
// Here you will define your flags and configuration settings. | ||
func runHello(cmd *cobra.Command, args []string) error { | ||
out := `{"hello": "world"}` | ||
if viper.GetBool("informal") { | ||
out = `{"hi": "world"}` | ||
} | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// helloCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
fmt.Fprintf(cmd.OutOrStdout(), out+"\n") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// helloCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.