2021-07-01 21:40:39 +00:00
|
|
|
// Copyright © 2021 Steve Francia <spf@spf13.com>.
|
2015-10-28 16:51:48 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2019-06-07 19:48:51 +00:00
|
|
|
"os"
|
2015-10-28 16:51:48 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2017-05-15 07:49:18 +00:00
|
|
|
var (
|
|
|
|
// Used for flags.
|
2019-01-29 08:46:54 +00:00
|
|
|
cfgFile string
|
|
|
|
userLicense string
|
2017-05-15 07:49:18 +00:00
|
|
|
|
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "cobra",
|
|
|
|
Short: "A generator for Cobra based Applications",
|
|
|
|
Long: `Cobra is a CLI library for Go that empowers applications.
|
2015-11-20 22:28:40 +00:00
|
|
|
This application is a tool to generate the needed files
|
|
|
|
to quickly create a Cobra application.`,
|
2017-05-15 07:49:18 +00:00
|
|
|
}
|
|
|
|
)
|
2015-10-28 16:51:48 +00:00
|
|
|
|
2017-05-09 09:14:33 +00:00
|
|
|
// Execute executes the root command.
|
2019-07-30 06:34:42 +00:00
|
|
|
func Execute() error {
|
|
|
|
return rootCmd.Execute()
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-07-30 07:19:17 +00:00
|
|
|
cobra.OnInitialize(initConfig)
|
2017-05-04 20:28:09 +00:00
|
|
|
|
2017-05-09 09:14:33 +00:00
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
|
2017-05-28 10:20:47 +00:00
|
|
|
rootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
|
2017-05-28 10:42:06 +00:00
|
|
|
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
|
2021-07-01 21:40:39 +00:00
|
|
|
rootCmd.PersistentFlags().Bool("viper", false, "use Viper for configuration")
|
2021-02-08 00:08:50 +00:00
|
|
|
cobra.CheckErr(viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author")))
|
|
|
|
cobra.CheckErr(viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper")))
|
2015-10-28 16:51:48 +00:00
|
|
|
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
2021-07-01 21:45:46 +00:00
|
|
|
viper.SetDefault("license", "none")
|
2017-05-04 20:28:09 +00:00
|
|
|
|
2017-05-09 09:14:33 +00:00
|
|
|
rootCmd.AddCommand(addCmd)
|
2017-05-28 10:42:06 +00:00
|
|
|
rootCmd.AddCommand(initCmd)
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 07:19:17 +00:00
|
|
|
func initConfig() {
|
2017-05-04 20:28:09 +00:00
|
|
|
if cfgFile != "" {
|
|
|
|
// Use config file from the flag.
|
2015-10-28 16:51:48 +00:00
|
|
|
viper.SetConfigFile(cfgFile)
|
2017-04-29 10:02:02 +00:00
|
|
|
} else {
|
2017-05-04 20:28:09 +00:00
|
|
|
// Find home directory.
|
2019-06-07 19:48:51 +00:00
|
|
|
home, err := os.UserHomeDir()
|
2021-02-08 00:08:50 +00:00
|
|
|
cobra.CheckErr(err)
|
2017-05-04 20:28:09 +00:00
|
|
|
|
|
|
|
// Search config in home directory with name ".cobra" (without extension).
|
|
|
|
viper.AddConfigPath(home)
|
2021-06-16 01:52:13 +00:00
|
|
|
viper.SetConfigType("yaml")
|
2017-04-29 10:02:02 +00:00
|
|
|
viper.SetConfigName(".cobra")
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 10:02:02 +00:00
|
|
|
viper.AutomaticEnv()
|
2015-10-28 16:51:48 +00:00
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
2021-12-07 23:00:50 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
2015-10-28 16:51:48 +00:00
|
|
|
}
|
|
|
|
}
|