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

fixing issue #888 #967

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (ac *AddCommand) runAdd(cmd *cobra.Command, args []string) error {
}
} else {
// check path
file, err := CheckAndTransformFilePath(file)
file, err := CheckAndTransformFilePath(file, cfgCreate)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (mc MergeCommand) runMerge(command *cobra.Command, args []string) error {
}

if folder != "" {
folder, err = CheckAndTransformFilePath(folder)
folder, err = CheckAndTransformDirPath(folder)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import (
)

var (
// cfgFile represents the path to the configuration file.
cfgFile string
uiSize int
macNotify bool
silenceTable bool
cfgCreate bool
)

// Cli cmd struct
Expand Down Expand Up @@ -68,6 +70,7 @@ func (cli *Cli) setFlags() {
}
flags := cli.rootCmd.PersistentFlags()
flags.StringVar(&cfgFile, "config", kubeconfig, "path of kubeconfig")
flags.BoolVar(&cfgCreate, "create", false, "Create a new kubeconfig file if not exists")
// let the `make doc-gen` command generate consistent output rather than parsing different $HOME environment variables for different users.
flags.Lookup("config").DefValue = "$HOME/.kube/config"
flags.IntVarP(&uiSize, "ui-size", "u", 4, "number of list items to show in menu at once")
Expand All @@ -78,7 +81,7 @@ func (cli *Cli) setFlags() {
// Run command
func (cli *Cli) Run() error {
// check and format kubeconfig path
config, err := CheckAndTransformFilePath(cfgFile)
config, err := CheckAndTransformFilePath(cfgFile, cfgCreate)
if err != nil {
return err
}
Expand Down
47 changes: 45 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func WriteConfig(cover bool, file string, outConfig *clientcmdapi.Config) error

// UpdateConfigFile update kubeconfig
func UpdateConfigFile(file string, updateConfig *clientcmdapi.Config) error {
file, err := CheckAndTransformFilePath(file)
file, err := CheckAndTransformFilePath(file, cfgCreate)
if err != nil {
return err
}
Expand Down Expand Up @@ -416,10 +416,31 @@ func appendConfig(c1, c2 *clientcmdapi.Config) *clientcmdapi.Config {
}

// CheckAndTransformFilePath return converted path
func CheckAndTransformFilePath(path string) (string, error) {
func CheckAndTransformFilePath(path string, autoCreate bool) (string, error) {
if strings.HasPrefix(path, "~/") {
path = filepath.Join(homeDir(), path[2:])
}
if IsFile(path) {
printYellow(os.Stdout, path+" Path Exist\n")
} else {
if !autoCreate {
return path, errors.New("path Not Exist")
}
printYellow(os.Stdout, "Createing Directory: "+filepath.Dir(path)+"\n")
printYellow(os.Stdout, path+" Path is Not Absolute, setting path to home dir\n")
pathDir := filepath.Join(homeDir(), ".kube")
path = filepath.Join(pathDir, "config")
err := os.MkdirAll(pathDir, 0777)
if err != nil {
return path, err
}
file, err := os.Create(path)
if err != nil {
return path, err
}
defer file.Close()
return path, err
}
// read files info
_, err := os.Stat(path)
if err != nil {
Expand Down Expand Up @@ -481,3 +502,25 @@ func validateContextTemplate(contextTemplate []string) error {
}
return nil
}

// checkes if a path exists
func IsFile(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return !info.IsDir()
}

// CheckAndTransformFilePath return converted path
func CheckAndTransformDirPath(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
path = filepath.Join(homeDir(), path[2:])
}
// read files info
_, err := os.Stat(path)
if err != nil {
return "", err
}
return path, nil
}
AdamRussak marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 14 additions & 4 deletions cmd/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"os/user"
"path/filepath"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -195,9 +196,10 @@ func TestExitOption(t *testing.T) {
}

func TestCheckAndTransformFilePath(t *testing.T) {
wantPath := homeDir()
wantPath := filepath.Join(homeDir(), ".kube", "config")
type args struct {
path string
path string
cfgCreate bool
}
tests := []struct {
name string
Expand All @@ -206,11 +208,13 @@ func TestCheckAndTransformFilePath(t *testing.T) {
wantErr bool
}{
// TODO: Add test cases.
{"test-~", args{path: "~/"}, wantPath, false},
{"test -~ with no auto create", args{path: "~/", cfgCreate: false}, homeDir(), true},
{"test -~ with auto create enabled", args{path: "~/", cfgCreate: true}, wantPath, false},
{"test - false config path no auto create", args{path: "", cfgCreate: false}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CheckAndTransformFilePath(tt.args.path)
got, err := CheckAndTransformFilePath(tt.args.path, tt.args.cfgCreate)
if (err != nil) != tt.wantErr {
t.Errorf("CheckAndTransformFilePath() error = %v, wantErr %v", err, tt.wantErr)
return
Expand All @@ -219,6 +223,12 @@ func TestCheckAndTransformFilePath(t *testing.T) {
t.Errorf("CheckAndTransformFilePath() got = %v, want %v", got, tt.want)
}
})
if tt.args.cfgCreate {
t.Cleanup(func() {
// Remove the file from wantPath after the test run is done
os.RemoveAll(filepath.Join(homeDir(), ".kube"))
})
}
}
}

Expand Down
Loading