versionsapi: add implementation of json flag

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-01-13 18:46:48 +01:00
parent 4239191b0d
commit 467c3f501c

View File

@ -8,6 +8,7 @@ package main
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -80,6 +81,20 @@ func runList(cmd *cobra.Command, args []string) error {
return err 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.") log.Debugf("Printing versions.")
for _, v := range patchVersions { for _, v := range patchVersions {
fmt.Println(v.ShortPath()) fmt.Println(v.ShortPath())
@ -139,6 +154,7 @@ type listFlags struct {
region string region string
bucket string bucket string
distributionID string distributionID string
json bool
logLevel zapcore.Level logLevel zapcore.Level
} }
@ -184,6 +200,10 @@ func parseListFlags(cmd *cobra.Command) (listFlags, error) {
if err != nil { if err != nil {
return listFlags{}, err return listFlags{}, err
} }
json, err := cmd.Flags().GetBool("json")
if err != nil {
return listFlags{}, err
}
verbose, err := cmd.Flags().GetBool("verbose") verbose, err := cmd.Flags().GetBool("verbose")
if err != nil { if err != nil {
return listFlags{}, err return listFlags{}, err
@ -200,6 +220,7 @@ func parseListFlags(cmd *cobra.Command) (listFlags, error) {
region: region, region: region,
bucket: bucket, bucket: bucket,
distributionID: distributionID, distributionID: distributionID,
json: json,
logLevel: logLevel, logLevel: logLevel,
}, nil }, nil
} }