mirror of
https://github.com/spf13/cobra
synced 2025-05-06 13:27:26 +00:00
756-vgo-support - add vgo bool flag
756 - tweak vgo param to be string * vgo param represents package name of project 756-vgo-support - create in current working directory 756-vgo-support - init outside of GOPATH 756-vgo-support - work on init cmd formatting
This commit is contained in:
parent
d2d81d9a96
commit
e570cad58a
4 changed files with 62 additions and 29 deletions
|
@ -49,13 +49,13 @@ Example: cobra add server -> resulting in a new cmd/server.go`,
|
||||||
|
|
||||||
var project *Project
|
var project *Project
|
||||||
if packageName != "" {
|
if packageName != "" {
|
||||||
project = NewProject(packageName)
|
project = NewProject(packageName, vgo)
|
||||||
} else {
|
} else {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
}
|
}
|
||||||
project = NewProjectFromPath(wd)
|
project = NewProjectFromPath(wd, vgo)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdName := validateCmdName(args[0])
|
cmdName := validateCmdName(args[0])
|
||||||
|
|
|
@ -40,6 +40,7 @@ and the appropriate structure for a Cobra-based CLI application.
|
||||||
Init will not use an existing directory with contents.`,
|
Init will not use an existing directory with contents.`,
|
||||||
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er(err)
|
er(err)
|
||||||
|
@ -47,16 +48,16 @@ Init will not use an existing directory with contents.`,
|
||||||
|
|
||||||
var project *Project
|
var project *Project
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
project = NewProjectFromPath(wd)
|
project = NewProjectFromPath(wd, vgo)
|
||||||
} else if len(args) == 1 {
|
} else if len(args) == 1 {
|
||||||
arg := args[0]
|
arg := args[0]
|
||||||
if arg[0] == '.' {
|
if arg[0] == '.' {
|
||||||
arg = filepath.Join(wd, arg)
|
arg = filepath.Join(wd, arg)
|
||||||
}
|
}
|
||||||
if filepath.IsAbs(arg) {
|
if filepath.IsAbs(arg) {
|
||||||
project = NewProjectFromPath(arg)
|
project = NewProjectFromPath(arg, vgo)
|
||||||
} else {
|
} else {
|
||||||
project = NewProject(arg)
|
project = NewProject(arg, vgo) // testing this case
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
er("please provide only one argument")
|
er("please provide only one argument")
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -17,7 +18,7 @@ type Project struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewProject returns Project with specified project name.
|
// NewProject returns Project with specified project name.
|
||||||
func NewProject(projectName string) *Project {
|
func NewProject(projectName string, vgo string) *Project {
|
||||||
if projectName == "" {
|
if projectName == "" {
|
||||||
er("can't create project with blank name")
|
er("can't create project with blank name")
|
||||||
}
|
}
|
||||||
|
@ -25,16 +26,20 @@ func NewProject(projectName string) *Project {
|
||||||
p := new(Project)
|
p := new(Project)
|
||||||
p.name = projectName
|
p.name = projectName
|
||||||
|
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
er(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* add support for Go modules. ISSUE #756 */
|
||||||
|
if vgo == "" {
|
||||||
|
// vgo not set, business as usual
|
||||||
// 1. Find already created protect.
|
// 1. Find already created protect.
|
||||||
p.absPath = findPackage(projectName)
|
p.absPath = findPackage(projectName)
|
||||||
|
|
||||||
// 2. If there are no created project with this path, and user is in GOPATH,
|
// 2. If there are no created project with this path, and user is in GOPATH,
|
||||||
// then use GOPATH/src/projectName.
|
// then use GOPATH/src/projectName.
|
||||||
if p.absPath == "" {
|
if p.absPath == "" {
|
||||||
wd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
er(err)
|
|
||||||
}
|
|
||||||
for _, srcPath := range srcPaths {
|
for _, srcPath := range srcPaths {
|
||||||
goPath := filepath.Dir(srcPath)
|
goPath := filepath.Dir(srcPath)
|
||||||
if filepathHasPrefix(wd, goPath) {
|
if filepathHasPrefix(wd, goPath) {
|
||||||
|
@ -48,10 +53,29 @@ func NewProject(projectName string) *Project {
|
||||||
if p.absPath == "" {
|
if p.absPath == "" {
|
||||||
p.absPath = filepath.Join(srcPaths[0], projectName)
|
p.absPath = filepath.Join(srcPaths[0], projectName)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if wd != p.name {
|
||||||
|
wd = fmt.Sprintf("%s/%s", wd, p.name)
|
||||||
|
}
|
||||||
|
p.name = vgo
|
||||||
|
p = createWithModuleSupport(wd, p)
|
||||||
|
}
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* add support for Go modules. ISSUE #756 */
|
||||||
|
func createWithModuleSupport(workingDir string, project *Project) *Project {
|
||||||
|
for _, srcPath := range srcPaths {
|
||||||
|
goPath := filepath.Dir(srcPath)
|
||||||
|
if filepathHasPrefix(workingDir, goPath) || workingDir == goPath {
|
||||||
|
er("using modules, must be outside of GOPATH")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
project.absPath = workingDir
|
||||||
|
return project
|
||||||
|
}
|
||||||
|
|
||||||
// findPackage returns full path to existing go package in GOPATHs.
|
// findPackage returns full path to existing go package in GOPATHs.
|
||||||
func findPackage(packageName string) string {
|
func findPackage(packageName string) string {
|
||||||
if packageName == "" {
|
if packageName == "" {
|
||||||
|
@ -70,7 +94,7 @@ func findPackage(packageName string) string {
|
||||||
|
|
||||||
// NewProjectFromPath returns Project with specified absolute path to
|
// NewProjectFromPath returns Project with specified absolute path to
|
||||||
// package.
|
// package.
|
||||||
func NewProjectFromPath(absPath string) *Project {
|
func NewProjectFromPath(absPath string, vgo string) *Project {
|
||||||
if absPath == "" {
|
if absPath == "" {
|
||||||
er("can't create project: absPath can't be blank")
|
er("can't create project: absPath can't be blank")
|
||||||
}
|
}
|
||||||
|
@ -92,8 +116,14 @@ func NewProjectFromPath(absPath string) *Project {
|
||||||
}
|
}
|
||||||
|
|
||||||
p := new(Project)
|
p := new(Project)
|
||||||
|
if vgo == "" {
|
||||||
p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath))
|
p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath))
|
||||||
p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath()))
|
p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath()))
|
||||||
|
} else {
|
||||||
|
p.name = vgo
|
||||||
|
p = createWithModuleSupport(absPath, p)
|
||||||
|
}
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ import (
|
||||||
var (
|
var (
|
||||||
// Used for flags.
|
// Used for flags.
|
||||||
cfgFile, userLicense string
|
cfgFile, userLicense string
|
||||||
|
vgo string
|
||||||
|
|
||||||
rootCmd = &cobra.Command{
|
rootCmd = &cobra.Command{
|
||||||
Use: "cobra",
|
Use: "cobra",
|
||||||
|
@ -46,6 +47,7 @@ func init() {
|
||||||
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", true, "use Viper for configuration")
|
||||||
|
rootCmd.PersistentFlags().StringVar(&vgo, "vgo", "", "use vgo/modules available in Go >= 1.11")
|
||||||
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>")
|
||||||
|
|
Loading…
Add table
Reference in a new issue