refactor: show shorten kubeconfig path (#722)

This commit is contained in:
naison
2025-09-30 16:28:17 +08:00
committed by GitHub
parent f4a0ab39f4
commit 00bcef1e49
2 changed files with 31 additions and 2 deletions

View File

@@ -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 {

View File

@@ -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()
}