Skip to content

Commit 7585c33

Browse files
authored
Merge pull request #94 from wzshiming/feat/augerctl-get-format-json
[augerctl] Support for the json format as output
2 parents a0134ea + 4af761f commit 7585c33

File tree

4 files changed

+74
-31
lines changed

4 files changed

+74
-31
lines changed

augerctl/command/get_command.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func newCtlGetCommand(f *flagpole) *cobra.Command {
8484
},
8585
}
8686

87-
cmd.Flags().StringVarP(&flags.Output, "output", "o", "yaml", "output format. One of: (yaml).")
87+
cmd.Flags().StringVarP(&flags.Output, "output", "o", "yaml", "output format. One of: (yaml, json).")
8888
cmd.Flags().StringVarP(&flags.Namespace, "namespace", "n", "", "namespace of resource")
8989
cmd.Flags().Int64Var(&flags.ChunkSize, "chunk-size", 500, "chunk size of the list pager")
9090
cmd.Flags().StringVar(&flags.Prefix, "prefix", "/registry", "prefix to prepend to the resource")

augerctl/command/printer.go

+2-29
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ limitations under the License.
1717
package command
1818

1919
import (
20-
"errors"
21-
"fmt"
2220
"io"
2321

2422
"github.com/etcd-io/auger/pkg/client"
25-
"github.com/etcd-io/auger/pkg/encoding"
26-
"github.com/etcd-io/auger/pkg/scheme"
2723
)
2824

2925
type Printer interface {
@@ -34,31 +30,8 @@ func NewPrinter(w io.Writer, printerType string) Printer {
3430
switch printerType {
3531
case "yaml":
3632
return &yamlPrinter{w: w}
37-
}
38-
return nil
39-
}
40-
41-
func formatResponse(w io.Writer, outMediaType string, kv *client.KeyValue) error {
42-
value := kv.Value
43-
inMediaType, _, err := encoding.DetectAndExtract(value)
44-
if err != nil {
45-
_, err0 := fmt.Fprintf(w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
46-
if err0 != nil {
47-
return errors.Join(err, err0)
48-
}
49-
return nil
50-
}
51-
data, _, err := encoding.Convert(scheme.Codecs, inMediaType, outMediaType, value)
52-
if err != nil {
53-
_, err0 := fmt.Fprintf(w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
54-
if err0 != nil {
55-
return errors.Join(err, err0)
56-
}
57-
return nil
58-
}
59-
_, err = fmt.Fprintf(w, "---\n# %s | %s\n%s\n", kv.Key, inMediaType, data)
60-
if err != nil {
61-
return err
33+
case "json":
34+
return &jsonPrinter{w: w}
6235
}
6336
return nil
6437
}

augerctl/command/printer_json.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package command
18+
19+
import (
20+
"io"
21+
22+
"github.com/etcd-io/auger/pkg/client"
23+
"github.com/etcd-io/auger/pkg/encoding"
24+
"github.com/etcd-io/auger/pkg/scheme"
25+
)
26+
27+
type jsonPrinter struct {
28+
w io.Writer
29+
}
30+
31+
func (p *jsonPrinter) Print(kv *client.KeyValue) error {
32+
value := kv.Value
33+
inMediaType, _, err := encoding.DetectAndExtract(value)
34+
if err != nil {
35+
return err
36+
}
37+
data, _, err := encoding.Convert(scheme.Codecs, inMediaType, encoding.JsonMediaType, value)
38+
if err != nil {
39+
return err
40+
}
41+
_, err = p.w.Write(data)
42+
if err != nil {
43+
return err
44+
}
45+
return nil
46+
}

augerctl/command/printer_yaml.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,40 @@ limitations under the License.
1717
package command
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"io"
2123

2224
"github.com/etcd-io/auger/pkg/client"
2325
"github.com/etcd-io/auger/pkg/encoding"
26+
"github.com/etcd-io/auger/pkg/scheme"
2427
)
2528

2629
type yamlPrinter struct {
2730
w io.Writer
2831
}
2932

3033
func (p *yamlPrinter) Print(kv *client.KeyValue) error {
31-
return formatResponse(p.w, encoding.YamlMediaType, kv)
34+
value := kv.Value
35+
inMediaType, _, err := encoding.DetectAndExtract(value)
36+
if err != nil {
37+
_, err0 := fmt.Fprintf(p.w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
38+
if err0 != nil {
39+
return errors.Join(err, err0)
40+
}
41+
return nil
42+
}
43+
data, _, err := encoding.Convert(scheme.Codecs, inMediaType, encoding.YamlMediaType, value)
44+
if err != nil {
45+
_, err0 := fmt.Fprintf(p.w, "---\n# %s | raw | %v\n# %s\n", kv.Key, err, value)
46+
if err0 != nil {
47+
return errors.Join(err, err0)
48+
}
49+
return nil
50+
}
51+
_, err = fmt.Fprintf(p.w, "---\n# %s | %s\n%s\n", kv.Key, inMediaType, data)
52+
if err != nil {
53+
return err
54+
}
55+
return nil
3256
}

0 commit comments

Comments
 (0)