refactor: move cobra/cmd/* to cobra/

This commit is contained in:
umarcor 2019-03-19 01:32:43 +01:00
parent 6d2dc43606
commit 755e6f0a04
24 changed files with 82 additions and 106 deletions

View file

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package cmd package main
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
package cmd package main
import ( import (
"fmt" "fmt"

View file

@ -1,79 +0,0 @@
// Copyright © 2021 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"
"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 <EMAIL ADDRESS>")
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.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}

View file

@ -1,4 +1,4 @@
package cmd package main
import ( import (
"bytes" "bytes"

View file

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package cmd package main
import ( import (
"os" "os"

View file

@ -1,4 +1,4 @@
package cmd package main
import "testing" import "testing"

View file

@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package cmd package main
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package cmd package main
import ( import (
"fmt" "fmt"
@ -17,7 +17,7 @@ func getProject() *Project {
Legal: getLicense(), Legal: getLicense(),
Copyright: copyrightLine(), Copyright: copyrightLine(),
AppName: "cmd", AppName: "cmd",
PkgName: "github.com/spf13/cobra/cobra/cmd/cmd", PkgName: "github.com/spf13/cobra/cobra/cmd",
Viper: true, Viper: true,
} }
} }

View file

@ -1,4 +1,4 @@
package cmd package main
func initAgpl() { func initAgpl() {
Licenses["agpl"] = License{ Licenses["agpl"] = License{

View file

@ -13,7 +13,7 @@
// Parts inspired by https://github.com/ryanuber/go-license // Parts inspired by https://github.com/ryanuber/go-license
package cmd package main
func initApache2() { func initApache2() {
Licenses["apache"] = License{ Licenses["apache"] = License{

View file

@ -13,7 +13,7 @@
// Parts inspired by https://github.com/ryanuber/go-license // Parts inspired by https://github.com/ryanuber/go-license
package cmd package main
func initBsdClause2() { func initBsdClause2() {
Licenses["freebsd"] = License{ Licenses["freebsd"] = License{

View file

@ -13,7 +13,7 @@
// Parts inspired by https://github.com/ryanuber/go-license // Parts inspired by https://github.com/ryanuber/go-license
package cmd package main
func initBsdClause3() { func initBsdClause3() {
Licenses["bsd"] = License{ Licenses["bsd"] = License{

View file

@ -13,7 +13,7 @@
// Parts inspired by https://github.com/ryanuber/go-license // Parts inspired by https://github.com/ryanuber/go-license
package cmd package main
func initGpl2() { func initGpl2() {
Licenses["gpl2"] = License{ Licenses["gpl2"] = License{

View file

@ -13,7 +13,7 @@
// Parts inspired by https://github.com/ryanuber/go-license // Parts inspired by https://github.com/ryanuber/go-license
package cmd package main
func initGpl3() { func initGpl3() {
Licenses["gpl3"] = License{ Licenses["gpl3"] = License{

View file

@ -1,4 +1,4 @@
package cmd package main
func initLgpl() { func initLgpl() {
Licenses["lgpl"] = License{ Licenses["lgpl"] = License{

View file

@ -13,7 +13,7 @@
// Parts inspired by https://github.com/ryanuber/go-license // Parts inspired by https://github.com/ryanuber/go-license
package cmd package main
func initMit() { func initMit() {
Licenses["mit"] = License{ Licenses["mit"] = License{

View file

@ -13,7 +13,7 @@
// Parts inspired by https://github.com/ryanuber/go-license // Parts inspired by https://github.com/ryanuber/go-license
package cmd package main
import ( import (
"fmt" "fmt"

View file

@ -1,4 +1,4 @@
// Copyright © 2015 Steve Francia <spf@spf13.com>. // Copyright © 2021 Steve Francia <spf@spf13.com>.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -14,13 +14,68 @@
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/spf13/cobra/cobra/cmd" "github.com/spf13/cobra"
"github.com/spf13/viper"
) )
func main() { func main() {
if err := cmd.Execute(); err != nil { err := rootCmd.Execute()
if err != nil {
os.Exit(1) os.Exit(1)
} }
} }
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 <EMAIL ADDRESS>")
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.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}

View file

@ -1,4 +1,4 @@
package cmd package main
import ( import (
"fmt" "fmt"

View file

@ -1,3 +1,3 @@
package cmd package main
/* todo: write tests */ /* todo: write tests */

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
package main package main
import "github.com/spf13/cobra/cobra/cmd/cmd" import "github.com/spf13/cobra/cobra/cmd"
func main() { func main() {
cmd.Execute() cmd.Execute()

View file

@ -27,7 +27,7 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands // rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "cmd", Use: "cobra",
Short: "A brief description of your application", Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example: examples and usage of using your application. For example:
@ -56,7 +56,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // 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 // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
@ -73,10 +73,10 @@ func initConfig() {
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
cobra.CheckErr(err) 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.AddConfigPath(home)
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.SetConfigName(".cmd") viper.SetConfigName(".cobra")
} }
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match