| 333 | } |
| 334 | |
| 335 | func (t *treeCmd) completion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 336 | var suggestions []string |
| 337 | |
| 338 | // Support port completion from global ports. |
| 339 | if fileio.PathExists(dirs.PortsDir) { |
| 340 | filepath.WalkDir(dirs.PortsDir, func(path string, d fs.DirEntry, err error) error { |
| 341 | if err != nil { |
| 342 | return err |
| 343 | } |
| 344 | |
| 345 | if !d.IsDir() && strings.HasSuffix(d.Name(), ".toml") { |
| 346 | // For example: ports/t/testlib/1.0.0/port.toml |
| 347 | portDir := filepath.Dir(path) // ports/t/testlib/1.0.0 |
| 348 | libVersion := filepath.Base(portDir) // 1.0.0 |
| 349 | libName := filepath.Base(filepath.Dir(portDir)) // testlib |
| 350 | nameVersion := libName + "@" + libVersion |
| 351 | |
| 352 | if strings.HasPrefix(nameVersion, toComplete) { |
| 353 | suggestions = append(suggestions, nameVersion) |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return nil |
| 358 | }) |
| 359 | } |
| 360 | |
| 361 | // Support port completion from project-specific ports. |
| 362 | projectPortsDir := filepath.Join(dirs.ConfProjectsDir, t.celer.Project().GetName()) |
| 363 | if fileio.PathExists(projectPortsDir) { |
| 364 | filepath.WalkDir(projectPortsDir, func(path string, d fs.DirEntry, err error) error { |
| 365 | if err != nil { |
| 366 | return err |
| 367 | } |
| 368 | |
| 369 | if !d.IsDir() && strings.HasSuffix(d.Name(), ".toml") { |
| 370 | libName := filepath.Base(filepath.Dir(filepath.Dir(path))) |
| 371 | libVersion := filepath.Base(filepath.Dir(path)) |
| 372 | nameVersion := libName + "@" + libVersion |
| 373 | |
| 374 | if strings.HasPrefix(nameVersion, toComplete) { |
| 375 | suggestions = append(suggestions, nameVersion) |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return nil |
| 380 | }) |
| 381 | } |
| 382 | |
| 383 | // Support project completion. |
| 384 | if fileio.PathExists(dirs.ConfProjectsDir) { |
| 385 | entities, err := os.ReadDir(dirs.ConfProjectsDir) |
| 386 | if err != nil { |
| 387 | return suggestions, cobra.ShellCompDirectiveNoFileComp |
| 388 | } |
| 389 | |
| 390 | for _, entity := range entities { |
| 391 | if !entity.IsDir() && strings.HasSuffix(entity.Name(), ".toml") { |
| 392 | fileName := strings.TrimSuffix(entity.Name(), ".toml") |