From 4cfae0243db95b8cdf5a921203800c210332df2f Mon Sep 17 00:00:00 2001 From: Jakub Oskera Date: Tue, 3 May 2022 11:18:33 +0200 Subject: [PATCH] refactor: use one function for both conf files --- internal/tkgi/config.go | 14 ++------------ internal/tkgi/credentials.go | 14 ++------------ internal/tkgi/util.go | 13 +++++++++++++ 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/internal/tkgi/config.go b/internal/tkgi/config.go index df8d7f73..39d3691d 100644 --- a/internal/tkgi/config.go +++ b/internal/tkgi/config.go @@ -1,12 +1,9 @@ package tkgi import ( - "errors" "fmt" "io/ioutil" - "path/filepath" - "github.com/ahmetb/kubectx/internal/cmdutil" "gopkg.in/yaml.v3" ) @@ -18,8 +15,9 @@ type Config struct { } `yaml:"clusters"` } +// get method returns content of config.yaml file func (c *Config) get() *Config { - path, err := tkgiKubectxConfigFile() + path, err := getTkgiKubectxFile("config.yaml") if err != nil { fmt.Println(err) } @@ -38,11 +36,3 @@ func (c *Config) get() *Config { } return c } - -func tkgiKubectxConfigFile() (string, error) { - home := cmdutil.HomeDir() - if home == "" { - return "", errors.New("HOME or USERPROFILE environment variable not set") - } - return filepath.Join(home, ".kube", "tkgi-kubectx", "config.yaml"), nil -} diff --git a/internal/tkgi/credentials.go b/internal/tkgi/credentials.go index 9f1b6152..e99a7f2c 100644 --- a/internal/tkgi/credentials.go +++ b/internal/tkgi/credentials.go @@ -1,12 +1,9 @@ package tkgi import ( - "errors" "fmt" "io/ioutil" - "path/filepath" - "github.com/ahmetb/kubectx/internal/cmdutil" "gopkg.in/yaml.v3" ) @@ -18,8 +15,9 @@ type Credentials struct { } `yaml:"credentials"` } +// get method returns content of credentials.yaml file func (c *Credentials) get() *Credentials { - path, err := tkgiKubectxCredentialsFile() + path, err := getTkgiKubectxFile("credentials.yaml") if err != nil { fmt.Println(err) } @@ -38,11 +36,3 @@ func (c *Credentials) get() *Credentials { } return c } - -func tkgiKubectxCredentialsFile() (string, error) { - home := cmdutil.HomeDir() - if home == "" { - return "", errors.New("HOME or USERPROFILE environment variable not set") - } - return filepath.Join(home, ".kube", "tkgi-kubectx", "credentials.yaml"), nil -} diff --git a/internal/tkgi/util.go b/internal/tkgi/util.go index 7e6563be..478d8f69 100644 --- a/internal/tkgi/util.go +++ b/internal/tkgi/util.go @@ -3,8 +3,12 @@ package tkgi import ( "errors" "os" + "path/filepath" + + "github.com/ahmetb/kubectx/internal/cmdutil" ) +// exists returns true if file or folder exists func exists(name string) (bool, error) { _, err := os.Stat(name) if err == nil { @@ -15,3 +19,12 @@ func exists(name string) (bool, error) { } return false, err } + +// getTkgiKubectxFile returns full path for given file +func getTkgiKubectxFile(file string) (string, error) { + home := cmdutil.HomeDir() + if home == "" { + return "", errors.New("HOME or USERPROFILE environment variable not set") + } + return filepath.Join(home, ".kube", "tkgi-kubectx", file), nil +}