mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
commit
fe83a627d2
14 changed files with 403 additions and 158 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -36,3 +36,4 @@ tags
|
||||||
cobra.test
|
cobra.test
|
||||||
|
|
||||||
.idea
|
.idea
|
||||||
|
vendor
|
||||||
|
|
173
Gopkg.lock
generated
Normal file
173
Gopkg.lock
generated
Normal file
|
@ -0,0 +1,173 @@
|
||||||
|
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
|
||||||
|
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:7cb4fdca4c251b3ef8027c90ea35f70c7b661a593b9eeae34753c65499098bb1"
|
||||||
|
name = "github.com/cpuguy83/go-md2man"
|
||||||
|
packages = ["md2man"]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "20f5889cbdc3c73dbd2862796665e7c465ade7d1"
|
||||||
|
version = "v1.0.8"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:abeb38ade3f32a92943e5be54f55ed6d6e3b6602761d74b4aab4c9dd45c18abd"
|
||||||
|
name = "github.com/fsnotify/fsnotify"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9"
|
||||||
|
version = "v1.4.7"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:c0d19ab64b32ce9fe5cf4ddceba78d5bc9807f0016db6b1183599da3dcc24d10"
|
||||||
|
name = "github.com/hashicorp/hcl"
|
||||||
|
packages = [
|
||||||
|
".",
|
||||||
|
"hcl/ast",
|
||||||
|
"hcl/parser",
|
||||||
|
"hcl/printer",
|
||||||
|
"hcl/scanner",
|
||||||
|
"hcl/strconv",
|
||||||
|
"hcl/token",
|
||||||
|
"json/parser",
|
||||||
|
"json/scanner",
|
||||||
|
"json/token",
|
||||||
|
]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "8cb6e5b959231cc1119e43259c4a608f9c51a241"
|
||||||
|
version = "v1.0.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be"
|
||||||
|
name = "github.com/inconshreveable/mousetrap"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75"
|
||||||
|
version = "v1.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:c568d7727aa262c32bdf8a3f7db83614f7af0ed661474b24588de635c20024c7"
|
||||||
|
name = "github.com/magiconair/properties"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "c2353362d570a7bfa228149c62842019201cfb71"
|
||||||
|
version = "v1.8.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:5d231480e1c64a726869bc4142d270184c419749d34f167646baa21008eb0a79"
|
||||||
|
name = "github.com/mitchellh/go-homedir"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "af06845cf3004701891bf4fdb884bfe4920b3727"
|
||||||
|
version = "v1.1.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:53bc4cd4914cd7cd52139990d5170d6dc99067ae31c56530621b18b35fc30318"
|
||||||
|
name = "github.com/mitchellh/mapstructure"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "3536a929edddb9a5b34bd6861dc4a9647cb459fe"
|
||||||
|
version = "v1.1.2"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:95741de3af260a92cc5c7f3f3061e85273f5a81b5db20d4bd68da74bd521675e"
|
||||||
|
name = "github.com/pelletier/go-toml"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "c01d1270ff3e442a8a57cddc1c92dc1138598194"
|
||||||
|
version = "v1.2.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:b36a0ede02c4c2aef7df7f91cbbb7bb88a98b5d253509d4f997dda526e50c88c"
|
||||||
|
name = "github.com/russross/blackfriday"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "05f3235734ad95d0016f6a23902f06461fcf567a"
|
||||||
|
version = "v1.5.2"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:3e39bafd6c2f4bf3c76c3bfd16a2e09e016510ad5db90dc02b88e2f565d6d595"
|
||||||
|
name = "github.com/spf13/afero"
|
||||||
|
packages = [
|
||||||
|
".",
|
||||||
|
"mem",
|
||||||
|
]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "f4711e4db9e9a1d3887343acb72b2bbfc2f686f5"
|
||||||
|
version = "v1.2.1"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:08d65904057412fc0270fc4812a1c90c594186819243160dc779a402d4b6d0bc"
|
||||||
|
name = "github.com/spf13/cast"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "8c9545af88b134710ab1cd196795e7f2388358d7"
|
||||||
|
version = "v1.3.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:68ea4e23713989dc20b1bded5d9da2c5f9be14ff9885beef481848edd18c26cb"
|
||||||
|
name = "github.com/spf13/jwalterweatherman"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "4a4406e478ca629068e7768fc33f3f044173c0a6"
|
||||||
|
version = "v1.0.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:c1b1102241e7f645bc8e0c22ae352e8f0dc6484b6cb4d132fa9f24174e0119e2"
|
||||||
|
name = "github.com/spf13/pflag"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "298182f68c66c05229eb03ac171abe6e309ee79a"
|
||||||
|
version = "v1.0.3"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:de37e343c64582d7026bf8ab6ac5b22a72eac54f3a57020db31524affed9f423"
|
||||||
|
name = "github.com/spf13/viper"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "6d33b5a963d922d182c91e8a1c88d81fd150cfd4"
|
||||||
|
version = "v1.3.1"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
branch = "master"
|
||||||
|
digest = "1:d0e9a312c4610a508569ab25e54d34600b1a96d19b1866ece104ffdf1c1b9d2c"
|
||||||
|
name = "golang.org/x/sys"
|
||||||
|
packages = ["unix"]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "cd391775e71e684db52b63df9affd58269495083"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:8029e9743749d4be5bc9f7d42ea1659471767860f0cdc34d37c3111bd308a295"
|
||||||
|
name = "golang.org/x/text"
|
||||||
|
packages = [
|
||||||
|
"internal/gen",
|
||||||
|
"internal/triegen",
|
||||||
|
"internal/ucd",
|
||||||
|
"transform",
|
||||||
|
"unicode/cldr",
|
||||||
|
"unicode/norm",
|
||||||
|
]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
|
||||||
|
version = "v0.3.0"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96"
|
||||||
|
name = "gopkg.in/yaml.v2"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "51d6538a90f86fe93ac480b35f37b2be17fef232"
|
||||||
|
version = "v2.2.2"
|
||||||
|
|
||||||
|
[solve-meta]
|
||||||
|
analyzer-name = "dep"
|
||||||
|
analyzer-version = 1
|
||||||
|
input-imports = [
|
||||||
|
"github.com/cpuguy83/go-md2man/md2man",
|
||||||
|
"github.com/inconshreveable/mousetrap",
|
||||||
|
"github.com/mitchellh/go-homedir",
|
||||||
|
"github.com/spf13/pflag",
|
||||||
|
"github.com/spf13/viper",
|
||||||
|
"gopkg.in/yaml.v2",
|
||||||
|
]
|
||||||
|
solver-name = "gps-cdcl"
|
||||||
|
solver-version = 1
|
54
Gopkg.toml
Normal file
54
Gopkg.toml
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
# Gopkg.toml example
|
||||||
|
#
|
||||||
|
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
|
||||||
|
# for detailed Gopkg.toml documentation.
|
||||||
|
#
|
||||||
|
# required = ["github.com/user/thing/cmd/thing"]
|
||||||
|
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
|
||||||
|
#
|
||||||
|
# [[constraint]]
|
||||||
|
# name = "github.com/user/project"
|
||||||
|
# version = "1.0.0"
|
||||||
|
#
|
||||||
|
# [[constraint]]
|
||||||
|
# name = "github.com/user/project2"
|
||||||
|
# branch = "dev"
|
||||||
|
# source = "github.com/myfork/project2"
|
||||||
|
#
|
||||||
|
# [[override]]
|
||||||
|
# name = "github.com/x/y"
|
||||||
|
# version = "2.4.0"
|
||||||
|
#
|
||||||
|
# [prune]
|
||||||
|
# non-go = false
|
||||||
|
# go-tests = true
|
||||||
|
# unused-packages = true
|
||||||
|
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/cpuguy83/go-md2man"
|
||||||
|
version = "1.0.8"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/inconshreveable/mousetrap"
|
||||||
|
version = "1.0.0"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/mitchellh/go-homedir"
|
||||||
|
version = "1.1.0"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/spf13/pflag"
|
||||||
|
version = "1.0.3"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/spf13/viper"
|
||||||
|
version = "1.3.1"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "gopkg.in/yaml.v2"
|
||||||
|
version = "2.2.2"
|
||||||
|
|
||||||
|
[prune]
|
||||||
|
go-tests = true
|
||||||
|
unused-packages = true
|
|
@ -25,9 +25,11 @@ import (
|
||||||
func init() {
|
func init() {
|
||||||
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")
|
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")
|
||||||
addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command")
|
addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command")
|
||||||
|
addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`)
|
||||||
}
|
}
|
||||||
|
|
||||||
var packageName, parentName string
|
var packageName, parentName string
|
||||||
|
var cmdFlags []string
|
||||||
|
|
||||||
var addCmd = &cobra.Command{
|
var addCmd = &cobra.Command{
|
||||||
Use: "add [command name]",
|
Use: "add [command name]",
|
||||||
|
@ -69,7 +71,7 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
|
||||||
// validateCmdName returns source without any dashes and underscore.
|
// validateCmdName returns source without any dashes and underscore.
|
||||||
// If there will be dash or underscore, next letter will be uppered.
|
// If there will be dash or underscore, next letter will be uppered.
|
||||||
// It supports only ASCII (1-byte character) strings.
|
// It supports only ASCII (1-byte character) strings.
|
||||||
// https://github.com/spf13/cobra/issues/269
|
// https://github.com/OneCloudInc/cobra/issues/269
|
||||||
func validateCmdName(source string) string {
|
func validateCmdName(source string) string {
|
||||||
i := 0
|
i := 0
|
||||||
l := len(source)
|
l := len(source)
|
||||||
|
@ -128,37 +130,24 @@ package {{.cmdPackage}}
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/OneCloudInc/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
{{ printFlagVars .flags }}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
{{.parentName}}.AddCommand({{.cmdName}}Cmd){{ printFlagCreates .flags false }}
|
||||||
|
}
|
||||||
|
|
||||||
// {{.cmdName}}Cmd represents the {{.cmdName}} command
|
// {{.cmdName}}Cmd represents the {{.cmdName}} command
|
||||||
var {{.cmdName}}Cmd = &cobra.Command{
|
var {{.cmdName}}Cmd = &cobra.Command{
|
||||||
Use: "{{.cmdName}}",
|
Use: "{{.cmdName}}",
|
||||||
Short: "A brief description of your command",
|
Short: "{{.cmdName}}",
|
||||||
Long: ` + "`" + `A longer description that spans multiple lines and likely contains examples
|
Long: ` + "`" + `Description` + "`" + `,
|
||||||
and usage of using your command. For example:
|
|
||||||
|
|
||||||
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.` + "`" + `,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("{{.cmdName}} called")
|
fmt.Println("{{.cmdName}} called, place the command logic here")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
{{.parentName}}.AddCommand({{.cmdName}}Cmd)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
|
||||||
// and all subcommands, e.g.:
|
|
||||||
// {{.cmdName}}Cmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
||||||
|
|
||||||
// Cobra supports local flags which will only run when this command
|
|
||||||
// is called directly, e.g.:
|
|
||||||
// {{.cmdName}}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
|
||||||
`
|
`
|
||||||
|
|
||||||
data := make(map[string]interface{})
|
data := make(map[string]interface{})
|
||||||
|
@ -167,6 +156,15 @@ func init() {
|
||||||
data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path
|
data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path
|
||||||
data["parentName"] = parentName
|
data["parentName"] = parentName
|
||||||
data["cmdName"] = cmdName
|
data["cmdName"] = cmdName
|
||||||
|
flags := []flagDefinition{}
|
||||||
|
for _, value := range cmdFlags {
|
||||||
|
f, err := buildFlag(value, cmdName)
|
||||||
|
if err != nil {
|
||||||
|
er(err)
|
||||||
|
}
|
||||||
|
flags = append(flags, f)
|
||||||
|
}
|
||||||
|
data["flags"] = flags
|
||||||
|
|
||||||
cmdScript, err := executeTemplate(template, data)
|
cmdScript, err := executeTemplate(template, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,13 +10,13 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestGoldenAddCmd initializes the project "github.com/spf13/testproject"
|
// TestGoldenAddCmd initializes the project "github.com/OneCloudInc/testproject"
|
||||||
// in GOPATH, adds "test" command
|
// in GOPATH, adds "test" command
|
||||||
// and compares the content of all files in cmd directory of testproject
|
// and compares the content of all files in cmd directory of testproject
|
||||||
// with appropriate golden files.
|
// with appropriate golden files.
|
||||||
// Use -update to update existing golden files.
|
// Use -update to update existing golden files.
|
||||||
func TestGoldenAddCmd(t *testing.T) {
|
func TestGoldenAddCmd(t *testing.T) {
|
||||||
projectName := "github.com/spf13/testproject"
|
projectName := "github.com/OneCloudInc/testproject"
|
||||||
project := NewProject(projectName)
|
project := NewProject(projectName)
|
||||||
defer os.RemoveAll(project.AbsPath())
|
defer os.RemoveAll(project.AbsPath())
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ func TestGoldenAddCmd(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make path relative to project.CmdPath().
|
// Make path relative to project.CmdPath().
|
||||||
// E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go"
|
// E.g. path = "/home/user/go/src/github.com/OneCloudInc/testproject/cmd/root.go"
|
||||||
// then it returns just "root.go".
|
// then it returns just "root.go".
|
||||||
relPath, err := filepath.Rel(project.CmdPath(), path)
|
relPath, err := filepath.Rel(project.CmdPath(), path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
89
cobra/cmd/flag_helpers.go
Normal file
89
cobra/cmd/flag_helpers.go
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var specialFlagTypes = []string{"stringMapFlag", "genericMapFlag"}
|
||||||
|
|
||||||
|
// Indexes for flag definition string after split {name}:{type}:{description}
|
||||||
|
const (
|
||||||
|
FlagDefLength = 3
|
||||||
|
FlagDefSeparator = ":"
|
||||||
|
FlagDefault = ` "",`
|
||||||
|
FlagNameIndex = 0
|
||||||
|
FlagTypeIndex = 1
|
||||||
|
FlagDescriptionIndex = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
type flagDefinition struct {
|
||||||
|
Name string
|
||||||
|
FlagType string
|
||||||
|
FlagDescription string
|
||||||
|
CreateFn string
|
||||||
|
VarName string
|
||||||
|
Default string
|
||||||
|
CmdName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func printFlagVars(iFlags interface{}) string {
|
||||||
|
flags := toFlagArray(iFlags)
|
||||||
|
varDefs := ""
|
||||||
|
for _, f := range flags {
|
||||||
|
varDefs += fmt.Sprintf("var %s %s\r\n", f.VarName, f.FlagType)
|
||||||
|
}
|
||||||
|
return varDefs
|
||||||
|
}
|
||||||
|
|
||||||
|
func printFlagCreates(iFlags interface{}, persistent bool) string {
|
||||||
|
flags := toFlagArray(iFlags)
|
||||||
|
createStrs := ""
|
||||||
|
flagsFn := "Flags"
|
||||||
|
if persistent {
|
||||||
|
flagsFn = "PersistentFlags"
|
||||||
|
}
|
||||||
|
for _, f := range flags {
|
||||||
|
createStrs += fmt.Sprintf( `
|
||||||
|
%sCmd.%s().%s(&%s, "%s",%s "%s")`,
|
||||||
|
f.CmdName,
|
||||||
|
flagsFn,
|
||||||
|
f.CreateFn,
|
||||||
|
f.VarName,
|
||||||
|
f.Name,
|
||||||
|
f.Default,
|
||||||
|
f.FlagDescription)
|
||||||
|
}
|
||||||
|
return createStrs
|
||||||
|
}
|
||||||
|
|
||||||
|
func toFlagArray(value interface{}) []flagDefinition {
|
||||||
|
switch v := value.(type) {
|
||||||
|
case []flagDefinition:
|
||||||
|
return v
|
||||||
|
// Add whatever other types you need
|
||||||
|
default:
|
||||||
|
return []flagDefinition{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildFlag(in, cmdName string) (flagDefinition, error) {
|
||||||
|
var def flagDefinition
|
||||||
|
values := strings.Split(in, FlagDefSeparator)
|
||||||
|
if len(values) != FlagDefLength {
|
||||||
|
return def, errors.New("invalid flag definition, make sure to specify flags as 'name:type:definition' they are all required")
|
||||||
|
}
|
||||||
|
name, flagType, desc := values[FlagNameIndex], values[FlagTypeIndex],values[FlagDescriptionIndex]
|
||||||
|
def = flagDefinition{
|
||||||
|
Name: name,
|
||||||
|
FlagType: flagType,
|
||||||
|
FlagDescription: desc,
|
||||||
|
CreateFn: fmt.Sprintf("%s%s", strings.Title(flagType), "Var"),
|
||||||
|
VarName: fmt.Sprintf("%s%sFlag", cmdName, name),
|
||||||
|
Default:FlagDefault,
|
||||||
|
CmdName: cmdName,
|
||||||
|
}
|
||||||
|
|
||||||
|
return def, nil
|
||||||
|
}
|
|
@ -111,7 +111,7 @@ func exists(path string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func executeTemplate(tmplStr string, data interface{}) (string, error) {
|
func executeTemplate(tmplStr string, data interface{}) (string, error) {
|
||||||
tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString}).Parse(tmplStr)
|
tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString, "printFlagVars": printFlagVars, "printFlagCreates": printFlagCreates}).Parse(tmplStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,10 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
initCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`)
|
||||||
|
}
|
||||||
|
|
||||||
var initCmd = &cobra.Command{
|
var initCmd = &cobra.Command{
|
||||||
Use: "init [name]",
|
Use: "init [name]",
|
||||||
Aliases: []string{"initialize", "initialise", "create"},
|
Aliases: []string{"initialize", "initialise", "create"},
|
||||||
|
@ -150,6 +154,23 @@ import (
|
||||||
|
|
||||||
var cfgFile string{{end}}
|
var cfgFile string{{end}}
|
||||||
|
|
||||||
|
{{ printFlagVars .flags }}
|
||||||
|
|
||||||
|
func init() { {{- if .viper}}
|
||||||
|
cobra.OnInitialize(initConfig)
|
||||||
|
{{end}}
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
// Cobra supports persistent flags, which, if defined here,
|
||||||
|
// will be global for your application.{{ if .viper }}
|
||||||
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }}
|
||||||
|
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }}
|
||||||
|
|
||||||
|
// Cobra also supports local flags, which will only run
|
||||||
|
// when this action is called directly.
|
||||||
|
|
||||||
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle"){{ printFlagCreates .flags true }}
|
||||||
|
}
|
||||||
|
|
||||||
// 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: "{{.appName}}",
|
Use: "{{.appName}}",
|
||||||
|
@ -173,20 +194,7 @@ func Execute() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
{{ if .viper }}
|
||||||
func init() { {{- if .viper}}
|
|
||||||
cobra.OnInitialize(initConfig)
|
|
||||||
{{end}}
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
// Cobra supports persistent flags, which, if defined here,
|
|
||||||
// will be global for your application.{{ if .viper }}
|
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }}
|
|
||||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }}
|
|
||||||
|
|
||||||
// Cobra also supports local flags, which will only run
|
|
||||||
// when this action is called directly.
|
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}{{ if .viper }}
|
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
@ -221,6 +229,16 @@ func initConfig() {
|
||||||
data["license"] = project.License().Header
|
data["license"] = project.License().Header
|
||||||
data["appName"] = path.Base(project.Name())
|
data["appName"] = path.Base(project.Name())
|
||||||
|
|
||||||
|
flags := []flagDefinition{}
|
||||||
|
for _, value := range cmdFlags {
|
||||||
|
f, err := buildFlag(value, "rootCmd")
|
||||||
|
if err != nil {
|
||||||
|
er(err)
|
||||||
|
}
|
||||||
|
flags = append(flags, f)
|
||||||
|
}
|
||||||
|
data["flags"] = flags
|
||||||
|
|
||||||
rootCmdScript, err := executeTemplate(template, data)
|
rootCmdScript, err := executeTemplate(template, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
|
|
|
@ -10,12 +10,12 @@ import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestGoldenInitCmd initializes the project "github.com/spf13/testproject"
|
// TestGoldenInitCmd initializes the project "github.com/OneCloudInc/testproject"
|
||||||
// in GOPATH and compares the content of files in initialized project with
|
// in GOPATH and compares the content of files in initialized project with
|
||||||
// appropriate golden files ("testdata/*.golden").
|
// appropriate golden files ("testdata/*.golden").
|
||||||
// Use -update to update existing golden files.
|
// Use -update to update existing golden files.
|
||||||
func TestGoldenInitCmd(t *testing.T) {
|
func TestGoldenInitCmd(t *testing.T) {
|
||||||
projectName := "github.com/spf13/testproject"
|
projectName := "github.com/OneCloudInc/testproject"
|
||||||
project := NewProject(projectName)
|
project := NewProject(projectName)
|
||||||
defer os.RemoveAll(project.AbsPath())
|
defer os.RemoveAll(project.AbsPath())
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ func TestGoldenInitCmd(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make path relative to project.AbsPath().
|
// Make path relative to project.AbsPath().
|
||||||
// E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go"
|
// E.g. path = "/home/user/go/src/github.com/OneCloudInc/testproject/cmd/root.go"
|
||||||
// then it returns just "cmd/root.go".
|
// then it returns just "cmd/root.go".
|
||||||
relPath, err := filepath.Rel(project.AbsPath(), path)
|
relPath, err := filepath.Rel(project.AbsPath(), path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -45,7 +45,7 @@ func init() {
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
|
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().StringP("author", "a", "YOUR NAME", "author name for copyright attribution")
|
||||||
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
|
rootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "name of license for the project")
|
||||||
rootCmd.PersistentFlags().Bool("viper", true, "use Viper for configuration")
|
rootCmd.PersistentFlags().Bool("viper", false, "use Viper for configuration")
|
||||||
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
|
viper.BindPFlag("author", rootCmd.PersistentFlags().Lookup("author"))
|
||||||
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
viper.BindPFlag("useViper", rootCmd.PersistentFlags().Lookup("viper"))
|
||||||
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
|
|
||||||
//
|
|
||||||
// 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"
|
|
||||||
|
|
||||||
"github.com/OneCloudInc/cobra"
|
|
||||||
)
|
|
||||||
|
|
||||||
var testqueryIdFlag string
|
|
||||||
var testparamsFlag stringMap
|
|
||||||
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
testCmd.Flags().StringVar(&testqueryIdFlag, "queryId", "", "Query ID")
|
|
||||||
testCmd.Flags().StringMapVar(&testparamsFlag, "params", "", "Params")
|
|
||||||
|
|
||||||
rootCmd.AddCommand(testCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// testCmd represents the test command
|
|
||||||
var testCmd = &cobra.Command{
|
|
||||||
Use: "test",
|
|
||||||
Short: "A brief description of your command",
|
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
|
||||||
and usage of using your command. For example:
|
|
||||||
|
|
||||||
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.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
fmt.Println("test called")
|
|
||||||
},
|
|
||||||
}
|
|
2
cobra/cmd/testdata/main.go.golden
vendored
2
cobra/cmd/testdata/main.go.golden
vendored
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/spf13/testproject/cmd"
|
import "github.com/OneCloudInc/testproject/cmd"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
|
|
54
cobra/cmd/testdata/root.go.golden
vendored
54
cobra/cmd/testdata/root.go.golden
vendored
|
@ -18,12 +18,22 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
|
||||||
"github.com/OneCloudInc/cobra"
|
"github.com/OneCloudInc/cobra"
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var cfgFile string
|
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Here you will define your flags and configuration settings.
|
||||||
|
// Cobra supports persistent flags, which, if defined here,
|
||||||
|
// will be global for your application.
|
||||||
|
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")
|
||||||
|
|
||||||
|
// Cobra also supports local flags, which will only run
|
||||||
|
// when this action is called directly.
|
||||||
|
|
||||||
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||||
|
}
|
||||||
|
|
||||||
// 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{
|
||||||
|
@ -49,41 +59,3 @@ func Execute() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
cobra.OnInitialize(initConfig)
|
|
||||||
|
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
// Cobra supports persistent flags, which, if defined here,
|
|
||||||
// will be global for your application.
|
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")
|
|
||||||
|
|
||||||
// Cobra also supports local flags, which will only run
|
|
||||||
// when this action is called directly.
|
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
|
||||||
func initConfig() {
|
|
||||||
if cfgFile != "" {
|
|
||||||
// Use config file from the flag.
|
|
||||||
viper.SetConfigFile(cfgFile)
|
|
||||||
} else {
|
|
||||||
// Find home directory.
|
|
||||||
home, err := homedir.Dir()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search config in home directory with name ".testproject" (without extension).
|
|
||||||
viper.AddConfigPath(home)
|
|
||||||
viper.SetConfigName(".testproject")
|
|
||||||
}
|
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
|
||||||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
33
cobra/cmd/testdata/test.go.golden
vendored
33
cobra/cmd/testdata/test.go.golden
vendored
|
@ -20,31 +20,18 @@ import (
|
||||||
"github.com/OneCloudInc/cobra"
|
"github.com/OneCloudInc/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// testCmd represents the test command
|
|
||||||
var testCmd = &cobra.Command{
|
|
||||||
Use: "test",
|
|
||||||
Short: "A brief description of your command",
|
|
||||||
Long: `A longer description that spans multiple lines and likely contains examples
|
|
||||||
and usage of using your command. For example:
|
|
||||||
|
|
||||||
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.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
fmt.Println("test called")
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(testCmd)
|
rootCmd.AddCommand(testCmd)
|
||||||
|
}
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
|
// testCmd represents the test command
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
var testCmd = &cobra.Command{
|
||||||
// and all subcommands, e.g.:
|
Use: "test",
|
||||||
// testCmd.PersistentFlags().String("foo", "", "A help for foo")
|
Short: "test",
|
||||||
|
Long: `Description`,
|
||||||
// Cobra supports local flags which will only run when this command
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// is called directly, e.g.:
|
fmt.Println("test called, place the command logic here")
|
||||||
// testCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue