cmd: Fix #612 (#614)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
If user has a project in symlink, just use its destination folder and work there.
authored by Albert Nigmatzianov · committed by GitHub · parent b95ab73 · Browse files · 2 files·+22 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+22 added−20 removed
- other:+22 added−20 removed
Provenance
- Commit
0c34d16- Tree
4bcd026- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
b95ab73
2 files changed+22 added−20 removed
modified:cobra/cmd/helpers.go-3 removed
22 unmodified lines23 "strings"24 "text/template"25 )26 27-var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}27 var srcPaths []string28 29 func init() {30 // Initialize srcPaths.95 unmodified lines126 }127 128 // writeToFile writes r to file with path only129 // if file/directory on given path doesn't exist.131-// If file/directory exists on given path, then132-// it terminates app and prints an appropriate error.130 func writeToFile(path string, r io.Reader) error {131 if exists(path) {132 return fmt.Errorf("%v already exists", path)133 }35 unmodified linesmodified:cobra/cmd/project.go+22 added-17 removed
15 unmodified lines16 name string17 }18 19 // NewProject returns Project with specified project name.20-// If projectName is blank string, it returns nil.20 func NewProject(projectName string) *Project {21 if projectName == "" {23- return nil22+ er("can't create project with blank name")23 }24 25 p := new(Project)26 p.name = projectName25 unmodified lines52 return p53 }54 55 // findPackage returns full path to existing go package in GOPATHs.57-// findPackage returns "", if it can't find path.58-// If packageName is "", findPackage returns "".56 func findPackage(packageName string) string {57 if packageName == "" {58 return ""59 }9 unmodified lines69 }70 71 // NewProjectFromPath returns Project with specified absolute path to72 // package.76-// If absPath is blank string or if absPath is not actually absolute,77-// it returns nil.73 func NewProjectFromPath(absPath string) *Project {79- if absPath == "" || !filepath.IsAbs(absPath) {80- return nil74+ if absPath == "" {75+ er("can't create project: absPath can't be blank")76 }77+ if !filepath.IsAbs(absPath) {78+ er("can't create project: absPath is not absolute")79+ }80 81+ // If absPath is symlink, use its destination.82+ fi, err := os.Lstat(absPath)83+ if err != nil {84+ er("can't read path info: " + err.Error())85+ }86+ if fi.Mode()&os.ModeSymlink != 0 {87+ path, err := os.Readlink(absPath)88+ if err != nil {89+ er("can't read the destination of symlink: " + err.Error())90+ }91+ absPath = path92+ }93+94 p := new(Project)84- p.absPath = absPath85- p.absPath = strings.TrimSuffix(p.absPath, findCmdDir(p.absPath))95+ p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath))96 p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath()))97 return p98 }99 100 // trimSrcPath trims at the beginning of absPath the srcPath.101 func trimSrcPath(absPath, srcPath string) string {102 relPath, err := filepath.Rel(srcPath, absPath)103 if err != nil {94- er("Cobra supports project only within $GOPATH: " + err.Error())104+ er(err)105 }106 return relPath107 }108 1 unmodified line110 func (p *Project) License() License {111 if p.license.Text == "" && p.license.Name != "None" {112 p.license = getLicense()113 }104-114 return p.license115 }116 117 // Name returns the name of project, e.g. "github.com/spf13/cobra"1 unmodified line119 return p.name120 }121 122 // CmdPath returns absolute path to directory, where all commands are located.114-//115-// CmdPath returns blank string, only if p.AbsPath() is a blank string.123 func (p *Project) CmdPath() string {124 if p.absPath == "" {125 return ""126 }4 unmodified lines131 }132 133 // findCmdDir checks if base of absPath is cmd dir and returns it or134 // looks for existing cmd dir in absPath.128-// If the cmd dir doesn't exist, empty, or cannot be found,129-// it returns "cmd".135 func findCmdDir(absPath string) string {136 if !exists(absPath) || isEmpty(absPath) {137 return "cmd"138 }14 unmodified lines153 154 // isCmdDir checks if base of name is one of cmdDir.155 func isCmdDir(name string) bool {156 name = filepath.Base(name)152- for _, cmdDir := range cmdDirs {157+ for _, cmdDir := range []string{"cmd", "cmds", "command", "commands"} {158 if name == cmdDir {159 return true160 }161 }39 unmodified lines