From e570cad58a897ededa8499674f1155da34c06100 Mon Sep 17 00:00:00 2001 From: Joshua Harshman Date: Thu, 11 Oct 2018 11:38:30 -0700 Subject: [PATCH] 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 --- cobra/cmd/add.go | 4 +-- cobra/cmd/init.go | 7 ++-- cobra/cmd/project.go | 78 ++++++++++++++++++++++++++++++-------------- cobra/cmd/root.go | 2 ++ 4 files changed, 62 insertions(+), 29 deletions(-) diff --git a/cobra/cmd/add.go b/cobra/cmd/add.go index fb22096a..1c76701c 100644 --- a/cobra/cmd/add.go +++ b/cobra/cmd/add.go @@ -49,13 +49,13 @@ Example: cobra add server -> resulting in a new cmd/server.go`, var project *Project if packageName != "" { - project = NewProject(packageName) + project = NewProject(packageName, vgo) } else { wd, err := os.Getwd() if err != nil { er(err) } - project = NewProjectFromPath(wd) + project = NewProjectFromPath(wd, vgo) } cmdName := validateCmdName(args[0]) diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index d65e6c8c..ec5a59cc 100644 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -40,6 +40,7 @@ and the appropriate structure for a Cobra-based CLI application. Init will not use an existing directory with contents.`, Run: func(cmd *cobra.Command, args []string) { + wd, err := os.Getwd() if err != nil { er(err) @@ -47,16 +48,16 @@ Init will not use an existing directory with contents.`, var project *Project if len(args) == 0 { - project = NewProjectFromPath(wd) + project = NewProjectFromPath(wd, vgo) } else if len(args) == 1 { arg := args[0] if arg[0] == '.' { arg = filepath.Join(wd, arg) } if filepath.IsAbs(arg) { - project = NewProjectFromPath(arg) + project = NewProjectFromPath(arg, vgo) } else { - project = NewProject(arg) + project = NewProject(arg, vgo) // testing this case } } else { er("please provide only one argument") diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index 7ddb8258..a8a2b5a1 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "os" "path/filepath" "runtime" @@ -17,7 +18,7 @@ type Project struct { } // NewProject returns Project with specified project name. -func NewProject(projectName string) *Project { +func NewProject(projectName string, vgo string) *Project { if projectName == "" { er("can't create project with blank name") } @@ -25,33 +26,56 @@ func NewProject(projectName string) *Project { p := new(Project) p.name = projectName - // 1. Find already created protect. - p.absPath = findPackage(projectName) - - // 2. If there are no created project with this path, and user is in GOPATH, - // then use GOPATH/src/projectName. - if p.absPath == "" { - wd, err := os.Getwd() - if err != nil { - er(err) - } - for _, srcPath := range srcPaths { - goPath := filepath.Dir(srcPath) - if filepathHasPrefix(wd, goPath) { - p.absPath = filepath.Join(srcPath, projectName) - break - } - } + wd, err := os.Getwd() + if err != nil { + er(err) } - // 3. If user is not in GOPATH, then use (first GOPATH)/src/projectName. - if p.absPath == "" { - p.absPath = filepath.Join(srcPaths[0], projectName) + /* add support for Go modules. ISSUE #756 */ + if vgo == "" { + // vgo not set, business as usual + // 1. Find already created protect. + p.absPath = findPackage(projectName) + + // 2. If there are no created project with this path, and user is in GOPATH, + // then use GOPATH/src/projectName. + if p.absPath == "" { + for _, srcPath := range srcPaths { + goPath := filepath.Dir(srcPath) + if filepathHasPrefix(wd, goPath) { + p.absPath = filepath.Join(srcPath, projectName) + break + } + } + } + + // 3. If user is not in GOPATH, then use (first GOPATH)/src/projectName. + if p.absPath == "" { + 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 } +/* 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. func findPackage(packageName string) string { if packageName == "" { @@ -70,7 +94,7 @@ func findPackage(packageName string) string { // NewProjectFromPath returns Project with specified absolute path to // package. -func NewProjectFromPath(absPath string) *Project { +func NewProjectFromPath(absPath string, vgo string) *Project { if absPath == "" { er("can't create project: absPath can't be blank") } @@ -92,8 +116,14 @@ func NewProjectFromPath(absPath string) *Project { } p := new(Project) - p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath)) - p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath())) + if vgo == "" { + p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath)) + p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath())) + } else { + p.name = vgo + p = createWithModuleSupport(absPath, p) + } + return p } diff --git a/cobra/cmd/root.go b/cobra/cmd/root.go index 19568f98..c09881f5 100644 --- a/cobra/cmd/root.go +++ b/cobra/cmd/root.go @@ -24,6 +24,7 @@ import ( var ( // Used for flags. cfgFile, userLicense string + vgo string rootCmd = &cobra.Command{ Use: "cobra", @@ -46,6 +47,7 @@ func init() { 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", 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("useViper", rootCmd.PersistentFlags().Lookup("viper")) viper.SetDefault("author", "NAME HERE ")