From 31779aed37ba33a0c045307c3289fd77a306212a Mon Sep 17 00:00:00 2001 From: umarcor Date: Tue, 19 Mar 2019 01:32:43 +0100 Subject: [PATCH] refactor: move cobra/cmd/* to cobra/ --- cobra/{cmd => }/add.go | 2 +- cobra/{cmd => }/add_test.go | 2 +- cobra/cmd/root.go | 79 ------------------------- cobra/{cmd => }/golden_test.go | 2 +- cobra/{cmd => }/helpers.go | 2 +- cobra/{cmd => }/helpers_test.go | 2 +- cobra/{cmd => }/init.go | 2 +- cobra/{cmd => }/init_test.go | 4 +- cobra/{cmd => }/license_agpl.go | 2 +- cobra/{cmd => }/license_apache_2.go | 2 +- cobra/{cmd => }/license_bsd_clause_2.go | 2 +- cobra/{cmd => }/license_bsd_clause_3.go | 2 +- cobra/{cmd => }/license_gpl_2.go | 2 +- cobra/{cmd => }/license_gpl_3.go | 2 +- cobra/{cmd => }/license_lgpl.go | 2 +- cobra/{cmd => }/license_mit.go | 2 +- cobra/{cmd => }/licenses.go | 2 +- cobra/main.go | 60 +++++++++++++++++-- cobra/{cmd => }/project.go | 2 +- cobra/{cmd => }/project_test.go | 2 +- cobra/{cmd => }/testdata/LICENSE.golden | 0 cobra/{cmd => }/testdata/main.go.golden | 2 +- cobra/{cmd => }/testdata/root.go.golden | 8 +-- cobra/{cmd => }/testdata/test.go.golden | 0 24 files changed, 80 insertions(+), 107 deletions(-) rename cobra/{cmd => }/add.go (99%) rename cobra/{cmd => }/add_test.go (98%) delete mode 100644 cobra/cmd/root.go rename cobra/{cmd => }/golden_test.go (99%) rename cobra/{cmd => }/helpers.go (99%) rename cobra/{cmd => }/helpers_test.go (87%) rename cobra/{cmd => }/init.go (99%) rename cobra/{cmd => }/init_test.go (96%) rename cobra/{cmd => }/license_agpl.go (99%) rename cobra/{cmd => }/license_apache_2.go (99%) rename cobra/{cmd => }/license_bsd_clause_2.go (99%) rename cobra/{cmd => }/license_bsd_clause_3.go (99%) rename cobra/{cmd => }/license_gpl_2.go (99%) rename cobra/{cmd => }/license_gpl_3.go (99%) rename cobra/{cmd => }/license_lgpl.go (99%) rename cobra/{cmd => }/license_mit.go (99%) rename cobra/{cmd => }/licenses.go (99%) rename cobra/{cmd => }/project.go (99%) rename cobra/{cmd => }/project_test.go (65%) rename cobra/{cmd => }/testdata/LICENSE.golden (100%) rename cobra/{cmd => }/testdata/main.go.golden (93%) rename cobra/{cmd => }/testdata/root.go.golden (93%) rename cobra/{cmd => }/testdata/test.go.golden (100%) 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 8377411edd..4e4aef72d3 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 20bc34f2ec..7c11e9b88a 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 51bba2cc5e..0000000000 --- a/cobra/cmd/root.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright © 2021 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", false, "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", "none") - - 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 99b92e31b7..3248c782e2 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 6a8047e38b..776df2457b 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 c5d20026cf..d96ce4bf1a 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 dbdc25bba3..ce49cce63e 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 ( "encoding/json" diff --git a/cobra/cmd/init_test.go b/cobra/init_test.go similarity index 96% rename from cobra/cmd/init_test.go rename to cobra/init_test.go index 930313c592..3b4a7d7210 100644 --- a/cobra/cmd/init_test.go +++ b/cobra/init_test.go @@ -1,4 +1,4 @@ -package cmd +package main import ( "fmt" @@ -17,7 +17,7 @@ func getProject() *Project { Legal: getLicense(), Copyright: copyrightLine(), AppName: "cmd", - PkgName: "github.com/spf13/cobra/cobra/cmd/cmd", + PkgName: "github.com/spf13/cobra/cobra/cmd", Viper: true, } } 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 bc22e9732b..cf8f660545 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 38393d5417..7df0ed9d80 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 4a847e04a0..5aa92d9f23 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 c7476b31f5..284c821645 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 03e05b3a7e..9dc5ae5799 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 ce07679c77..4308287786 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 0f8b96cad0..f1c1f92a4a 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 bd2d0c4fa8..647b09861c 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 30c7b242dd..2e027aeb32 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 eeaf9824ef..f6b9999903 100644 --- a/cobra/main.go +++ b/cobra/main.go @@ -1,4 +1,4 @@ -// Copyright © 2015 Steve Francia . +// Copyright © 2021 Steve Francia . // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -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", false, "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", "none") + + 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 bd68a31d78..b3c50db173 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 ed5b054a29..1f6cc10f53 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 93% rename from cobra/cmd/testdata/main.go.golden rename to cobra/testdata/main.go.golden index 0af77e17fd..72503a1c74 100644 --- a/cobra/cmd/testdata/main.go.golden +++ b/cobra/testdata/main.go.golden @@ -15,7 +15,7 @@ limitations under the License. */ package main -import "github.com/spf13/cobra/cobra/cmd/cmd" +import "github.com/spf13/cobra/cobra/cmd" func main() { cmd.Execute() diff --git a/cobra/cmd/testdata/root.go.golden b/cobra/testdata/root.go.golden similarity index 93% rename from cobra/cmd/testdata/root.go.golden rename to cobra/testdata/root.go.golden index 2adeaea53a..fda11b61da 100644 --- a/cobra/cmd/testdata/root.go.golden +++ b/cobra/testdata/root.go.golden @@ -27,7 +27,7 @@ var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "cmd", + Use: "cobra", Short: "A brief description of your application", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your application. For example: @@ -53,7 +53,7 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cmd.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") // Cobra also supports local flags, which will only run // when this action is called directly. @@ -70,10 +70,10 @@ func initConfig() { home, err := os.UserHomeDir() cobra.CheckErr(err) - // Search config in home directory with name ".cmd" (without extension). + // Search config in home directory with name ".cobra" (without extension). viper.AddConfigPath(home) viper.SetConfigType("yaml") - viper.SetConfigName(".cmd") + viper.SetConfigName(".cobra") } viper.AutomaticEnv() // read in environment variables that match 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