From 78d24ead0988e2f9da1c8cbdfcae0200d804eafa Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 17 Jan 2024 18:03:36 -0500 Subject: [PATCH] feat: add the ability to specify the output format when listing ports --- docs/development/plugin-triggers.md | 2 +- plugins/ports/proxy.go | 6 +++--- plugins/ports/src/triggers/triggers.go | 3 ++- plugins/ports/triggers.go | 21 ++++++++++++++++++++- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/docs/development/plugin-triggers.md b/docs/development/plugin-triggers.md index ac2dc2852..f718b4356 100644 --- a/docs/development/plugin-triggers.md +++ b/docs/development/plugin-triggers.md @@ -1371,7 +1371,7 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x - Description: Returns a list of port mappings, newline delimited - Invoked by: Various networking plugins -- Arguments: `$APP` +- Arguments: `$APP $FORMAT` - Example: ```shell diff --git a/plugins/ports/proxy.go b/plugins/ports/proxy.go index 1918a8c53..805b8c1c3 100644 --- a/plugins/ports/proxy.go +++ b/plugins/ports/proxy.go @@ -6,9 +6,9 @@ import ( // PortMap is a struct that contains a scheme:host-port:container-port mapping type PortMap struct { - ContainerPort int - HostPort int - Scheme string + ContainerPort int `json:"container_port"` + HostPort int `json:"host_port"` + Scheme string `json:"scheme"` } func (p PortMap) String() string { diff --git a/plugins/ports/src/triggers/triggers.go b/plugins/ports/src/triggers/triggers.go index d0e7cfe25..a1f1383a7 100644 --- a/plugins/ports/src/triggers/triggers.go +++ b/plugins/ports/src/triggers/triggers.go @@ -28,7 +28,8 @@ func main() { err = ports.TriggerPortsConfigure(appName) case "ports-get": appName := flag.Arg(0) - err = ports.TriggerPortsGet(appName) + format := flag.Arg(1) + err = ports.TriggerPortsGet(appName, format) case "ports-get-available": err = ports.TriggerPortsGetAvailable() case "ports-get-property": diff --git a/plugins/ports/triggers.go b/plugins/ports/triggers.go index e872aea2d..7a522d9c4 100644 --- a/plugins/ports/triggers.go +++ b/plugins/ports/triggers.go @@ -1,6 +1,7 @@ package ports import ( + "encoding/json" "fmt" "sort" @@ -64,17 +65,35 @@ func TriggerPortsConfigure(appName string) error { } // TriggerPortsGet prints out the port mapping for a given app -func TriggerPortsGet(appName string) error { +func TriggerPortsGet(appName string, format string) error { + if format != "json" && format != "stdout" { + return fmt.Errorf("Invalid format specified: %s", format) + } portMaps := getPortMaps(appName) if len(portMaps) == 0 { portMaps = getDetectedPortMaps(appName) } + persisted := []PortMap{} for _, portMap := range portMaps { if portMap.AllowsPersistence() { continue } + persisted = append(persisted, portMap) + } + + if format == "json" { + b, err := json.Marshal(persisted) + if err != nil { + return fmt.Errorf("Unable to marshal port mapping: %s", err.Error()) + } + + fmt.Println(string(b)) + return nil + } + + for _, portMap := range persisted { fmt.Println(portMap) }