e973b2ce20
- Implemented `pcli project export` command to export project hierarchy as JSON. - Added `pcli project import` command to import project data from JSON. - Created user client to fetch user details for comment attribution. - Introduced new data structures for export and import processes. - Ensured name-based references in exports and handled conflicts during imports. - Added versioning and progress reporting for import operations. - Updated documentation and specifications for new features.
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// resolveProject takes a project ID or name and returns the project ID.
|
|
// If the input matches a project ID exactly, it's returned directly.
|
|
// If it matches project name(s), it returns the ID if exactly one matches.
|
|
func resolveProject(idOrName string) (string, error) {
|
|
// First try as an ID
|
|
project, err := getClient().GetProject(getContext(), idOrName)
|
|
if err == nil {
|
|
return project.ID, nil
|
|
}
|
|
|
|
// Fall back to name lookup
|
|
projects, err := getClient().ListProjects(getContext())
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to list projects: %w", err)
|
|
}
|
|
|
|
var matches []struct{ id, name string }
|
|
for _, p := range projects {
|
|
if strings.EqualFold(p.Name, idOrName) {
|
|
matches = append(matches, struct{ id, name string }{p.ID, p.Name})
|
|
}
|
|
}
|
|
|
|
switch len(matches) {
|
|
case 0:
|
|
return "", fmt.Errorf("project %q not found", idOrName)
|
|
case 1:
|
|
return matches[0].id, nil
|
|
default:
|
|
var names []string
|
|
for _, m := range matches {
|
|
names = append(names, fmt.Sprintf("%s (%s)", m.name, m.id))
|
|
}
|
|
return "", fmt.Errorf("ambiguous project name %q matches multiple projects: %s", idOrName, strings.Join(names, ", "))
|
|
}
|
|
}
|