2015-10-28 16:51:48 +00:00
|
|
|
// Copyright © 2015 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// 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"
|
2017-08-07 01:10:25 +00:00
|
|
|
"os"
|
2015-10-28 16:51:48 +00:00
|
|
|
|
2017-08-07 01:10:25 +00:00
|
|
|
lol "github.com/kris-nova/lolgopher"
|
2017-05-04 20:28:09 +00:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
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.
|
2017-05-28 10:20:47 +00:00
|
|
|
cfgFile, userLicense string
|
2017-08-07 01:10:25 +00:00
|
|
|
fab bool
|
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-08-07 01:10:25 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
if fab {
|
|
|
|
command.SetOutput(&lol.Writer{Output: os.Stdout, ColorMode: lol.ColorModeTrueColor})
|
|
|
|
}
|
|
|
|
command.Help()
|
|
|
|
},
|
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.
|
2015-10-28 16:51:48 +00:00
|
|
|
func Execute() {
|
2017-05-28 10:23:49 +00:00
|
|
|
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")
|
2017-08-07 01:10:25 +00:00
|
|
|
rootCmd.PersistentFlags().BoolVarP(&fab, "fab", "f", false, "enable fabulous encoding to STDOUT")
|
2017-05-28 10:20:47 +00:00
|
|
|
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
|
2017-05-09 09:14:33 +00:00
|
|
|
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
|
|
|
|
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
2015-10-28 16:51:48 +00:00
|
|
|
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
|
|
|
viper.SetDefault("license", "apache")
|
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)
|
2017-08-07 01:10:25 +00:00
|
|
|
|
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.
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
2017-05-14 10:03:16 +00:00
|
|
|
er(err)
|
2017-05-04 20:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search config in home directory with name ".cobra" (without extension).
|
|
|
|
viper.AddConfigPath(home)
|
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 {
|
|
|
|
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
|
|
}
|
|
|
|
}
|