From b1e80b8cb8d53a15cdcdff250d1184c31ef91eeb Mon Sep 17 00:00:00 2001 From: bakito Date: Thu, 29 Feb 2024 23:37:22 +0100 Subject: [PATCH] write file --- cmd/decrypt.go | 2 +- cmd/root.go | 2 ++ pkg/export/worker/worker.go | 5 +++-- pkg/types/config.go | 21 +++++---------------- pkg/types/encrypted.go | 7 ++++++- pkg/uor/update.go | 15 +-------------- pkg/utils/read.go | 25 +++++++++++++++++++++++++ 7 files changed, 43 insertions(+), 34 deletions(-) diff --git a/cmd/decrypt.go b/cmd/decrypt.go index bb38d8a..76d8b64 100644 --- a/cmd/decrypt.go +++ b/cmd/decrypt.go @@ -29,7 +29,7 @@ var ( aesKey = string(key) } - return types.Decrypt(aesKey, resourceFiles...) + return types.Decrypt(printFlags, aesKey, resourceFiles...) }, } ) diff --git a/cmd/root.go b/cmd/root.go index 9c38b4c..4efeac2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -136,4 +136,6 @@ func init() { klog.InitFlags(fs) _ = fs.Parse([]string{"-logtostderr=false"}) klog.SetOutput(io.Discard) + + printFlags.AddFlags(decrypt) } diff --git a/pkg/export/worker/worker.go b/pkg/export/worker/worker.go index 6af8e7f..e84bc50 100644 --- a/pkg/export/worker/worker.go +++ b/pkg/export/worker/worker.go @@ -13,6 +13,7 @@ import ( "github.com/bakito/kubexporter/pkg/client" "github.com/bakito/kubexporter/pkg/log" "github.com/bakito/kubexporter/pkg/types" + "github.com/bakito/kubexporter/pkg/utils" "github.com/vbauerster/mpb/v5" "github.com/vbauerster/mpb/v5/decor" "k8s.io/apimachinery/pkg/api/errors" @@ -280,7 +281,7 @@ func (w *worker) exportLists(res *types.GroupResource, ul *unstructured.Unstruct continue } - err = w.config.PrintObj(usl, f) + err = utils.PrintObj(w.config.PrintFlags, usl, f) if err != nil { res.Error = err.Error() continue @@ -331,7 +332,7 @@ func (w *worker) exportSingleResources(res *types.GroupResource, ul *unstructure continue } - err = w.config.PrintObj(us, f) + err = utils.PrintObj(w.config.PrintFlags, us, f) if err != nil { res.Error = err.Error() continue diff --git a/pkg/types/config.go b/pkg/types/config.go index 053dc2b..34c038f 100644 --- a/pkg/types/config.go +++ b/pkg/types/config.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "io" "os" "path/filepath" "regexp" @@ -17,7 +16,6 @@ import ( "github.com/ghodss/yaml" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/client-go/rest" @@ -93,7 +91,7 @@ func NewConfig(configFlags *genericclioptions.ConfigFlags, printFlags *genericcl }, SortSlices: KindFields{}, configFlags: configFlags, - printFlags: printFlags, + PrintFlags: printFlags, } } @@ -125,7 +123,7 @@ type Config struct { includedSet set log log.YALI configFlags *genericclioptions.ConfigFlags - printFlags *genericclioptions.PrintFlags + PrintFlags *genericclioptions.PrintFlags `json:"-" yaml:"-"` } // Progress type @@ -344,7 +342,7 @@ func (c *Config) fileName(res *GroupResource, namespace string, name string, tem "Name": name, "Kind": res.Kind(), "Group": res.APIGroup, - "Extension": *c.printFlags.OutputFormat, + "Extension": *c.PrintFlags.OutputFormat, }, ) @@ -397,15 +395,6 @@ func (c *Config) Validate() error { return nil } -// PrintObj print the given object -func (c *Config) PrintObj(ro runtime.Object, out io.Writer) error { - p, err := c.printFlags.ToPrinter() - if err != nil { - return err - } - return p.PrintObj(ro, out) -} - // Logger get the logger func (c *Config) Logger() log.YALI { if c.log == nil { @@ -416,8 +405,8 @@ func (c *Config) Logger() log.YALI { // OutputFormat get the current output format func (c *Config) OutputFormat() string { - if c.printFlags != nil && c.printFlags.OutputFormat != nil { - return *c.printFlags.OutputFormat + if c.PrintFlags != nil && c.PrintFlags.OutputFormat != nil { + return *c.PrintFlags.OutputFormat } return "" } diff --git a/pkg/types/encrypted.go b/pkg/types/encrypted.go index 00d7294..55fb5ba 100644 --- a/pkg/types/encrypted.go +++ b/pkg/types/encrypted.go @@ -12,6 +12,7 @@ import ( "github.com/bakito/kubexporter/pkg/utils" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/cli-runtime/pkg/genericclioptions" ) const prefix = "AES@" @@ -65,7 +66,7 @@ func (c *Config) EncryptFields(res *GroupResource, us unstructured.Unstructured) transformNestedFields(c.Encrypted.KindFields, c.Encrypted.doEncrypt, res.GroupKind(), us) } -func Decrypt(aesKey string, files ...string) error { +func Decrypt(printFlags *genericclioptions.PrintFlags, aesKey string, files ...string) error { gcm, err := setupAES(aesKey) if err != nil { return err @@ -80,6 +81,10 @@ func Decrypt(aesKey string, files ...string) error { if err := decryptFields(us.Object, gcm, nonceSize); err != nil { return err } + + if err := utils.WriteFile(printFlags, file, us); err != nil { + return err + } } return nil } diff --git a/pkg/uor/update.go b/pkg/uor/update.go index 2de8f87..f3b6079 100644 --- a/pkg/uor/update.go +++ b/pkg/uor/update.go @@ -104,7 +104,7 @@ func updateFile(ctx context.Context, config *types.Config, file string, ac *clie } if changed { us.SetOwnerReferences(refs) - err := write(config, file, us) + err := utils.WriteFile(config.PrintFlags, file, us) if err != nil { return err } @@ -137,16 +137,3 @@ func findOwner(ctx context.Context, ac *client.ApiClient, owners map[string]*uns owners[key] = owner return owner, nil } - -func write(config *types.Config, file string, us *unstructured.Unstructured) error { - f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666) - if err != nil { - return err - } - defer f.Close() - err = config.PrintObj(us, f) - if err != nil { - return err - } - return nil -} diff --git a/pkg/utils/read.go b/pkg/utils/read.go index 9ae8df7..d55484a 100644 --- a/pkg/utils/read.go +++ b/pkg/utils/read.go @@ -2,10 +2,13 @@ package utils import ( "bufio" + "io" "os" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/yaml" + "k8s.io/cli-runtime/pkg/genericclioptions" ) func ReadFile(file string) (*unstructured.Unstructured, error) { @@ -24,3 +27,25 @@ func ReadFile(file string) (*unstructured.Unstructured, error) { } return us, nil } + +func WriteFile(printFlags *genericclioptions.PrintFlags, file string, us *unstructured.Unstructured) error { + f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666) + if err != nil { + return err + } + defer f.Close() + err = PrintObj(printFlags, us, f) + if err != nil { + return err + } + return nil +} + +// PrintObj print the given object +func PrintObj(printFlags *genericclioptions.PrintFlags, ro runtime.Object, out io.Writer) error { + p, err := printFlags.ToPrinter() + if err != nil { + return err + } + return p.PrintObj(ro, out) +}