spf13--cobra/cobra/cmd/project.go

208 lines
4.4 KiB
Go
Raw Normal View History

2017-04-29 10:02:02 +00:00
package cmd
import (
"os"
"path/filepath"
"runtime"
2017-04-29 10:02:02 +00:00
"strings"
)
2017-04-29 12:53:52 +00:00
// Project contains name, license and paths to projects.
2017-04-29 10:02:02 +00:00
type Project struct {
// v2
PkgName string
Copyright string
AbsolutePath string
Legal License
// v1
2017-04-29 10:02:02 +00:00
absPath string
2017-04-29 12:53:52 +00:00
cmdPath string
2017-04-29 10:02:02 +00:00
srcPath string
license License
name string
}
2017-04-29 12:53:52 +00:00
// NewProject returns Project with specified project name.
2017-04-29 10:02:02 +00:00
func NewProject(projectName string) *Project {
if projectName == "" {
er("can't create project with blank name")
2017-04-29 10:02:02 +00:00
}
p := new(Project)
p.name = projectName
// 1. Find already created protect.
p.absPath = findPackage(projectName)
2017-04-29 12:53:52 +00:00
// 2. If there are no created project with this path, and user is in GOPATH,
2017-05-05 08:06:10 +00:00
// then use GOPATH/src/projectName.
2017-04-29 10:02:02 +00:00
if p.absPath == "" {
wd, err := os.Getwd()
if err != nil {
er(err)
}
2017-05-05 08:06:10 +00:00
for _, srcPath := range srcPaths {
goPath := filepath.Dir(srcPath)
if filepathHasPrefix(wd, goPath) {
2017-05-05 08:06:10 +00:00
p.absPath = filepath.Join(srcPath, projectName)
2017-04-29 10:02:02 +00:00
break
}
}
}
2017-05-05 08:06:10 +00:00
// 3. If user is not in GOPATH, then use (first GOPATH)/src/projectName.
2017-04-29 10:02:02 +00:00
if p.absPath == "" {
p.absPath = filepath.Join(srcPaths[0], projectName)
}
return p
}
2017-04-29 12:53:52 +00:00
// findPackage returns full path to existing go package in GOPATHs.
2017-04-29 10:02:02 +00:00
func findPackage(packageName string) string {
if packageName == "" {
return ""
}
for _, srcPath := range srcPaths {
packagePath := filepath.Join(srcPath, packageName)
if exists(packagePath) {
return packagePath
}
}
return ""
}
2017-04-29 12:53:52 +00:00
// NewProjectFromPath returns Project with specified absolute path to
// package.
2017-04-29 10:02:02 +00:00
func NewProjectFromPath(absPath string) *Project {
if absPath == "" {
er("can't create project: absPath can't be blank")
}
if !filepath.IsAbs(absPath) {
er("can't create project: absPath is not absolute")
}
// If absPath is symlink, use its destination.
fi, err := os.Lstat(absPath)
if err != nil {
er("can't read path info: " + err.Error())
}
if fi.Mode()&os.ModeSymlink != 0 {
path, err := os.Readlink(absPath)
if err != nil {
er("can't read the destination of symlink: " + err.Error())
}
absPath = path
2017-04-29 10:02:02 +00:00
}
p := new(Project)
p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath))
2017-04-29 10:02:02 +00:00
p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath()))
return p
}
2017-04-29 20:50:31 +00:00
// trimSrcPath trims at the beginning of absPath the srcPath.
2017-04-29 10:02:02 +00:00
func trimSrcPath(absPath, srcPath string) string {
relPath, err := filepath.Rel(srcPath, absPath)
if err != nil {
er(err)
2017-04-29 10:02:02 +00:00
}
return relPath
}
2017-04-29 12:53:52 +00:00
// License returns the License object of project.
2017-04-29 10:02:02 +00:00
func (p *Project) License() License {
2017-04-29 12:53:52 +00:00
if p.license.Text == "" && p.license.Name != "None" {
2017-04-29 10:02:02 +00:00
p.license = getLicense()
}
return p.license
}
2017-04-29 12:53:52 +00:00
// Name returns the name of project, e.g. "github.com/spf13/cobra"
2017-04-29 10:02:02 +00:00
func (p Project) Name() string {
return p.name
}
2017-04-29 12:53:52 +00:00
// CmdPath returns absolute path to directory, where all commands are located.
func (p *Project) CmdPath() string {
2017-04-29 10:02:02 +00:00
if p.absPath == "" {
return ""
}
2017-04-29 12:53:52 +00:00
if p.cmdPath == "" {
p.cmdPath = filepath.Join(p.absPath, findCmdDir(p.absPath))
2017-04-29 10:02:02 +00:00
}
2017-04-29 12:53:52 +00:00
return p.cmdPath
2017-04-29 10:02:02 +00:00
}
2017-04-29 12:53:52 +00:00
// findCmdDir checks if base of absPath is cmd dir and returns it or
// looks for existing cmd dir in absPath.
2017-04-29 10:02:02 +00:00
func findCmdDir(absPath string) string {
if !exists(absPath) || isEmpty(absPath) {
return "cmd"
}
2017-05-05 08:06:10 +00:00
if isCmdDir(absPath) {
return filepath.Base(absPath)
2017-04-29 12:53:52 +00:00
}
2017-04-29 10:02:02 +00:00
files, _ := filepath.Glob(filepath.Join(absPath, "c*"))
2017-04-29 12:53:52 +00:00
for _, file := range files {
2017-05-05 08:06:10 +00:00
if isCmdDir(file) {
2017-05-05 08:31:07 +00:00
return filepath.Base(file)
2017-04-29 10:02:02 +00:00
}
}
return "cmd"
}
2017-05-05 08:06:10 +00:00
// isCmdDir checks if base of name is one of cmdDir.
func isCmdDir(name string) bool {
name = filepath.Base(name)
for _, cmdDir := range []string{"cmd", "cmds", "command", "commands"} {
2017-05-05 08:06:10 +00:00
if name == cmdDir {
return true
}
}
return false
}
2017-04-29 12:53:52 +00:00
// AbsPath returns absolute path of project.
2017-04-29 10:02:02 +00:00
func (p Project) AbsPath() string {
return p.absPath
}
2017-04-29 12:53:52 +00:00
// SrcPath returns absolute path to $GOPATH/src where project is located.
2017-04-29 10:02:02 +00:00
func (p *Project) SrcPath() string {
if p.srcPath != "" {
return p.srcPath
}
if p.absPath == "" {
p.srcPath = srcPaths[0]
return p.srcPath
}
for _, srcPath := range srcPaths {
if filepathHasPrefix(p.absPath, srcPath) {
2017-04-29 10:02:02 +00:00
p.srcPath = srcPath
break
}
}
return p.srcPath
}
func filepathHasPrefix(path string, prefix string) bool {
if len(path) <= len(prefix) {
return false
}
if runtime.GOOS == "windows" {
// Paths in windows are case-insensitive.
return strings.EqualFold(path[0:len(prefix)], prefix)
}
return path[0:len(prefix)] == prefix
}