Skip to content
Myr

format tpl/main.go templates (#980)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Joshua Harshman · committed by GitHub · parent 0d9d2d4 · Browse files · 3 files+92 added−99 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+92 added−99 removed

  • other:+92 added−99 removed

3 files+92 added−99 removed

modified:cobra/cmd/testdata/main.go.golden+1 added-1 removed
16 unmodified lines
1718import "github.com/spf13/testproject/cmd"1920func main() {21 cmd.Execute()21 cmd.Execute()22}
modified:cobra/cmd/testdata/root.go.golden+42 added-48 removed
14 unmodified lines
15*/16package cmd1718import (19 "fmt"20 "os"21 "github.com/spf13/cobra"19 "fmt"20 "github.com/spf13/cobra"21 "os"2223 homedir "github.com/mitchellh/go-homedir"24 "github.com/spf13/viper"2523 homedir "github.com/mitchellh/go-homedir"24 "github.com/spf13/viper"25)262827var cfgFile string283129// rootCmd represents the base command when called without any subcommands30var rootCmd = &cobra.Command{34 Use: "testproject",35 Short: "A brief description of your application",36 Long: `A longer description that spans multiple lines and likely contains31 Use: "testproject",32 Short: "A brief description of your application",33 Long: `A longer description that spans multiple lines and likely contains34examples and usage of using your application. For example:3536Cobra is a CLI library for Go that empowers applications.37This application is a tool to generate the needed files38to quickly create a Cobra application.`,42 // Uncomment the following line if your bare application43 // has an action associated with it:44 // Run: func(cmd *cobra.Command, args []string) { },39 // Uncomment the following line if your bare application40 // has an action associated with it:41 // Run: func(cmd *cobra.Command, args []string) { },42}4344// Execute adds all child commands to the root command and sets flags appropriately.45// This is called by main.main(). It only needs to happen once to the rootCmd.46func Execute() {50 if err := rootCmd.Execute(); err != nil {51 fmt.Println(err)52 os.Exit(1)53 }47 if err := rootCmd.Execute(); err != nil {48 fmt.Println(err)49 os.Exit(1)50 }51}5253func init() {57 cobra.OnInitialize(initConfig)54 cobra.OnInitialize(initConfig)5559 // Here you will define your flags and configuration settings.60 // Cobra supports persistent flags, which, if defined here,61 // will be global for your application.56 // Here you will define your flags and configuration settings.57 // Cobra supports persistent flags, which, if defined here,58 // will be global for your application.5963 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")60 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")616566 // Cobra also supports local flags, which will only run67 // when this action is called directly.68 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")62 // Cobra also supports local flags, which will only run63 // when this action is called directly.64 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")65}667167// initConfig reads in config file and ENV variables if set.68func initConfig() {74 if cfgFile != "" {75 // Use config file from the flag.76 viper.SetConfigFile(cfgFile)77 } else {78 // Find home directory.79 home, err := homedir.Dir()80 if err != nil {81 fmt.Println(err)82 os.Exit(1)83 }69 if cfgFile != "" {70 // Use config file from the flag.71 viper.SetConfigFile(cfgFile)72 } else {73 // Find home directory.74 home, err := homedir.Dir()75 if err != nil {76 fmt.Println(err)77 os.Exit(1)78 }7985 // Search config in home directory with name ".testproject" (without extension).86 viper.AddConfigPath(home)87 viper.SetConfigName(".testproject")88 }80 // Search config in home directory with name ".testproject" (without extension).81 viper.AddConfigPath(home)82 viper.SetConfigName(".testproject")83 }8490 viper.AutomaticEnv() // read in environment variables that match85 viper.AutomaticEnv() // read in environment variables that match8692 // If a config file is found, read it in.93 if err := viper.ReadInConfig(); err == nil {94 fmt.Println("Using config file:", viper.ConfigFileUsed())95 }87 // If a config file is found, read it in.88 if err := viper.ReadInConfig(); err == nil {89 fmt.Println("Using config file:", viper.ConfigFileUsed())90 }91}97
modified:cobra/tpl/main.go+49 added-50 removed
8 unmodified lines
910import "{{ .PkgName }}/cmd"1112func main() {13 cmd.Execute()13 cmd.Execute()14}15`)16}17
4 unmodified lines
22*/23package cmd2425import (26 "fmt"27 "os"28 "github.com/spf13/cobra"26 "fmt"27 "github.com/spf13/cobra"28 "os"29{{ if .Viper }}30 homedir "github.com/mitchellh/go-homedir"31 "github.com/spf13/viper"32{{ end }}30 homedir "github.com/mitchellh/go-homedir"31 "github.com/spf13/viper"32{{ end -}}33)3435{{ if .Viper }}35{{ if .Viper -}}36var cfgFile string37{{ end }}37{{- end }}3839// rootCmd represents the base command when called without any subcommands40var rootCmd = &cobra.Command{41 Use: "{{ .AppName }}",42 Short: "A brief description of your application",43 Long: ` + "`" + `A longer description that spans multiple lines and likely contains41 Use: "{{ .AppName }}",42 Short: "A brief description of your application",43 Long: ` + "`" + `A longer description that spans multiple lines and likely contains44examples and usage of using your application. For example:4546Cobra is a CLI library for Go that empowers applications.47This application is a tool to generate the needed files48to quickly create a Cobra application.` + "`" + `,49 // Uncomment the following line if your bare application50 // has an action associated with it:51 // Run: func(cmd *cobra.Command, args []string) { },49 // Uncomment the following line if your bare application50 // has an action associated with it:51 // Run: func(cmd *cobra.Command, args []string) { },52}5354// Execute adds all child commands to the root command and sets flags appropriately.55// This is called by main.main(). It only needs to happen once to the rootCmd.56func Execute() {57 if err := rootCmd.Execute(); err != nil {58 fmt.Println(err)59 os.Exit(1)60 }57 if err := rootCmd.Execute(); err != nil {58 fmt.Println(err)59 os.Exit(1)60 }61}6263func init() {64{{- if .Viper }}65 cobra.OnInitialize(initConfig)65 cobra.OnInitialize(initConfig)66{{ end }}67 // Here you will define your flags and configuration settings.68 // Cobra supports persistent flags, which, if defined here,69 // will be global for your application.67 // Here you will define your flags and configuration settings.68 // Cobra supports persistent flags, which, if defined here,69 // will be global for your application.70{{ if .Viper }}71 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .AppName }}.yaml)")71 rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .AppName }}.yaml)")72{{ else }}73 // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .AppName }}.yaml)")73 // rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .AppName }}.yaml)")74{{ end }}7576 // Cobra also supports local flags, which will only run77 // when this action is called directly.78 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")75 // Cobra also supports local flags, which will only run76 // when this action is called directly.77 rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")78}7981{{ if .Viper }}80{{ if .Viper -}}81// initConfig reads in config file and ENV variables if set.82func initConfig() {84 if cfgFile != "" {85 // Use config file from the flag.86 viper.SetConfigFile(cfgFile)87 } else {88 // Find home directory.89 home, err := homedir.Dir()90 if err != nil {91 fmt.Println(err)92 os.Exit(1)93 }83 if cfgFile != "" {84 // Use config file from the flag.85 viper.SetConfigFile(cfgFile)86 } else {87 // Find home directory.88 home, err := homedir.Dir()89 if err != nil {90 fmt.Println(err)91 os.Exit(1)92 }9395 // Search config in home directory with name ".{{ .AppName }}" (without extension).96 viper.AddConfigPath(home)97 viper.SetConfigName(".{{ .AppName }}")98 }94 // Search config in home directory with name ".{{ .AppName }}" (without extension).95 viper.AddConfigPath(home)96 viper.SetConfigName(".{{ .AppName }}")97 }98100 viper.AutomaticEnv() // read in environment variables that match99 viper.AutomaticEnv() // read in environment variables that match100102 // If a config file is found, read it in.103 if err := viper.ReadInConfig(); err == nil {104 fmt.Println("Using config file:", viper.ConfigFileUsed())105 }101 // If a config file is found, read it in.102 if err := viper.ReadInConfig(); err == nil {103 fmt.Println("Using config file:", viper.ConfigFileUsed())104 }105}107{{ end }}106{{- end }}107`)108}109110func AddCommandTemplate() []byte {
42 unmodified lines