2025-11-28 08:33:50 +02:00
package cli
import (
2026-05-14 08:45:20 -04:00
"fmt"
"strings"
2025-11-28 08:33:50 +02:00
"golang.org/x/xerrors"
"github.com/coder/coder/v2/agent/agentsocket"
"github.com/coder/coder/v2/agent/unit"
"github.com/coder/coder/v2/cli/cliui"
2026-01-08 15:24:11 +04:00
"github.com/coder/serpent"
2025-11-28 08:33:50 +02:00
)
func ( * RootCmd ) syncWant ( socketPath * string ) * serpent . Command {
cmd := & serpent . Command {
2026-04-01 11:55:32 -04:00
Use : "want <unit> <depends-on> [depends-on...]" ,
Short : "Declare that a unit depends on other units completing before it can start" ,
Long : "Declare that a unit depends on one or more other units completing before it can start. The unit specified first will not start until all subsequent units have signaled that they have completed." ,
2025-11-28 08:33:50 +02:00
Handler : func ( i * serpent . Invocation ) error {
ctx := i . Context ()
2026-04-01 11:55:32 -04:00
if len ( i . Args ) < 2 {
return xerrors . New ( "at least two arguments are required: unit and one or more depends-on" )
2025-11-28 08:33:50 +02:00
}
dependentUnit := unit . ID ( i . Args [ 0 ])
opts := [] agentsocket . Option {}
if * socketPath != "" {
opts = append ( opts , agentsocket . WithPath ( * socketPath ))
}
client , err := agentsocket . NewClient ( ctx , opts ... )
if err != nil {
return xerrors . Errorf ( "connect to agent socket: %w" , err )
}
defer client . Close ()
2026-04-01 11:55:32 -04:00
for _ , dep := range i . Args [ 1 :] {
if err := client . SyncWant ( ctx , dependentUnit , unit . ID ( dep )); err != nil {
return xerrors . Errorf ( "declare dependency failed: %w" , err )
}
2025-11-28 08:33:50 +02:00
}
2026-05-14 08:45:20 -04:00
cliui . Info ( i . Stdout , fmt . Sprintf ( "Unit %q declared dependencies: [%s]" , dependentUnit , strings . Join ( i . Args [ 1 :], ", " )))
2025-11-28 08:33:50 +02:00
return nil
},
}
return cmd
}