Skip to content
Myr

vgo - strip out unused methods

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by jharshman · committed by Steve Francia · parent 0bb1506 · Browse files · 2 files+6 added−214 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+6 added−214 removed

  • other:+6 added−214 removed

2 files+6 added−214 removed

modified:cobra/cmd/project_test.go+1 added-22 removed
1package cmd23import (4 "testing"5)67func TestFindExistingPackage(t *testing.T) {8 path := findPackage("github.com/spf13/cobra")9 if path == "" {10 t.Fatal("findPackage didn't find the existing package")11 }12 if !hasGoPathPrefix(path) {13 t.Fatalf("%q is not in GOPATH, but must be", path)14 }15}1617func hasGoPathPrefix(path string) bool {18 for _, srcPath := range srcPaths {19 if filepathHasPrefix(path, srcPath) {20 return true21 }22 }23 return false24}3/* todo: write tests */
modified:cobra/cmd/project.go+5 added-192 removed
2 unmodified lines
3import (4 "fmt"5 "github.com/spf13/cobra/cobra/tpl"6 "os"7 "path/filepath"8 "runtime"9 "strings"7 "text/template"8)910// Project contains name, license and paths to projects.
5 unmodified lines
16 Legal License17 Viper bool18 AppName string1923 // v124 absPath string25 cmdPath string26 srcPath string27 license License28 name string20 //absPath string21 //cmdPath string22 //srcPath string23 //license License24 //name string25}2627type Command struct {28 CmdName string
70 unmodified lines
99 return err100 }101 return nil102}107108// NewProject returns Project with specified project name.109func NewProject(projectName string) *Project {110 if projectName == "" {111 er("can't create project with blank name")112 }113114 p := new(Project)115 p.name = projectName116117 // 1. Find already created protect.118 p.absPath = findPackage(projectName)119120 // 2. If there are no created project with this path, and user is in GOPATH,121 // then use GOPATH/src/projectName.122 if p.absPath == "" {123 wd, err := os.Getwd()124 if err != nil {125 er(err)126 }127 for _, srcPath := range srcPaths {128 goPath := filepath.Dir(srcPath)129 if filepathHasPrefix(wd, goPath) {130 p.absPath = filepath.Join(srcPath, projectName)131 break132 }133 }134 }135136 // 3. If user is not in GOPATH, then use (first GOPATH)/src/projectName.137 if p.absPath == "" {138 p.absPath = filepath.Join(srcPaths[0], projectName)139 }140141 return p142}143144// findPackage returns full path to existing go package in GOPATHs.145func findPackage(packageName string) string {146 if packageName == "" {147 return ""148 }149150 for _, srcPath := range srcPaths {151 packagePath := filepath.Join(srcPath, packageName)152 if exists(packagePath) {153 return packagePath154 }155 }156157 return ""158}159160// NewProjectFromPath returns Project with specified absolute path to161// package.162func NewProjectFromPath(absPath string) *Project {163 if absPath == "" {164 er("can't create project: absPath can't be blank")165 }166 if !filepath.IsAbs(absPath) {167 er("can't create project: absPath is not absolute")168 }169170 // If absPath is symlink, use its destination.171 fi, err := os.Lstat(absPath)172 if err != nil {173 er("can't read path info: " + err.Error())174 }175 if fi.Mode()&os.ModeSymlink != 0 {176 path, err := os.Readlink(absPath)177 if err != nil {178 er("can't read the destination of symlink: " + err.Error())179 }180 absPath = path181 }182183 p := new(Project)184 p.absPath = strings.TrimSuffix(absPath, findCmdDir(absPath))185 p.name = filepath.ToSlash(trimSrcPath(p.absPath, p.SrcPath()))186 return p187}188189// trimSrcPath trims at the beginning of absPath the srcPath.190func trimSrcPath(absPath, srcPath string) string {191 relPath, err := filepath.Rel(srcPath, absPath)192 if err != nil {193 er(err)194 }195 return relPath196}197198// License returns the License object of project.199func (p *Project) License() License {200 if p.license.Text == "" && p.license.Name != "None" {201 p.license = getLicense()202 }203 return p.license204}205206// Name returns the name of project, e.g. "github.com/spf13/cobra"207func (p Project) Name() string {208 return p.name209}210211// CmdPath returns absolute path to directory, where all commands are located.212func (p *Project) CmdPath() string {213 if p.absPath == "" {214 return ""215 }216 if p.cmdPath == "" {217 p.cmdPath = filepath.Join(p.absPath, findCmdDir(p.absPath))218 }219 return p.cmdPath220}221222// findCmdDir checks if base of absPath is cmd dir and returns it or223// looks for existing cmd dir in absPath.224func findCmdDir(absPath string) string {225 if !exists(absPath) || isEmpty(absPath) {226 return "cmd"227 }228229 if isCmdDir(absPath) {230 return filepath.Base(absPath)231 }232233 files, _ := filepath.Glob(filepath.Join(absPath, "c*"))234 for _, file := range files {235 if isCmdDir(file) {236 return filepath.Base(file)237 }238 }239240 return "cmd"241}242243// isCmdDir checks if base of name is one of cmdDir.244func isCmdDir(name string) bool {245 name = filepath.Base(name)246 for _, cmdDir := range []string{"cmd", "cmds", "command", "commands"} {247 if name == cmdDir {248 return true249 }250 }251 return false252}253254// AbsPath returns absolute path of project.255func (p Project) AbsPath() string {256 return p.absPath257}258259// SrcPath returns absolute path to $GOPATH/src where project is located.260func (p *Project) SrcPath() string {261 if p.srcPath != "" {262 return p.srcPath263 }264 if p.absPath == "" {265 p.srcPath = srcPaths[0]266 return p.srcPath267 }268269 for _, srcPath := range srcPaths {270 if filepathHasPrefix(p.absPath, srcPath) {271 p.srcPath = srcPath272 break273 }274 }275276 return p.srcPath277}278279func filepathHasPrefix(path string, prefix string) bool {280 if len(path) <= len(prefix) {281 return false282 }283 if runtime.GOOS == "windows" {284 // Paths in windows are case-insensitive.285 return strings.EqualFold(path[0:len(prefix)], prefix)286 }287 return path[0:len(prefix)] == prefix288289}