Skip to content

Commit

Permalink
refactor: use one function for both conf files
Browse files Browse the repository at this point in the history
  • Loading branch information
jakuboskera committed May 3, 2022
1 parent 6a50f55 commit 4cfae02
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
14 changes: 2 additions & 12 deletions internal/tkgi/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package tkgi

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"

"github.com/ahmetb/kubectx/internal/cmdutil"
"gopkg.in/yaml.v3"
)

Expand All @@ -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)
}
Expand All @@ -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
}
14 changes: 2 additions & 12 deletions internal/tkgi/credentials.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package tkgi

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"

"github.com/ahmetb/kubectx/internal/cmdutil"
"gopkg.in/yaml.v3"
)

Expand All @@ -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)
}
Expand All @@ -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
}
13 changes: 13 additions & 0 deletions internal/tkgi/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 4cfae02

Please sign in to comment.