Skip to content
Myr

cmd: Use correct HasPrefix function for paths

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

Updates #448

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent e9078fc · Browse files · 3 files+25 added−10 removed
This commit

State

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

Diff stats

3 files+25 added−10 removed

  • other:+25 added−10 removed

3 files+25 added−10 removed

modified:cobra/cmd/project_test.go+1 added-2 removed
1package cmd23import (4 "strings"4 "testing"5)67func TestFindExistingPackage(t *testing.T) {
7 unmodified lines
15}1617func hasGoPathPrefix(path string) bool {18 for _, srcPath := range srcPaths {20 if strings.HasPrefix(path, srcPath) {19 if filepathHasPrefix(path, srcPath) {20 return true21 }22 }23 return false
1 unmodified line
modified:cobra/cmd/project.go+15 added-2 removed
1 unmodified line
23import (4 "os"5 "path/filepath"6 "runtime"7 "strings"8)910// Project contains name, license and paths to projects.
26 unmodified lines
37 er(err)38 }39 for _, srcPath := range srcPaths {40 goPath := filepath.Dir(srcPath)40 if strings.HasPrefix(wd, goPath) {41 if filepathHasPrefix(wd, goPath) {42 p.absPath = filepath.Join(srcPath, projectName)43 break44 }45 }
126 unmodified lines
172 return p.srcPath173 }174175 for _, srcPath := range srcPaths {175 if strings.HasPrefix(p.absPath, srcPath) {176 if filepathHasPrefix(p.absPath, srcPath) {177 p.srcPath = srcPath178 break179 }180 }181182 return p.srcPath183}184185func filepathHasPrefix(path string, prefix string) bool {186 if len(path) <= len(prefix) {187 return false188 }189 if runtime.GOOS == "windows" {190 // Paths in windows are case-insensitive.191 return strings.EqualFold(path[0:len(prefix)], prefix)192 }193 return path[0:len(prefix)] == prefix194195}
modified:cobra/cmd/root.go+9 added-6 removed
19 unmodified lines
20 "github.com/spf13/cobra"21 "github.com/spf13/viper"22)2324var cfgFile, projectBase, userLicense string // are used for flags24var (25 // Used for flags.26 cfgFile, projectBase, userLicense string2726var rootCmd = &cobra.Command{27 Use: "cobra",28 Short: "A generator for Cobra based Applications",29 Long: `Cobra is a CLI library for Go that empowers applications.28 rootCmd = &cobra.Command{29 Use: "cobra",30 Short: "A generator for Cobra based Applications",31 Long: `Cobra is a CLI library for Go that empowers applications.32This application is a tool to generate the needed files33to quickly create a Cobra application.`,32}34 }35)3637// Execute executes the root command.38func Execute() {39 if err := rootCmd.Execute(); err != nil {
45 unmodified lines