-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sc-235696/add viper #8
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can keep the command-specific stuff in the constructor so we don't need to do this all in the root command. |
||
|
||
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're going to want this pattern to eventually pass in dependencies. I'd like to move away from a global
viper
variable since it's implicit state, but it's easier to leave it as-is for now.