MCPcopy
hub / github.com/dagger/container-use / resolveEnvironmentID

Function resolveEnvironmentID

cmd/container-use/env_selection.go:17–49  ·  view source on GitHub ↗

resolveEnvironmentID resolves the environment ID for commands that take env_id as the only positional argument. If no args are provided, it filters environments to those where the local repo head is a parent of the environment's head, then either auto-selects if there's only one match or prompts the

(ctx context.Context, repo *repository.Repository, args []string)

Source from the content-addressed store, hash-verified

15// If no args are provided, it filters environments to those where the local repo head is a parent of the environment's head,
16// then either auto-selects if there's only one match or prompts the user to select from multiple options.
17func resolveEnvironmentID(ctx context.Context, repo *repository.Repository, args []string) (string, error) {
18 if len(args) == 1 {
19 return args[0], nil
20 }
21 if len(args) > 1 {
22 return "", errors.New("too many arguments")
23 }
24
25 // Get current user repo head - this could easily go inside ListDescendantEnvironments, but keeping it outside simplifies testing
26 currentHead, err := repository.RunGitCommand(ctx, repo.SourcePath(), "rev-parse", "HEAD")
27 if err != nil {
28 return "", fmt.Errorf("failed to get current HEAD: %w", err)
29 }
30 currentHead = strings.TrimSpace(currentHead)
31
32 // Get environments that are descendants of current HEAD
33 filteredEnvs, err := repo.ListDescendantEnvironments(ctx, currentHead)
34 if err != nil {
35 return "", fmt.Errorf("failed to list descendant environments: %w", err)
36 }
37
38 if len(filteredEnvs) == 0 {
39 return "", errors.New("no environments found that are descendants of the current HEAD")
40 }
41
42 // If only one environment matches, use it
43 if len(filteredEnvs) == 1 {
44 return filteredEnvs[0].ID, nil
45 }
46
47 // Multiple environments - prompt user to select
48 return promptForEnvironmentSelection(filteredEnvs)
49}
50
51// promptForEnvironmentSelection prompts the user to select from multiple environments
52func promptForEnvironmentSelection(envs []*environment.EnvironmentInfo) (string, error) {

Callers 8

inspect.goFile · 0.85
apply.goFile · 0.85
diff.goFile · 0.85
TestResolveEnvironmentIDFunction · 0.85
merge.goFile · 0.85
checkout.goFile · 0.85
log.goFile · 0.85
terminal.goFile · 0.85

Calls 4

RunGitCommandFunction · 0.92
SourcePathMethod · 0.80

Tested by 1

TestResolveEnvironmentIDFunction · 0.68