diff --git a/cobra/cmd/add.go b/cobra/add.go similarity index 99% rename from cobra/cmd/add.go rename to cobra/add.go index 8377411ed..4e4aef72d 100644 --- a/cobra/cmd/add.go +++ b/cobra/add.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package main import ( "fmt" diff --git a/cobra/cmd/add_test.go b/cobra/add_test.go similarity index 98% rename from cobra/cmd/add_test.go rename to cobra/add_test.go index 0b32ca67e..d9984730c 100644 --- a/cobra/cmd/add_test.go +++ b/cobra/add_test.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "fmt" diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go deleted file mode 100644 index 350df1034..000000000 --- a/cobra/cmd/root.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright © 2015 Steve Francia . -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "fmt" - "os" - - "github.com/spf13/cobra" - "github.com/spf13/viper" -) - -var ( - // Used for flags. - cfgFile string - userLicense string - - rootCmd = &cobra.Command{ - Use: "cobra", - Short: "A generator for Cobra based Applications", - Long: `Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, - } -) - -// Execute executes the root command. -func Execute() error { - return rootCmd.Execute() -} - -func init() { - cobra.OnInitialize(initConfig) - - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") - rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") - rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project") - rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") - cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))) - cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))) - viper.SetDefault("author", "NAME HERE ") - viper.SetDefault("license", "apache") - - rootCmd.AddCommand(addCmd) - rootCmd.AddCommand(initCmd) -} - -func initConfig() { - if cfgFile != "" { - // Use config file from the flag. - viper.SetConfigFile(cfgFile) - } else { - // Find home directory. - home, err := os.UserHomeDir() - cobra.CheckErr(err) - - // Search config in home directory with name ".cobra" (without extension). - viper.AddConfigPath(home) - viper.SetConfigType("yaml") - viper.SetConfigName(".cobra") - } - - viper.AutomaticEnv() - - if err := viper.ReadInConfig(); err == nil { - fmt.Println("Using config file:", viper.ConfigFileUsed()) - } -} diff --git a/cobra/cmd/golden_test.go b/cobra/golden_test.go similarity index 99% rename from cobra/cmd/golden_test.go rename to cobra/golden_test.go index 99b92e31b..3248c782e 100644 --- a/cobra/cmd/golden_test.go +++ b/cobra/golden_test.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "bytes" diff --git a/cobra/cmd/helpers.go b/cobra/helpers.go similarity index 99% rename from cobra/cmd/helpers.go rename to cobra/helpers.go index 6a8047e38..776df2457 100644 --- a/cobra/cmd/helpers.go +++ b/cobra/helpers.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package main import ( "os" diff --git a/cobra/cmd/helpers_test.go b/cobra/helpers_test.go similarity index 87% rename from cobra/cmd/helpers_test.go rename to cobra/helpers_test.go index c5d20026c..d96ce4bf1 100644 --- a/cobra/cmd/helpers_test.go +++ b/cobra/helpers_test.go @@ -1,4 +1,4 @@ -package cmd +package main import "testing" diff --git a/cobra/cmd/init.go b/cobra/init.go similarity index 99% rename from cobra/cmd/init.go rename to cobra/init.go index 8c0e617ab..db90e6f0f 100644 --- a/cobra/cmd/init.go +++ b/cobra/init.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package cmd +package main import ( "fmt" diff --git a/cobra/cmd/init_test.go b/cobra/init_test.go similarity index 99% rename from cobra/cmd/init_test.go rename to cobra/init_test.go index e364f4098..ac90ad847 100644 --- a/cobra/cmd/init_test.go +++ b/cobra/init_test.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "fmt" diff --git a/cobra/cmd/license_agpl.go b/cobra/license_agpl.go similarity index 99% rename from cobra/cmd/license_agpl.go rename to cobra/license_agpl.go index bc22e9732..cf8f66054 100644 --- a/cobra/cmd/license_agpl.go +++ b/cobra/license_agpl.go @@ -1,4 +1,4 @@ -package cmd +package main func initAgpl() { Licenses["agpl"] = License{ diff --git a/cobra/cmd/license_apache_2.go b/cobra/license_apache_2.go similarity index 99% rename from cobra/cmd/license_apache_2.go rename to cobra/license_apache_2.go index 38393d541..7df0ed9d8 100644 --- a/cobra/cmd/license_apache_2.go +++ b/cobra/license_apache_2.go @@ -13,7 +13,7 @@ // Parts inspired by https://github.com/ryanuber/go-license -package cmd +package main func initApache2() { Licenses["apache"] = License{ diff --git a/cobra/cmd/license_bsd_clause_2.go b/cobra/license_bsd_clause_2.go similarity index 99% rename from cobra/cmd/license_bsd_clause_2.go rename to cobra/license_bsd_clause_2.go index 4a847e04a..5aa92d9f2 100644 --- a/cobra/cmd/license_bsd_clause_2.go +++ b/cobra/license_bsd_clause_2.go @@ -13,7 +13,7 @@ // Parts inspired by https://github.com/ryanuber/go-license -package cmd +package main func initBsdClause2() { Licenses["freebsd"] = License{ diff --git a/cobra/cmd/license_bsd_clause_3.go b/cobra/license_bsd_clause_3.go similarity index 99% rename from cobra/cmd/license_bsd_clause_3.go rename to cobra/license_bsd_clause_3.go index c7476b31f..284c82164 100644 --- a/cobra/cmd/license_bsd_clause_3.go +++ b/cobra/license_bsd_clause_3.go @@ -13,7 +13,7 @@ // Parts inspired by https://github.com/ryanuber/go-license -package cmd +package main func initBsdClause3() { Licenses["bsd"] = License{ diff --git a/cobra/cmd/license_gpl_2.go b/cobra/license_gpl_2.go similarity index 99% rename from cobra/cmd/license_gpl_2.go rename to cobra/license_gpl_2.go index 03e05b3a7..9dc5ae579 100644 --- a/cobra/cmd/license_gpl_2.go +++ b/cobra/license_gpl_2.go @@ -13,7 +13,7 @@ // Parts inspired by https://github.com/ryanuber/go-license -package cmd +package main func initGpl2() { Licenses["gpl2"] = License{ diff --git a/cobra/cmd/license_gpl_3.go b/cobra/license_gpl_3.go similarity index 99% rename from cobra/cmd/license_gpl_3.go rename to cobra/license_gpl_3.go index ce07679c7..430828778 100644 --- a/cobra/cmd/license_gpl_3.go +++ b/cobra/license_gpl_3.go @@ -13,7 +13,7 @@ // Parts inspired by https://github.com/ryanuber/go-license -package cmd +package main func initGpl3() { Licenses["gpl3"] = License{ diff --git a/cobra/cmd/license_lgpl.go b/cobra/license_lgpl.go similarity index 99% rename from cobra/cmd/license_lgpl.go rename to cobra/license_lgpl.go index 0f8b96cad..f1c1f92a4 100644 --- a/cobra/cmd/license_lgpl.go +++ b/cobra/license_lgpl.go @@ -1,4 +1,4 @@ -package cmd +package main func initLgpl() { Licenses["lgpl"] = License{ diff --git a/cobra/cmd/license_mit.go b/cobra/license_mit.go similarity index 99% rename from cobra/cmd/license_mit.go rename to cobra/license_mit.go index bd2d0c4fa..647b09861 100644 --- a/cobra/cmd/license_mit.go +++ b/cobra/license_mit.go @@ -13,7 +13,7 @@ // Parts inspired by https://github.com/ryanuber/go-license -package cmd +package main func initMit() { Licenses["mit"] = License{ diff --git a/cobra/cmd/licenses.go b/cobra/licenses.go similarity index 99% rename from cobra/cmd/licenses.go rename to cobra/licenses.go index 2b3a42438..10fbd8ae7 100644 --- a/cobra/cmd/licenses.go +++ b/cobra/licenses.go @@ -13,7 +13,7 @@ // Parts inspired by https://github.com/ryanuber/go-license -package cmd +package main import ( "fmt" diff --git a/cobra/main.go b/cobra/main.go index eeaf9824e..083fc59aa 100644 --- a/cobra/main.go +++ b/cobra/main.go @@ -14,13 +14,65 @@ package main import ( + "fmt" "os" - "github.com/spf13/cobra/cobra/cmd" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) func main() { - if err := cmd.Execute(); err != nil { - os.Exit(1) + cobra.CheckErr(rootCmd.Execute()) +} + +var ( + // Used for flags. + cfgFile string + userLicense string + + rootCmd = &cobra.Command{ + Use: "cobra", + Short: "A generator for Cobra based Applications", + Long: `Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + } +) + +func init() { + cobra.OnInitialize(initConfig) + + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") + rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution") + rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project") + rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration") + cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))) + cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))) + viper.SetDefault("author", "NAME HERE ") + viper.SetDefault("license", "apache") + + rootCmd.AddCommand(addCmd) + rootCmd.AddCommand(initCmd) +} + +func initConfig() { + if cfgFile != "" { + // Use config file from the flag. + viper.SetConfigFile(cfgFile) + } else { + // Find home directory. + home, err := os.UserHomeDir() + cobra.CheckErr(err) + + // Search config in home directory with name ".cobra" (without extension). + viper.AddConfigPath(home) + viper.SetConfigType("yaml") + viper.SetConfigName(".cobra") + } + + viper.AutomaticEnv() + + if err := viper.ReadInConfig(); err == nil { + fmt.Println("Using config file:", viper.ConfigFileUsed()) } } diff --git a/cobra/cmd/project.go b/cobra/project.go similarity index 99% rename from cobra/cmd/project.go rename to cobra/project.go index bd68a31d7..b3c50db17 100644 --- a/cobra/cmd/project.go +++ b/cobra/project.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "fmt" diff --git a/cobra/cmd/project_test.go b/cobra/project_test.go similarity index 65% rename from cobra/cmd/project_test.go rename to cobra/project_test.go index ed5b054a2..1f6cc10f5 100644 --- a/cobra/cmd/project_test.go +++ b/cobra/project_test.go @@ -1,3 +1,3 @@ -package cmd +package main /* todo: write tests */ diff --git a/cobra/cmd/testdata/LICENSE.golden b/cobra/testdata/LICENSE.golden similarity index 100% rename from cobra/cmd/testdata/LICENSE.golden rename to cobra/testdata/LICENSE.golden diff --git a/cobra/cmd/testdata/main.go.golden b/cobra/testdata/main.go.golden similarity index 100% rename from cobra/cmd/testdata/main.go.golden rename to cobra/testdata/main.go.golden diff --git a/cobra/cmd/testdata/root.go.golden b/cobra/testdata/root.go.golden similarity index 100% rename from cobra/cmd/testdata/root.go.golden rename to cobra/testdata/root.go.golden index 32c1529e7..abeac7b1e 100644 --- a/cobra/cmd/testdata/root.go.golden +++ b/cobra/testdata/root.go.golden @@ -18,8 +18,8 @@ package cmd import ( "fmt" "os" - "github.com/spf13/cobra" + "github.com/spf13/cobra" "github.com/spf13/viper" ) diff --git a/cobra/cmd/testdata/test.go.golden b/cobra/testdata/test.go.golden similarity index 100% rename from cobra/cmd/testdata/test.go.golden rename to cobra/testdata/test.go.golden diff --git a/cobra/tpl/main.go b/cobra/tpl/main.go index ed3a98e14..bb074cf6d 100644 --- a/cobra/tpl/main.go +++ b/cobra/tpl/main.go @@ -25,9 +25,9 @@ package cmd import ( "fmt" "os" + "github.com/spf13/cobra" -{{ if .Viper }} - "github.com/spf13/viper"{{ end }} +{{ if .Viper }} "github.com/spf13/viper"{{ end }} ) {{ if .Viper -}}