k9s/internal/client/helpers.go

41 lines
785 B
Go

package client
import (
"os/user"
"path"
"regexp"
"strings"
"github.com/rs/zerolog/log"
)
var toFileName = regexp.MustCompile(`[^(\w/\.)]`)
// Namespaced converts a resource path to namespace and resource name.
func Namespaced(p string) (string, string) {
ns, n := path.Split(p)
return strings.Trim(ns, "/"), n
}
// FQN returns a fully qualified resource name.
func FQN(ns, n string) string {
if ns == "" {
return n
}
return ns + "/" + n
}
func mustHomeDir() string {
usr, err := user.Current()
if err != nil {
log.Fatal().Err(err).Msg("Die getting user home directory")
}
return usr.HomeDir
}
func toHostDir(host string) string {
h := strings.Replace(strings.Replace(host, "https://", "", 1), "http://", "", 1)
return toFileName.ReplaceAllString(h, "_")
}