Skip to content
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

ssh: Add output flag #258

Merged
merged 10 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use sigs.k8s.io yaml encoder
  • Loading branch information
petersutter committed Mar 29, 2023
commit 2ff410caa7a263bd912454de417dc76dbe823ddd
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
k8s.io/klog/v2 v2.70.1
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed
sigs.k8s.io/controller-runtime v0.13.0
sigs.k8s.io/yaml v1.3.0
)

require (
Expand Down Expand Up @@ -131,7 +132,6 @@ require (
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace k8s.io/client-go => k8s.io/client-go v0.25.0
11 changes: 4 additions & 7 deletions pkg/cmd/base/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
petersutter marked this conversation as resolved.
Show resolved Hide resolved

"github.com/gardener/gardenctl-v2/internal/util"
)
Expand Down Expand Up @@ -75,15 +75,12 @@ func (o *Options) PrintObject(obj interface{}) error {
fmt.Fprintf(o.IOStreams.Out, "%v", obj)

case "yaml":
yamlEncoder := yaml.NewEncoder(o.IOStreams.Out)
defer yamlEncoder.Close()

yamlEncoder.SetIndent(2)

err := yamlEncoder.Encode(&obj)
marshalled, err := yaml.Marshal(&obj)
if err != nil {
return err
}

fmt.Fprintln(o.IOStreams.Out, string(marshalled))
case "json":
marshalled, err := json.MarshalIndent(&obj, "", " ")
if err != nil {
Expand Down
19 changes: 10 additions & 9 deletions pkg/cmd/base/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
var _ = Describe("Base Options", func() {
Describe("given an instance", func() {
type barType struct {
Baz string
Baz string `json:"baz"`
}

type fooType struct {
Foo string
Bar barType
Foo string `json:"foo"`
Bar barType `json:"bar"`
}

var (
Expand Down Expand Up @@ -78,9 +78,9 @@ var _ = Describe("Base Options", func() {
Expect(options.PrintObject(foo)).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf(
`{
"Foo": %q,
"Bar": {
"Baz": %q
"foo": %q,
"bar": {
"baz": %q
}
}
`, foo.Foo, foo.Bar.Baz)))
Expand All @@ -99,11 +99,12 @@ var _ = Describe("Base Options", func() {
It("should print with yaml format", func() {
Expect(options.PrintObject(foo)).To(Succeed())
Expect(buf.String()).To(Equal(fmt.Sprintf(
`foo: %s
bar:
`bar:
baz: %s
foo: %s

`,
foo.Foo, foo.Bar.Baz)))
foo.Bar.Baz, foo.Foo)))
})
})

Expand Down