Skip to content

Commit

Permalink
Merge pull request #2559 from alexissellier/list-port-json-output
Browse files Browse the repository at this point in the history
feat: add a json output for list port command
  • Loading branch information
FabianKramm authored Feb 21, 2023
2 parents 9483c4f + 554d4ce commit 358eb58
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions cmd/list/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package list

import (
"context"
"encoding/json"
"fmt"

"github.com/loft-sh/devspace/cmd/flags"
"github.com/loft-sh/devspace/pkg/util/factory"
Expand All @@ -13,6 +15,14 @@ import (

type portsCmd struct {
*flags.GlobalFlags

Output string
}

type jsonOutput struct {
ImageSelector string `json:"imageSelector"`
LabelSelector string `json:"labelSelector"`
Port string `json:"port"`
}

func newPortsCmd(f factory.Factory, globalFlags *flags.GlobalFlags) *cobra.Command {
Expand All @@ -33,6 +43,7 @@ Lists the port forwarding configurations
return cmd.RunListPort(f, cobraCmd, args)
}}

portsCmd.Flags().StringVarP(&cmd.Output, "output", "o", "", "The output format of the command. Can be either empty or json")
return portsCmd
}

Expand Down Expand Up @@ -80,11 +91,30 @@ func (cmd *portsCmd) RunListPort(f factory.Factory, cobraCmd *cobra.Command, arg
logger.Info("No ports are forwarded.\n")
return nil
}
headerColumnNames := []string{
"ImageSelector",
"LabelSelector",
"Ports (Local:Remote)",

switch cmd.Output {
case "":
headerColumnNames := []string{
"ImageSelector",
"LabelSelector",
"Ports (Local:Remote)",
}
log.PrintTable(logger, headerColumnNames, portForwards)
case "json":
output := make([]jsonOutput, 0)
for _, portFoward := range portForwards {
output = append(output, jsonOutput{
ImageSelector: portFoward[0],
LabelSelector: portFoward[1],
Port: portFoward[2],
})
}

out, err := json.MarshalIndent(output, "", " ")
if err != nil {
return err
}
fmt.Print(string(out))
}
log.PrintTable(logger, headerColumnNames, portForwards)
return nil
}

0 comments on commit 358eb58

Please sign in to comment.