1
0
mirror of https://github.com/rancher/cli.git synced 2026-02-05 09:48:36 +01:00
Files
cli/config/config.go

88 lines
2.0 KiB
Go
Raw Permalink Normal View History

2018-01-31 15:21:11 -07:00
package config
import (
"encoding/json"
"net/url"
"os"
"path"
"strings"
"github.com/sirupsen/logrus"
"k8s.io/client-go/tools/clientcmd/api"
2018-01-31 15:21:11 -07:00
)
// Config holds the main config for the user
type Config struct {
Servers map[string]*ServerConfig
//Path to the config file
Path string `json:"path,omitempty"`
// CurrentServer the user has in focus
CurrentServer string
}
//ServerConfig holds the config for each server the user has setup
type ServerConfig struct {
AccessKey string `json:"accessKey"`
SecretKey string `json:"secretKey"`
TokenKey string `json:"tokenKey"`
URL string `json:"url"`
Project string `json:"project"`
CACerts string `json:"cacert"`
KubeCredentials map[string]*ExecCredential `json:"kubeCredentials"`
KubeConfigs map[string]*api.Config `json:"kubeConfigs"`
2018-01-31 15:21:11 -07:00
}
func (c Config) Write() error {
err := os.MkdirAll(path.Dir(c.Path), 0700)
if err != nil {
return err
}
logrus.Infof("Saving config to %s", c.Path)
p := c.Path
c.Path = ""
output, err := os.Create(p)
if err != nil {
return err
}
defer output.Close()
return json.NewEncoder(output).Encode(c)
}
func (c Config) FocusedServer() *ServerConfig {
return c.Servers[c.CurrentServer]
}
func (c ServerConfig) FocusedCluster() string {
return strings.Split(c.Project, ":")[0]
}
func (c ServerConfig) KubeToken(key string) *ExecCredential {
return c.KubeCredentials[key]
}
2018-01-31 15:21:11 -07:00
func (c ServerConfig) EnvironmentURL() (string, error) {
url, err := baseURL(c.URL)
if err != nil {
return "", err
}
return url, nil
}
func baseURL(fullURL string) (string, error) {
idx := strings.LastIndex(fullURL, "/v3")
if idx == -1 {
u, err := url.Parse(fullURL)
if err != nil {
return "", err
}
newURL := url.URL{
Scheme: u.Scheme,
Host: u.Host,
}
return newURL.String(), nil
}
return fullURL[:idx], nil
}