Skip to content

[augerctl] Support for the json format as output #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion augerctl/command/get_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func newCtlGetCommand(f *flagpole) *cobra.Command {
},
}

cmd.Flags().StringVarP(&flags.Output, "output", "o", "yaml", "output format. One of: (yaml).")
cmd.Flags().StringVarP(&flags.Output, "output", "o", "yaml", "output format. One of: (yaml, json).")
cmd.Flags().StringVarP(&flags.Namespace, "namespace", "n", "", "namespace of resource")
cmd.Flags().Int64Var(&flags.ChunkSize, "chunk-size", 500, "chunk size of the list pager")
cmd.Flags().StringVar(&flags.Prefix, "prefix", "/registry", "prefix to prepend to the resource")
Expand Down
31 changes: 2 additions & 29 deletions augerctl/command/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ limitations under the License.
package command

import (
"errors"
"fmt"
"io"

"github.com/etcd-io/auger/pkg/client"
"github.com/etcd-io/auger/pkg/encoding"
"github.com/etcd-io/auger/pkg/scheme"
)

type Printer interface {
Expand All @@ -34,31 +30,8 @@ func NewPrinter(w io.Writer, printerType string) Printer {
switch printerType {
case "yaml":
return &yamlPrinter{w: w}
}
return nil
}

func formatResponse(w io.Writer, outMediaType string, kv *client.KeyValue) error {
value := kv.Value
inMediaType, _, err := encoding.DetectAndExtract(value)
if err != nil {
_, err0 := fmt.Fprintf(w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
if err0 != nil {
return errors.Join(err, err0)
}
return nil
}
data, _, err := encoding.Convert(scheme.Codecs, inMediaType, outMediaType, value)
if err != nil {
_, err0 := fmt.Fprintf(w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
if err0 != nil {
return errors.Join(err, err0)
}
return nil
}
_, err = fmt.Fprintf(w, "---\n# %s | %s\n%s\n", kv.Key, inMediaType, data)
if err != nil {
return err
case "json":
return &jsonPrinter{w: w}
}
return nil
}
46 changes: 46 additions & 0 deletions augerctl/command/printer_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
"io"

"github.com/etcd-io/auger/pkg/client"
"github.com/etcd-io/auger/pkg/encoding"
"github.com/etcd-io/auger/pkg/scheme"
)

type jsonPrinter struct {
w io.Writer
}

func (p *jsonPrinter) Print(kv *client.KeyValue) error {
value := kv.Value
inMediaType, _, err := encoding.DetectAndExtract(value)
if err != nil {
return err
}
data, _, err := encoding.Convert(scheme.Codecs, inMediaType, encoding.JsonMediaType, value)
if err != nil {
return err
}
_, err = p.w.Write(data)
if err != nil {
return err
}
return nil
}
26 changes: 25 additions & 1 deletion augerctl/command/printer_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,40 @@ limitations under the License.
package command

import (
"errors"
"fmt"
"io"

"github.com/etcd-io/auger/pkg/client"
"github.com/etcd-io/auger/pkg/encoding"
"github.com/etcd-io/auger/pkg/scheme"
)

type yamlPrinter struct {
w io.Writer
}

func (p *yamlPrinter) Print(kv *client.KeyValue) error {
return formatResponse(p.w, encoding.YamlMediaType, kv)
value := kv.Value
inMediaType, _, err := encoding.DetectAndExtract(value)
if err != nil {
_, err0 := fmt.Fprintf(p.w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
if err0 != nil {
return errors.Join(err, err0)
}
return nil
}
data, _, err := encoding.Convert(scheme.Codecs, inMediaType, encoding.YamlMediaType, value)
if err != nil {
_, err0 := fmt.Fprintf(p.w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
if err0 != nil {
return errors.Join(err, err0)
}
return nil
}
_, err = fmt.Fprintf(p.w, "---\n# %s | %s\n%s\n", kv.Key, inMediaType, data)
if err != nil {
return err
}
return nil
}
Loading