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
Provenance
- Commit
447f182- Tree
185aec3- Author
Joshua Harshman <joshgreyhat@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
0d9d2d4
3 files changed+92 added−99 removed
modified:cobra/cmd/testdata/main.go.golden+1 added-1 removed
16 unmodified lines17 18 import "github.com/spf13/testproject/cmd"19 20 func main() {21- cmd.Execute()21+ cmd.Execute()22 }modified:cobra/cmd/testdata/root.go.golden+42 added-48 removed
14 unmodified lines15 */16 package cmd17 18 import (19- "fmt"20- "os"21- "github.com/spf13/cobra"19+ "fmt"20+ "github.com/spf13/cobra"21+ "os"22 23- homedir "github.com/mitchellh/go-homedir"24- "github.com/spf13/viper"25-23+ homedir "github.com/mitchellh/go-homedir"24+ "github.com/spf13/viper"25 )26 28-27 var cfgFile string28 31-29 // rootCmd represents the base command when called without any subcommands30 var 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 contains34 examples and usage of using your application. For example:35 36 Cobra is a CLI library for Go that empowers applications.37 This application is a tool to generate the needed files38 to 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 }43 44 // 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.46 func 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 }52 53 func init() {57- cobra.OnInitialize(initConfig)54+ cobra.OnInitialize(initConfig)55 59- // 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.59 63- 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)")61 65-66- // 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 }66 71-67 // initConfig reads in config file and ENV variables if set.68 func 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+ }79 85- // 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+ }84 90- viper.AutomaticEnv() // read in environment variables that match85+ viper.AutomaticEnv() // read in environment variables that match86 92- // 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 lines9 10 import "{{ .PkgName }}/cmd"11 12 func main() {13- cmd.Execute()13+ cmd.Execute()14 }15 `)16 }17 4 unmodified lines22 */23 package cmd24 25 import (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 )34 35-{{ if .Viper }}35+{{ if .Viper -}}36 var cfgFile string37-{{ end }}37+{{- end }}38 39 // rootCmd represents the base command when called without any subcommands40 var 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 contains44 examples and usage of using your application. For example:45 46 Cobra is a CLI library for Go that empowers applications.47 This application is a tool to generate the needed files48 to 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 }53 54 // 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.56 func 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 }62 63 func 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 }}75-76- // 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 }79 81-{{ if .Viper }}80+{{ if .Viper -}}81 // initConfig reads in config file and ENV variables if set.82 func 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+ }93 95- // 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+ }98 100- viper.AutomaticEnv() // read in environment variables that match99+ viper.AutomaticEnv() // read in environment variables that match100 102- // 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 }109 110 func AddCommandTemplate() []byte {42 unmodified lines