feat: add --format json support to ps:scale

The `ps:scale` command now accepts a `--format` flag that defaults to `stdout` and can be set to `json` to emit the current formation as a JSON array of process type and quantity objects, matching the JSON output the `:report` subcommands already provide. The flag only applies when displaying the current formation; it is ignored when process types are supplied for scaling. When no scale has been set for the app, the JSON output is an empty array.
This commit is contained in:
Jose Diaz-Gonzalez
2026-07-06 13:57:19 -04:00
parent 4a88f3aba4
commit 857d115fdd
6 changed files with 74 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
package ps
import (
"encoding/json"
"errors"
"fmt"
"os"
@@ -243,12 +244,22 @@ func restorePrep() error {
return nil
}
func scaleReport(appName string) error {
func scaleReport(appName string, format string) error {
formations, err := getFormations(appName)
if err != nil {
return err
}
if format == "json" {
out, err := json.Marshal(formations)
if err != nil {
return err
}
common.Log(string(out))
return nil
}
common.LogInfo1Quiet(fmt.Sprintf("Scaling for %s", appName))
content := []string{}

View File

@@ -23,7 +23,7 @@ Additional commands:`
ps:report [<app>] [<flag>], Displays a process report for one or more apps
ps:restart [--parallel count] [--all|<app>] [<process-name>], Restart an app
ps:restore [<app>], Start previously running apps e.g. after reboot
ps:scale [--skip-deploy] <app> <proc>=<count> [<proc>=<count>...], Get/Set how many instances of a given process to run
ps:scale [--skip-deploy] [--format stdout|json] <app> [<proc>=<count>...], Get/Set how many instances of a given process to run
ps:set <app> <key> <value>, Set or clear a ps property for an app
ps:start [--parallel count] [--all|<app>], Start an app
ps:stop [--parallel count] [--all|<app>], Stop an app

View File

@@ -65,10 +65,11 @@ func main() {
case "scale":
args := flag.NewFlagSet("ps:scale", flag.ExitOnError)
skipDeploy := args.Bool("skip-deploy", false, "--skip-deploy: skip deploy of the app")
format := args.String("format", "stdout", "format: [ stdout | json ]")
args.Parse(os.Args[2:])
appName := args.Arg(0)
_, processTuples := common.ShiftString(args.Args())
err = ps.CommandScale(appName, *skipDeploy, processTuples)
err = ps.CommandScale(appName, *skipDeploy, *format, processTuples)
case "set":
args := flag.NewFlagSet("ps:set", flag.ExitOnError)
global := args.Bool("global", false, "--global: set a global property")

View File

@@ -158,13 +158,13 @@ func CommandRetire(appName string) error {
}
// CommandScale gets or sets how many instances of a given process to run
func CommandScale(appName string, skipDeploy bool, processTuples []string) error {
func CommandScale(appName string, skipDeploy bool, format string, processTuples []string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}
if len(processTuples) == 0 {
return scaleReport(appName)
return scaleReport(appName, format)
}
if !canScaleApp(appName) {