From 00bcef1e49be1b71b17f646a6327fe0e5d52d098 Mon Sep 17 00:00:00 2001 From: naison <895703375@qq.com> Date: Tue, 30 Sep 2025 16:28:17 +0800 Subject: [PATCH] refactor: show shorten kubeconfig path (#722) --- cmd/kubevpn/cmds/status.go | 24 +++++++++++++++++++++++- pkg/util/kube.go | 9 ++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cmd/kubevpn/cmds/status.go b/cmd/kubevpn/cmds/status.go index d416e29b..da8e7fd3 100644 --- a/cmd/kubevpn/cmds/status.go +++ b/cmd/kubevpn/cmds/status.go @@ -140,10 +140,32 @@ func genConnectMsg(w *tabwriter.Writer, currentConnectionID string, status []*rp _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "CURRENT", "CONNECTION ID", "CLUSTER", "KUBECONFIG", "NAMESPACE", "STATUS", "NETIF") for _, c := range status { current := util.If[string](c.ConnectionID == currentConnectionID, "*", "") - _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", current, c.ConnectionID, c.Cluster, c.Kubeconfig, c.Namespace, c.Status, c.Netif) + _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", current, c.ConnectionID, c.Cluster, shortenPath(c.Kubeconfig), c.Namespace, c.Status, c.Netif) } } +func shortenPath(absPath string) string { + // on windows + // cmd.exe not recognize '~', eg: cd ~, will error + // powershell.exe can recognize '~' + if util.IsWindows() { + return absPath + } + + homeDir, err := os.UserHomeDir() + if err != nil { + return absPath + } + relativePath, err := filepath.Rel(homeDir, absPath) + if err != nil { + return absPath + } + if strings.HasPrefix(relativePath, "..") { + return absPath + } + return filepath.Join("~", relativePath) +} + func genProxyMsg(w *tabwriter.Writer, list []*rpc.Status) { var needsPrint bool for _, status := range list { diff --git a/pkg/util/kube.go b/pkg/util/kube.go index cf729749..d69ccc3c 100644 --- a/pkg/util/kube.go +++ b/pkg/util/kube.go @@ -9,6 +9,7 @@ import ( "net/netip" "net/url" "os" + "path/filepath" "strconv" errors2 "github.com/pkg/errors" @@ -26,7 +27,13 @@ import ( func GetKubeConfigPath(f cmdutil.Factory) string { rawConfig := f.ToRawKubeConfigLoader() if rawConfig.ConfigAccess().IsExplicitFile() { - return rawConfig.ConfigAccess().GetExplicitFile() + file := rawConfig.ConfigAccess().GetExplicitFile() + abs, err := filepath.Abs(file) + if err != nil { + return file + } else { + return abs + } } else { return rawConfig.ConfigAccess().GetDefaultFilename() }