From c59527c8d81ebae1cd2fe589292eb85cb07b9b11 Mon Sep 17 00:00:00 2001 From: Luna Date: Tue, 10 Jun 2025 22:26:39 -0300 Subject: [PATCH] use cwd to find container --- commands.go | 56 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/commands.go b/commands.go index 3fc368b..6bdb5ba 100644 --- a/commands.go +++ b/commands.go @@ -8,6 +8,39 @@ import ( "strings" ) +func getContainerForCurrentDirectory() (string, error) { + // Get all hako containers + output, err := runCommandOutput("docker", "ps", "-a", "--filter", "name=hako-", "--format", "{{.Names}}") + if err != nil { + return "", err + } + + containers := strings.Split(strings.TrimSpace(output), "\n") + if len(containers) == 0 || containers[0] == "" { + return "", fmt.Errorf("no hako containers found") + } + + // Try to find containers that match any supported language for the current directory + supportedLangs := []string{"go", "py", "python"} + + for _, lang := range supportedLangs { + expectedName, err := getProjectContainerName(lang) + if err != nil { + continue // Skip if we can't generate a name for this language + } + + // Check if this expected container name exists in our list + for _, container := range containers { + if strings.TrimSpace(container) == expectedName { + return expectedName, nil + } + } + } + + // If no exact match found, return the first container as fallback + return strings.TrimSpace(containers[0]), nil +} + func initCommand(lang string) error { if lang == "" { fmt.Println("Building base hako image...") @@ -80,16 +113,12 @@ func downCommand(container string) error { if container != "" { containerName = container } else { - // Find any hako container for current project - output, err := runCommandOutput("docker", "ps", "-a", "--filter", "name=hako-", "--format", "{{.Names}}") + // Find the appropriate hako container for current directory + var err error + containerName, err = getContainerForCurrentDirectory() if err != nil { - return err + return fmt.Errorf("no hako containers found for current project: %v", err) } - containers := strings.Split(strings.TrimSpace(output), "\n") - if len(containers) == 0 || containers[0] == "" { - return fmt.Errorf("no hako containers found for current project") - } - containerName = containers[0] // Use first found container } if !containerExists(containerName) { @@ -130,16 +159,11 @@ func psCommand() error { } func syncCommand() error { - // TODO use the container for the currently working directory instead of just the first one in the filter... - output, err := runCommandOutput("docker", "ps", "-a", "--filter", "name=hako-", "--format", "{{.Names}}") + // Find the appropriate hako container for current directory + containerName, err := getContainerForCurrentDirectory() if err != nil { - return err + return fmt.Errorf("no hako containers found for current project: %v", err) } - containers := strings.Split(strings.TrimSpace(output), "\n") - if len(containers) == 0 || containers[0] == "" { - return fmt.Errorf("no hako containers found for current project") - } - containerName := containers[0] // Use first found container if !containerExists(containerName) { return fmt.Errorf("container %s does not exist", containerName)