| 89 | } |
| 90 | |
| 91 | func CanonizeExecutablePath(name, workDir, pathVar, homeDir string) (canonized string, err error) { |
| 92 | if len(name) == 0 { |
| 93 | err = ErrAppPathIsEmpty |
| 94 | return |
| 95 | } |
| 96 | if homeDir != "" && !filepath.IsAbs(homeDir) { |
| 97 | err = fmt.Errorf("home directory must be absolute: %q", workDir) |
| 98 | return |
| 99 | } |
| 100 | if !filepath.IsAbs(workDir) { |
| 101 | err = fmt.Errorf("directory must be absolute: %q", workDir) |
| 102 | return |
| 103 | } |
| 104 | if !strings.ContainsRune(name, os.PathSeparator) { |
| 105 | canonized, err = util.FindExecutable(name, workDir, pathVar) |
| 106 | return |
| 107 | } |
| 108 | |
| 109 | if filepath.IsAbs(name) { |
| 110 | canonized = name |
| 111 | } else if strings.HasPrefix(name, "~/") { |
| 112 | if homeDir == "" { |
| 113 | err = fmt.Errorf("cannot expand ~: home directory is not specified") |
| 114 | return |
| 115 | } |
| 116 | canonized = filepath.Join(homeDir, strings.TrimPrefix(name, "~/")) |
| 117 | } else { |
| 118 | canonized = filepath.Join(workDir, name) |
| 119 | } |
| 120 | canonized = filepath.Clean(canonized) |
| 121 | return |
| 122 | } |
| 123 | |
| 124 | func IsCommandMatchingContext(command []string, context FlagContext) bool { |
| 125 | subCommand := context.SubCommand |