Skip to content

Commit fe4678f

Browse files
Add services pcport command (#2784)
Closes #2783 ## Summary This command returns the port that process-compose is running on. That makes it more feasible to use the process-compose api in scripting. For example: ``` pcport=$(devbox services pcport) curl http://localhost:$pcport/process/good-server | jq ".status, .is_ready" ``` ## How was it tested? With process-compose running: ``` $ dist/devbox services pcport 53173 $ echo $? 0 ``` With process-compose stopped: ``` $ dist/devbox services pcport Error: process-compose is not running or it's config is missing. To start it, run `devbox services up` source: failed to find projectDir /Users/elliott.hilaire/github.com/elliotthilaire-ca/devbox in config.Instances $ echo $? 1 ``` ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). By creating this pull request, I represent that I have the right to license the contributions to the project maintainers under the Apache 2 License as stated in the [Community Contribution License](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license). ## AI disclosure Claude code assisted in creating this PR.
1 parent f0571c5 commit fe4678f

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

internal/boxcli/services.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ func servicesCmd(persistentPreRunE ...cobraFunc) *cobra.Command {
124124
},
125125
}
126126

127+
pcportCommand := &cobra.Command{
128+
Use: "pcport",
129+
Short: "Display the port that process-compose is running on",
130+
Args: cobra.ExactArgs(0),
131+
RunE: func(cmd *cobra.Command, args []string) error {
132+
return showProcessComposePort(cmd, flags)
133+
},
134+
}
135+
127136
flags.envFlag.register(servicesCommand)
128137
flags.config.registerPersistent(servicesCommand)
129138
servicesCommand.PersistentFlags().BoolVar(
@@ -141,6 +150,7 @@ func servicesCmd(persistentPreRunE ...cobraFunc) *cobra.Command {
141150
servicesCommand.AddCommand(restartCommand)
142151
servicesCommand.AddCommand(startCommand)
143152
servicesCommand.AddCommand(stopCommand)
153+
servicesCommand.AddCommand(pcportCommand)
144154
return servicesCommand
145155
}
146156

@@ -274,3 +284,16 @@ func startProcessManager(
274284
},
275285
)
276286
}
287+
288+
func showProcessComposePort(cmd *cobra.Command, flags servicesCmdFlags) error {
289+
box, err := devbox.Open(&devopt.Opts{
290+
Dir: flags.config.path,
291+
Environment: flags.config.environment,
292+
Stderr: cmd.ErrOrStderr(),
293+
})
294+
if err != nil {
295+
return errors.WithStack(err)
296+
}
297+
298+
return box.ShowProcessComposePort(cmd.Context(), cmd.OutOrStdout())
299+
}

internal/devbox/services.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package devbox
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"strconv"
78
"text/tabwriter"
89

@@ -272,3 +273,13 @@ func (d *Devbox) runDevboxServicesScript(ctx context.Context, cmdArgs []string)
272273
cmdArgs = append([]string{"services"}, cmdArgs...)
273274
return d.RunScript(ctx, devopt.EnvOptions{}, "devbox", cmdArgs)
274275
}
276+
277+
func (d *Devbox) ShowProcessComposePort(ctx context.Context, writer io.Writer) error {
278+
port, err := services.GetProcessManagerPort(d.projectDir)
279+
if err != nil {
280+
return err // Error already contains user-friendly message from services layer
281+
}
282+
283+
fmt.Fprintf(writer, "%d\n", port)
284+
return nil
285+
}

0 commit comments

Comments
 (0)