Skip to content

Commit de5bb79

Browse files
authored
[Devbox services] move functions to services.go, and improve runDevboxServicesScript (#2164)
## Summary 1. Moves relevant methods from `devbox.go` to `services.go`. Just to organize the code a bit better. 2. Improve `runDevboxServicesScript` to have the `services` sub-command in `devbox services` be internally set. ## How was it tested? CICD should pass
1 parent 1573ae8 commit de5bb79

File tree

2 files changed

+244
-239
lines changed

2 files changed

+244
-239
lines changed

internal/devbox/devbox.go

Lines changed: 0 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"slices"
1919
"strconv"
2020
"strings"
21-
"text/tabwriter"
2221
"time"
2322

2423
"github.com/briandowns/spinner"
@@ -230,13 +229,6 @@ func (d *Devbox) Shell(ctx context.Context, envOpts devopt.EnvOptions) error {
230229
return shell.Run()
231230
}
232231

233-
// runDevboxServicesScript invokes RunScript with the envOptions set to the appropriate
234-
// defaults for the `devbox services` scenario.
235-
// TODO: move to services.go
236-
func (d *Devbox) runDevboxServicesScript(ctx context.Context, cmdArgs []string) error {
237-
return d.RunScript(ctx, devopt.EnvOptions{}, "devbox", cmdArgs)
238-
}
239-
240232
func (d *Devbox) RunScript(ctx context.Context, envOpts devopt.EnvOptions, cmdName string, cmdArgs []string) error {
241233
ctx, task := trace.NewTask(ctx, "devboxRun")
242234
defer task.End()
@@ -579,237 +571,6 @@ func (d *Devbox) Services() (services.Services, error) {
579571
return result, nil
580572
}
581573

582-
// TODO: move to services.go
583-
func (d *Devbox) StartServices(
584-
ctx context.Context, runInCurrentShell bool, serviceNames ...string,
585-
) error {
586-
if !runInCurrentShell {
587-
return d.runDevboxServicesScript(ctx,
588-
append(
589-
[]string{"services", "start", "--run-in-current-shell"},
590-
serviceNames...,
591-
),
592-
)
593-
}
594-
595-
if !services.ProcessManagerIsRunning(d.projectDir) {
596-
fmt.Fprintln(d.stderr, "Process-compose is not running. Starting it now...")
597-
fmt.Fprintln(d.stderr, "\nNOTE: We recommend using `devbox services up` to start process-compose and your services")
598-
return d.StartProcessManager(ctx, runInCurrentShell, serviceNames, devopt.ProcessComposeOpts{Background: true})
599-
}
600-
601-
svcSet, err := d.Services()
602-
if err != nil {
603-
return err
604-
}
605-
606-
if len(svcSet) == 0 {
607-
return usererr.New("No services found in your project")
608-
}
609-
610-
for _, s := range serviceNames {
611-
if _, ok := svcSet[s]; !ok {
612-
return usererr.New(fmt.Sprintf("Service %s not found in your project", s))
613-
}
614-
}
615-
616-
for _, s := range serviceNames {
617-
err := services.StartServices(ctx, d.stderr, s, d.projectDir)
618-
if err != nil {
619-
fmt.Fprintf(d.stderr, "Error starting service %s: %s", s, err)
620-
} else {
621-
fmt.Fprintf(d.stderr, "Service %s started successfully", s)
622-
}
623-
}
624-
return nil
625-
}
626-
627-
// TODO: move to services.go
628-
func (d *Devbox) StopServices(ctx context.Context, runInCurrentShell, allProjects bool, serviceNames ...string) error {
629-
if !runInCurrentShell {
630-
args := []string{"services", "stop", "--run-in-current-shell"}
631-
args = append(args, serviceNames...)
632-
if allProjects {
633-
args = append(args, "--all-projects")
634-
}
635-
return d.runDevboxServicesScript(ctx, args)
636-
}
637-
638-
if allProjects {
639-
return services.StopAllProcessManagers(ctx, d.stderr)
640-
}
641-
642-
if !services.ProcessManagerIsRunning(d.projectDir) {
643-
return usererr.New("Process manager is not running. Run `devbox services up` to start it.")
644-
}
645-
646-
if len(serviceNames) == 0 {
647-
return services.StopProcessManager(ctx, d.projectDir, d.stderr)
648-
}
649-
650-
svcSet, err := d.Services()
651-
if err != nil {
652-
return err
653-
}
654-
655-
for _, s := range serviceNames {
656-
if _, ok := svcSet[s]; !ok {
657-
return usererr.New(fmt.Sprintf("Service %s not found in your project", s))
658-
}
659-
err := services.StopServices(ctx, s, d.projectDir, d.stderr)
660-
if err != nil {
661-
fmt.Fprintf(d.stderr, "Error stopping service %s: %s", s, err)
662-
}
663-
}
664-
return nil
665-
}
666-
667-
// TODO: move to services.go
668-
func (d *Devbox) ListServices(ctx context.Context, runInCurrentShell bool) error {
669-
if !runInCurrentShell {
670-
return d.runDevboxServicesScript(ctx, []string{"services", "ls", "--run-in-current-shell"})
671-
}
672-
673-
svcSet, err := d.Services()
674-
if err != nil {
675-
return err
676-
}
677-
678-
if len(svcSet) == 0 {
679-
fmt.Fprintln(d.stderr, "No services found in your project")
680-
return nil
681-
}
682-
683-
if !services.ProcessManagerIsRunning(d.projectDir) {
684-
fmt.Fprintln(d.stderr, "No services currently running. Run `devbox services up` to start them:")
685-
fmt.Fprintln(d.stderr, "")
686-
for _, s := range svcSet {
687-
fmt.Fprintf(d.stderr, " %s\n", s.Name)
688-
}
689-
return nil
690-
}
691-
tw := tabwriter.NewWriter(d.stderr, 3, 2, 8, ' ', tabwriter.TabIndent)
692-
pcSvcs, err := services.ListServices(ctx, d.projectDir, d.stderr)
693-
if err != nil {
694-
fmt.Fprintln(d.stderr, "Error listing services: ", err)
695-
} else {
696-
fmt.Fprintln(d.stderr, "Services running in process-compose:")
697-
fmt.Fprintln(tw, "NAME\tSTATUS\tEXIT CODE")
698-
for _, s := range pcSvcs {
699-
fmt.Fprintf(tw, "%s\t%s\t%d\n", s.Name, s.Status, s.ExitCode)
700-
}
701-
tw.Flush()
702-
}
703-
return nil
704-
}
705-
706-
// TODO: move to services.go
707-
func (d *Devbox) RestartServices(
708-
ctx context.Context, runInCurrentShell bool, serviceNames ...string,
709-
) error {
710-
if !runInCurrentShell {
711-
return d.runDevboxServicesScript(ctx,
712-
append(
713-
[]string{"services", "restart", "--run-in-current-shell"},
714-
serviceNames...,
715-
),
716-
)
717-
}
718-
719-
if !services.ProcessManagerIsRunning(d.projectDir) {
720-
fmt.Fprintln(d.stderr, "Process-compose is not running. Starting it now...")
721-
fmt.Fprintln(d.stderr, "\nTip: We recommend using `devbox services up` to start process-compose and your services")
722-
return d.StartProcessManager(ctx, runInCurrentShell, serviceNames, devopt.ProcessComposeOpts{Background: true})
723-
}
724-
725-
// TODO: Restart with no services should restart the _currently running_ services. This means we should get the list of running services from the process-compose, then restart them all.
726-
727-
svcSet, err := d.Services()
728-
if err != nil {
729-
return err
730-
}
731-
732-
for _, s := range serviceNames {
733-
if _, ok := svcSet[s]; !ok {
734-
return usererr.New(fmt.Sprintf("Service %s not found in your project", s))
735-
}
736-
err := services.RestartServices(ctx, s, d.projectDir, d.stderr)
737-
if err != nil {
738-
fmt.Printf("Error restarting service %s: %s", s, err)
739-
} else {
740-
fmt.Printf("Service %s restarted", s)
741-
}
742-
}
743-
return nil
744-
}
745-
746-
// TODO: move to services.go
747-
func (d *Devbox) StartProcessManager(
748-
ctx context.Context,
749-
runInCurrentShell bool,
750-
requestedServices []string,
751-
processComposeOpts devopt.ProcessComposeOpts,
752-
) error {
753-
if !runInCurrentShell {
754-
args := []string{"services", "up", "--run-in-current-shell"}
755-
args = append(args, requestedServices...)
756-
757-
// TODO: Here we're attempting to reconstruct arguments from the original command, so that we can reinvoke it in devbox shell.
758-
// Instead, we should consider refactoring this so that we can preserve and re-use the original command string,
759-
// because the current approach is fragile and will need to be updated each time we add new flags.
760-
if d.customProcessComposeFile != "" {
761-
args = append(args, "--process-compose-file", d.customProcessComposeFile)
762-
}
763-
if processComposeOpts.Background {
764-
args = append(args, "--background")
765-
}
766-
for _, flag := range processComposeOpts.ExtraFlags {
767-
args = append(args, "--pcflags", flag)
768-
}
769-
770-
return d.runDevboxServicesScript(ctx, args)
771-
}
772-
773-
svcs, err := d.Services()
774-
if err != nil {
775-
return err
776-
}
777-
778-
if len(svcs) == 0 {
779-
return usererr.New("No services found in your project")
780-
}
781-
782-
for _, s := range requestedServices {
783-
if _, ok := svcs[s]; !ok {
784-
return usererr.New(fmt.Sprintf("Service %s not found in your project", s))
785-
}
786-
}
787-
788-
err = initDevboxUtilityProject(ctx, d.stderr)
789-
if err != nil {
790-
return err
791-
}
792-
793-
processComposeBinPath, err := utilityLookPath("process-compose")
794-
if err != nil {
795-
return err
796-
}
797-
798-
// Start the process manager
799-
800-
return services.StartProcessManager(
801-
d.stderr,
802-
requestedServices,
803-
svcs,
804-
d.projectDir,
805-
services.ProcessComposeOpts{
806-
BinPath: processComposeBinPath,
807-
Background: processComposeOpts.Background,
808-
ExtraFlags: processComposeOpts.ExtraFlags,
809-
},
810-
)
811-
}
812-
813574
func (d *Devbox) execPrintDevEnv(ctx context.Context, usePrintDevEnvCache bool) (map[string]string, error) {
814575
var spinny *spinner.Spinner
815576
if !usePrintDevEnvCache {

0 commit comments

Comments
 (0)