From 467c3f501cab072df1c4ac0b7439f4ee93140498 Mon Sep 17 00:00:00 2001 From: Paul Meyer <49727155+katexochen@users.noreply.github.com> Date: Fri, 13 Jan 2023 18:46:48 +0100 Subject: [PATCH] versionsapi: add implementation of json flag Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com> --- internal/versionsapi/cli/list.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/internal/versionsapi/cli/list.go b/internal/versionsapi/cli/list.go index 8a4d02647..395540775 100644 --- a/internal/versionsapi/cli/list.go +++ b/internal/versionsapi/cli/list.go @@ -8,6 +8,7 @@ package main import ( "context" + "encoding/json" "errors" "fmt" @@ -80,6 +81,20 @@ func runList(cmd *cobra.Command, args []string) error { return err } + if flags.json { + log.Debugf("Printing versions as JSON.") + var vers []string + for _, v := range patchVersions { + vers = append(vers, v.Version) + } + raw, err := json.Marshal(vers) + if err != nil { + return fmt.Errorf("marshaling versions: %w", err) + } + fmt.Println(string(raw)) + return nil + } + log.Debugf("Printing versions.") for _, v := range patchVersions { fmt.Println(v.ShortPath()) @@ -139,6 +154,7 @@ type listFlags struct { region string bucket string distributionID string + json bool logLevel zapcore.Level } @@ -184,6 +200,10 @@ func parseListFlags(cmd *cobra.Command) (listFlags, error) { if err != nil { return listFlags{}, err } + json, err := cmd.Flags().GetBool("json") + if err != nil { + return listFlags{}, err + } verbose, err := cmd.Flags().GetBool("verbose") if err != nil { return listFlags{}, err @@ -200,6 +220,7 @@ func parseListFlags(cmd *cobra.Command) (listFlags, error) { region: region, bucket: bucket, distributionID: distributionID, + json: json, logLevel: logLevel, }, nil }