Skip to content

Commit da8237e

Browse files
committed
Initial services attach functionality
1 parent df187b9 commit da8237e

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

internal/boxcli/services.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ func servicesCmd(persistentPreRunE ...cobraFunc) *cobra.Command {
6969
},
7070
}
7171

72+
attachCommand := &cobra.Command{
73+
Use: "attach",
74+
Short: "Attach to a running process-compose for the current project",
75+
Args: cobra.ExactArgs(0),
76+
RunE: func(cmd *cobra.Command, args []string) error {
77+
return attachServices(cmd, flags)
78+
},
79+
}
80+
7281
lsCommand := &cobra.Command{
7382
Use: "ls",
7483
Short: "List available services",
@@ -123,6 +132,7 @@ func servicesCmd(persistentPreRunE ...cobraFunc) *cobra.Command {
123132
servicesCommand.Flag("run-in-current-shell").Hidden = true
124133
serviceUpFlags.register(upCommand)
125134
serviceStopFlags.register(stopCommand)
135+
servicesCommand.AddCommand(attachCommand)
126136
servicesCommand.AddCommand(lsCommand)
127137
servicesCommand.AddCommand(upCommand)
128138
servicesCommand.AddCommand(restartCommand)
@@ -131,6 +141,20 @@ func servicesCmd(persistentPreRunE ...cobraFunc) *cobra.Command {
131141
return servicesCommand
132142
}
133143

144+
func attachServices(cmd *cobra.Command, flags servicesCmdFlags) error {
145+
146+
box, err := devbox.Open(&devopt.Opts{
147+
Dir: flags.config.path,
148+
Environment: flags.config.environment,
149+
Stderr: cmd.ErrOrStderr(),
150+
})
151+
if err != nil {
152+
return errors.WithStack(err)
153+
}
154+
155+
return box.AttachToProcessManager(cmd.Context())
156+
}
157+
134158
func listServices(cmd *cobra.Command, flags servicesCmdFlags) error {
135159
box, err := devbox.Open(&devopt.Opts{
136160
Dir: flags.config.path,

internal/devbox/services.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,32 @@ func (d *Devbox) RestartServices(
170170
return nil
171171
}
172172

173+
func (d *Devbox) AttachToProcessManager(ctx context.Context) error {
174+
if !services.ProcessManagerIsRunning(d.projectDir) {
175+
return usererr.New("Process manager is not running. Run `devbox services up` to start it.")
176+
}
177+
178+
err := initDevboxUtilityProject(ctx, d.stderr)
179+
if err != nil {
180+
return err
181+
}
182+
183+
processComposeBinPath, err := utilityLookPath("process-compose")
184+
if err != nil {
185+
return err
186+
}
187+
188+
return services.AttachToProcessManager(
189+
ctx,
190+
d.stderr,
191+
d.projectDir,
192+
services.ProcessComposeOpts{
193+
BinPath: processComposeBinPath,
194+
},
195+
)
196+
197+
}
198+
173199
func (d *Devbox) StartProcessManager(
174200
ctx context.Context,
175201
runInCurrentShell bool,

internal/services/manager.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,30 @@ func StopAllProcessManagers(ctx context.Context, w io.Writer) error {
293293
return nil
294294
}
295295

296+
func AttachToProcessManager(ctx context.Context, w io.Writer, projectDir string, processComposeConfig ProcessComposeOpts) error {
297+
configFile, err := openGlobalConfigFile()
298+
if err != nil {
299+
return err
300+
}
301+
302+
defer configFile.Close()
303+
config := readGlobalProcessComposeJSON(configFile)
304+
305+
project, ok := config.Instances[projectDir]
306+
if !ok {
307+
return fmt.Errorf("Process-compose is not running for this project. To start it, run `devbox services up`")
308+
}
309+
310+
flags := []string{"attach", "-p", strconv.Itoa(project.Port)}
311+
cmd := exec.Command(processComposeConfig.BinPath, flags...)
312+
313+
cmd.Stdout = os.Stdout
314+
cmd.Stderr = os.Stderr
315+
cmd.Stdin = os.Stdin
316+
317+
return cmd.Run()
318+
}
319+
296320
func ProcessManagerIsRunning(projectDir string) bool {
297321
configFile, err := openGlobalConfigFile()
298322
if err != nil {

0 commit comments

Comments
 (0)