Skip to content

Commit

Permalink
write file
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Feb 29, 2024
1 parent c14cf2a commit b1e80b8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cmd/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
aesKey = string(key)
}

return types.Decrypt(aesKey, resourceFiles...)
return types.Decrypt(printFlags, aesKey, resourceFiles...)
},
}
)
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,6 @@ func init() {
klog.InitFlags(fs)
_ = fs.Parse([]string{"-logtostderr=false"})
klog.SetOutput(io.Discard)

printFlags.AddFlags(decrypt)
}
5 changes: 3 additions & 2 deletions pkg/export/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
21 changes: 5 additions & 16 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
Expand All @@ -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"
Expand Down Expand Up @@ -93,7 +91,7 @@ func NewConfig(configFlags *genericclioptions.ConfigFlags, printFlags *genericcl
},
SortSlices: KindFields{},
configFlags: configFlags,
printFlags: printFlags,
PrintFlags: printFlags,
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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 ""
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/types/encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -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@"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
15 changes: 1 addition & 14 deletions pkg/uor/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions pkg/utils/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}

0 comments on commit b1e80b8

Please sign in to comment.