cmd: Add possibility to specify the target package

Fix #455
This commit is contained in:
Albert Nigmatzianov 2017-05-28 12:42:06 +02:00
parent 84f471618b
commit 99ff9334bd
2 changed files with 14 additions and 8 deletions

View File

@ -23,10 +23,11 @@ import (
)
func init() {
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")
addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")
}
var parentName string
var packageName, parentName string
var addCmd = &cobra.Command{
Use: "add [command name]",
@ -45,11 +46,17 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
if len(args) < 1 {
er("add needs a name for the command")
}
wd, err := os.Getwd()
if err != nil {
er(err)
var project *Project
if packageName != "" {
project = NewProject(packageName)
} else {
wd, err := os.Getwd()
if err != nil {
er(err)
}
project = NewProjectFromPath(wd)
}
project := NewProjectFromPath(wd)
cmdName := validateCmdName(args[0])
cmdPath := filepath.Join(project.CmdPath(), cmdName+".go")

View File

@ -44,16 +44,15 @@ func init() {
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 (can provide `license` in config)")
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache")
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(initCmd)
}
func initViper() {