Skip to content
Myr

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

2 files+22 added−20 removed

modified:cobra/cmd/helpers.go-3 removed
22 unmodified lines
23 "strings"24 "text/template"25)2627var cmdDirs = [...]string{"cmd", "cmds", "command", "commands"}27var srcPaths []string2829func init() {30 // Initialize srcPaths.
95 unmodified lines
126}127128// 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.130func writeToFile(path string, r io.Reader) error {131 if exists(path) {132 return fmt.Errorf("%v already exists", path)133 }
35 unmodified lines
modified:cobra/cmd/project.go+22 added-17 removed
15 unmodified lines
16 name string17}1819// NewProject returns Project with specified project name.20// If projectName is blank string, it returns nil.20func NewProject(projectName string) *Project {21 if projectName == "" {23 return nil22 er("can't create project with blank name")23 }2425 p := new(Project)26 p.name = projectName
25 unmodified lines
52 return p53}5455// 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 "".56func findPackage(packageName string) string {57 if packageName == "" {58 return ""59 }
9 unmodified lines
69}7071// 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.73func 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 }8081 // 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 }9394 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}99100// trimSrcPath trims at the beginning of absPath the srcPath.101func 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 line
110func (p *Project) License() License {111 if p.license.Text == "" && p.license.Name != "None" {112 p.license = getLicense()113 }104114 return p.license115}116117// Name returns the name of project, e.g. "github.com/spf13/cobra"
1 unmodified line
119 return p.name120}121122// 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.123func (p *Project) CmdPath() string {124 if p.absPath == "" {125 return ""126 }
4 unmodified lines
131}132133// 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".135func findCmdDir(absPath string) string {136 if !exists(absPath) || isEmpty(absPath) {137 return "cmd"138 }
14 unmodified lines
153154// isCmdDir checks if base of name is one of cmdDir.155func 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