InferProjectName infers a project name from the given path. If multiple projects are found, it prompts the user to select one (or errors in non-interactive mode). The hint (e.g. "use --project to specify the name") is appended to error messages.
(ctx context.Context, pathToProject, hint string)
| 364 | // If multiple projects are found, it prompts the user to select one (or errors in non-interactive mode). |
| 365 | // The hint (e.g. "use --project to specify the name") is appended to error messages. |
| 366 | func (h *Helper) InferProjectName(ctx context.Context, pathToProject, hint string) (string, error) { |
| 367 | errorfWithHint := func(format string, a ...any) error { |
| 368 | if hint != "" { |
| 369 | return fmt.Errorf(format+" (%s)", append(append([]any{}, a...), hint)...) |
| 370 | } |
| 371 | return fmt.Errorf(format, a...) |
| 372 | } |
| 373 | |
| 374 | projects, err := h.InferProjects(ctx, h.Org, pathToProject) |
| 375 | if err != nil { |
| 376 | if errors.Is(err, ErrInferProjectFailed) { |
| 377 | return "", errorfWithHint("%w", err) |
| 378 | } |
| 379 | return "", errorfWithHint("failed to infer project: %w", err) |
| 380 | } |
| 381 | if len(projects) == 1 { |
| 382 | return projects[0].Name, nil |
| 383 | } |
| 384 | |
| 385 | if !h.Interactive { |
| 386 | return "", errorfWithHint("multiple projects match the current directory; you must explicitly specify a project") |
| 387 | } |
| 388 | |
| 389 | var names []string |
| 390 | for _, p := range projects { |
| 391 | names = append(names, p.Name) |
| 392 | } |
| 393 | return SelectPrompt("Select project", names, "") |
| 394 | } |
| 395 | |
| 396 | func (h *Helper) InferProjects(ctx context.Context, org, path string) ([]*adminv1.Project, error) { |
| 397 | path, err := fileutil.ExpandHome(path) |
no test coverage detected