mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
refactor: show shorten kubeconfig path (#722)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user