| 114 | } |
| 115 | |
| 116 | func completeAgentFilename(toComplete string) ([]string, cobra.ShellCompDirective) { |
| 117 | dirPrefix, base := filepath.Split(toComplete) |
| 118 | |
| 119 | dirToRead := dirPrefix |
| 120 | if dirToRead == "" { |
| 121 | dirToRead = "." |
| 122 | } |
| 123 | |
| 124 | entries, err := os.ReadDir(dirToRead) |
| 125 | if err != nil { |
| 126 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 127 | } |
| 128 | |
| 129 | var out []string |
| 130 | for _, e := range entries { |
| 131 | name := e.Name() |
| 132 | if !strings.HasPrefix(name, base) { |
| 133 | continue |
| 134 | } |
| 135 | |
| 136 | switch { |
| 137 | case e.IsDir(): |
| 138 | out = append(out, dirPrefix+name+string(filepath.Separator)) |
| 139 | case strings.EqualFold(filepath.Ext(name), ".yaml"), strings.EqualFold(filepath.Ext(name), ".yml"): |
| 140 | out = append(out, dirPrefix+name) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Don't add space after single directory completion |
| 145 | if len(out) == 1 && strings.HasSuffix(out[0], string(filepath.Separator)) { |
| 146 | return out, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace |
| 147 | } |
| 148 | |
| 149 | return out, cobra.ShellCompDirectiveNoFileComp |
| 150 | } |