use cwd to find container

This commit is contained in:
Luna 2025-06-10 22:26:39 -03:00
parent ed8a6f2d81
commit c59527c8d8

View file

@ -8,6 +8,39 @@ import (
"strings" "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 { func initCommand(lang string) error {
if lang == "" { if lang == "" {
fmt.Println("Building base hako image...") fmt.Println("Building base hako image...")
@ -80,16 +113,12 @@ func downCommand(container string) error {
if container != "" { if container != "" {
containerName = container containerName = container
} else { } else {
// Find any hako container for current project // Find the appropriate hako container for current directory
output, err := runCommandOutput("docker", "ps", "-a", "--filter", "name=hako-", "--format", "{{.Names}}") var err error
containerName, err = getContainerForCurrentDirectory()
if err != nil { 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) { if !containerExists(containerName) {
@ -130,16 +159,11 @@ func psCommand() error {
} }
func syncCommand() error { func syncCommand() error {
// TODO use the container for the currently working directory instead of just the first one in the filter... // Find the appropriate hako container for current directory
output, err := runCommandOutput("docker", "ps", "-a", "--filter", "name=hako-", "--format", "{{.Names}}") containerName, err := getContainerForCurrentDirectory()
if err != nil { 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) { if !containerExists(containerName) {
return fmt.Errorf("container %s does not exist", containerName) return fmt.Errorf("container %s does not exist", containerName)