FormatPathFromComplexString search for file/directory path and remove environments variables, quotes and extra parameters
(command string)
| 117 | |
| 118 | // FormatPathFromComplexString search for file/directory path and remove environments variables, quotes and extra parameters |
| 119 | func FormatPathFromComplexString(command string) (paths []string) { |
| 120 | var buffer []string |
| 121 | |
| 122 | // quoted path |
| 123 | if strings.Contains(command, `"`) || strings.Contains(command, `'`) { |
| 124 | re := regexp.MustCompile(`[\'\"](.+)[\'\"]`) |
| 125 | matches := re.FindStringSubmatch(command) |
| 126 | for i := range matches { |
| 127 | if i != 0 { |
| 128 | buffer = append(buffer, matches[i]) |
| 129 | } |
| 130 | } |
| 131 | } else { |
| 132 | for _, i := range strings.Split(strings.Replace(command, ",", "", -1), " ") { |
| 133 | buffer = append(buffer, i) |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |
| 138 | for _, item := range buffer { |
| 139 | // environment variables |
| 140 | if strings.Contains(command, `%`) { |
| 141 | re := regexp.MustCompile(`%(\w+)%`) |
| 142 | res := re.FindStringSubmatch(item) |
| 143 | for i := range res { |
| 144 | item = strings.Replace(item, "%"+res[i]+"%", os.Getenv(res[i]), -1) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // check if file exists |
| 149 | if _, err := os.Stat(item); !os.IsNotExist(err) { |
| 150 | paths = append(paths, item) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return paths |
| 155 | } |
| 156 | |
| 157 | // RetrivesFilesFromUserPath return a []string of available files from given path (includeFileExtensions is available only if listFiles is true) |
| 158 | func RetrivesFilesFromUserPath(path string, listFiles bool, includeFileExtensions []string, recursive bool, verbose bool) ([]string, error) { |
no outgoing calls
no test coverage detected