Add local files to the completions being built up
(toComplete string, result cobra.ShellCompDirective, completions []string)
| 35 | |
| 36 | // Add local files to the completions being built up |
| 37 | func addLocalFiles(toComplete string, result cobra.ShellCompDirective, completions []string) (cobra.ShellCompDirective, []string) { |
| 38 | path := filepath.Clean(toComplete) |
| 39 | dir, file := filepath.Split(path) |
| 40 | if dir == "" { |
| 41 | dir = "." |
| 42 | } |
| 43 | if len(dir) > 0 && dir[0] != filepath.Separator && dir[0] != '/' { |
| 44 | dir = strings.TrimRight(dir, string(filepath.Separator)) |
| 45 | dir = strings.TrimRight(dir, "/") |
| 46 | } |
| 47 | fi, err := os.Stat(toComplete) |
| 48 | if err == nil { |
| 49 | if fi.IsDir() { |
| 50 | dir = toComplete |
| 51 | file = "" |
| 52 | } |
| 53 | } |
| 54 | fis, err := os.ReadDir(dir) |
| 55 | if err != nil { |
| 56 | compLogf("Failed to read directory %q: %v", dir, err) |
| 57 | return result, completions |
| 58 | } |
| 59 | for _, fi := range fis { |
| 60 | name := fi.Name() |
| 61 | if strings.HasPrefix(name, file) { |
| 62 | path := filepath.Join(dir, name) |
| 63 | if fi.IsDir() { |
| 64 | path += string(filepath.Separator) |
| 65 | result |= cobra.ShellCompDirectiveNoSpace |
| 66 | } |
| 67 | completions = append(completions, path) |
| 68 | } |
| 69 | } |
| 70 | return result, completions |
| 71 | } |
| 72 | |
| 73 | // Add remote files to the completions being built up |
| 74 | func addRemoteFiles(toComplete string, result cobra.ShellCompDirective, completions []string) (cobra.ShellCompDirective, []string) { |