@@ -11,6 +11,7 @@ import (
1111 "github.com/docker/docker/api/types/container"
1212 "github.com/docker/docker/api/types/filters"
1313 dockerClient "github.com/docker/docker/client"
14+ "github.com/gosimple/slug"
1415
1516 "github.com/arduino/arduino-app-cli/pkg/parser"
1617)
@@ -45,9 +46,12 @@ func dockerComposeAppStatus(ctx context.Context, app parser.App) (DockerComposeA
4546 if err != nil {
4647 return DockerComposeAppStatusResponse {}, err
4748 }
48- composeName := app .FullPath .Base ()
49+ composeProjectName , err := getAppComposeProjectNameFromApp (app )
50+ if err != nil {
51+ return DockerComposeAppStatusResponse {}, err
52+ }
4953
50- process , err := paths .NewProcess (nil , "docker" , "compose" , "-f" , mainCompose .String (), "ls" , "--format" , "json" , "--all" , "--filter" , fmt .Sprintf ("name=%s" , composeName ))
54+ process , err := paths .NewProcess (nil , "docker" , "compose" , "-f" , mainCompose .String (), "ls" , "--format" , "json" , "--all" , "--filter" , fmt .Sprintf ("name=%s" , composeProjectName ))
5155 if err != nil {
5256 return DockerComposeAppStatusResponse {}, err
5357 }
@@ -67,17 +71,27 @@ func dockerComposeAppStatus(ctx context.Context, app parser.App) (DockerComposeA
6771 if len (statusResponse ) == 0 {
6872 return DockerComposeAppStatusResponse {}, fmt .Errorf ("failed to find app status in docker compose response" )
6973 }
70- // We only want the first response, as we are filtering by name
71- resp := statusResponse [0 ]
74+ // It is possible that the --filter returns multiple items as it's not an exact match
75+ var match DockerComposeAppStatusResponse
76+ for _ , v := range statusResponse {
77+ if v .Name == composeProjectName {
78+ match = v
79+ break
80+ }
81+ }
82+
83+ if match == (DockerComposeAppStatusResponse {}) {
84+ return DockerComposeAppStatusResponse {}, fmt .Errorf ("failed to find app status in docker compose response" )
85+ }
7286
7387 // The response from compose is in the form of "state(number_services)". Example: "running(2)"
7488 // We only want the state, so we remove the number of services
75- idx := strings .Index (resp .Status , "(" )
89+ idx := strings .Index (match .Status , "(" )
7690 if idx != - 1 {
77- resp .Status = resp .Status [:idx ]
91+ match .Status = match .Status [:idx ]
7892 }
7993
80- return resp , nil
94+ return match , nil
8195}
8296
8397func getRunningApp (ctx context.Context , docker * dockerClient.Client ) (* parser.App , error ) {
@@ -128,3 +142,11 @@ func getRunningApp(ctx context.Context, docker *dockerClient.Client) (*parser.Ap
128142 }
129143 return nil , nil
130144}
145+
146+ func getAppComposeProjectNameFromApp (app parser.App ) (string , error ) {
147+ composeProjectName , err := app .FullPath .RelFrom (orchestratorConfig .AppsDir ())
148+ if err != nil {
149+ return "" , fmt .Errorf ("failed to get compose project name: %w" , err )
150+ }
151+ return slug .Make (composeProjectName .String ()), nil
152+ }
0 commit comments