mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
parent
7ea921351f
commit
1d244debed
|
@ -5,11 +5,14 @@ import (
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
"oh-my-posh/properties"
|
"oh-my-posh/properties"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
GCPNOACTIVECONFIG = "NO ACTIVE CONFIG FOUND"
|
||||||
|
)
|
||||||
|
|
||||||
type Gcp struct {
|
type Gcp struct {
|
||||||
props properties.Properties
|
props properties.Properties
|
||||||
env environment.Environment
|
env environment.Environment
|
||||||
|
@ -37,39 +40,42 @@ func (g *Gcp) Enabled() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
cfgpath := path.Join(cfgDir, "configurations", "config_"+configFile)
|
cfgpath := path.Join(cfgDir, "configurations", "config_"+configFile)
|
||||||
|
cfg := g.env.FileContent(cfgpath)
|
||||||
|
|
||||||
cfg, err := ini.Load(cfgpath)
|
if len(cfg) == 0 {
|
||||||
|
g.env.Log(environment.Error, "Gcp.Enabled()", "config file is empty")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := ini.Load([]byte(cfg))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
g.env.Log(environment.Error, "Gcp.Enabled()", err.Error())
|
g.env.Log(environment.Error, "Gcp.Enabled()", err.Error())
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Project = cfg.Section("core").Key("project").String()
|
g.Project = data.Section("core").Key("project").String()
|
||||||
g.Account = cfg.Section("core").Key("account").String()
|
g.Account = data.Section("core").Key("account").String()
|
||||||
g.Region = cfg.Section("compute").Key("region").String()
|
g.Region = data.Section("compute").Key("region").String()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Gcp) getActiveConfig(cfgDir string) (string, error) {
|
func (g *Gcp) getActiveConfig(cfgDir string) (string, error) {
|
||||||
ap := path.Join(cfgDir, "active_config")
|
ap := path.Join(cfgDir, "active_config")
|
||||||
absolutePath, err := filepath.Abs(ap)
|
fileContent := g.env.FileContent(ap)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
fileContent := g.env.FileContent(absolutePath)
|
|
||||||
if len(fileContent) == 0 {
|
if len(fileContent) == 0 {
|
||||||
return "", errors.New("NO ACTIVE CONFIG FOUND")
|
return "", errors.New(GCPNOACTIVECONFIG)
|
||||||
}
|
}
|
||||||
return fileContent, nil
|
return fileContent, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Gcp) getConfigDirectory() string {
|
func (g *Gcp) getConfigDirectory() string {
|
||||||
cfgDir := g.env.Getenv("CLOUDSDK_CONFIG")
|
cfgDir := g.env.Getenv("CLOUDSDK_CONFIG")
|
||||||
if len(cfgDir) == 0 {
|
if len(cfgDir) != 0 {
|
||||||
cfgDir = path.Join(g.env.Home(), ".config", "gcloud")
|
return cfgDir
|
||||||
}
|
}
|
||||||
|
if g.env.GOOS() == environment.WINDOWS {
|
||||||
return cfgDir
|
return path.Join(g.env.Getenv("APPDATA"), "gcloud")
|
||||||
|
}
|
||||||
|
return path.Join(g.env.Home(), ".config", "gcloud")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package segments
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
|
@ -13,50 +12,132 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGcpSegment(t *testing.T) {
|
func TestGcpSegment(t *testing.T) {
|
||||||
standardTemplate := "{{ if .Error }}{{ .Error }}{{ else }}{{ .Project }}{{ end }}"
|
|
||||||
allTemplate := "{{.Project}} :: {{.Region}} :: {{.Account}}"
|
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Case string
|
Case string
|
||||||
Template string
|
CfgData string
|
||||||
ConfigPath string
|
|
||||||
ActiveConfig string
|
ActiveConfig string
|
||||||
ExpectedEnabled bool
|
ExpectedEnabled bool
|
||||||
ExpectedString string
|
ExpectedString string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
Case: "all information",
|
Case: "happy path",
|
||||||
Template: allTemplate,
|
|
||||||
ConfigPath: "../test/",
|
|
||||||
ActiveConfig: "gcptest",
|
|
||||||
ExpectedEnabled: true,
|
ExpectedEnabled: true,
|
||||||
ExpectedString: "test-test-test :: europe-test1 :: test@example.com",
|
ActiveConfig: "production",
|
||||||
|
CfgData: `
|
||||||
|
[core]
|
||||||
|
account = test@example.com
|
||||||
|
project = test-test-test
|
||||||
|
|
||||||
|
[compute]
|
||||||
|
region = europe-test1
|
||||||
|
`,
|
||||||
|
ExpectedString: "test-test-test :: europe-test1 :: test@example.com",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "non-existent config file",
|
Case: "no active config",
|
||||||
Template: standardTemplate,
|
ExpectedEnabled: false,
|
||||||
ConfigPath: "../invalid/",
|
|
||||||
ActiveConfig: "nofile",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "invalid active config file",
|
Case: "empty config",
|
||||||
Template: standardTemplate,
|
ActiveConfig: "production",
|
||||||
ConfigPath: "../invalid/",
|
ExpectedEnabled: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "bad config",
|
||||||
|
ActiveConfig: "production",
|
||||||
|
CfgData: "{bad}",
|
||||||
|
ExpectedEnabled: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
env := new(mock.MockedEnvironment)
|
env := new(mock.MockedEnvironment)
|
||||||
env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.ConfigPath)
|
env.On("Getenv", "CLOUDSDK_CONFIG").Return("config")
|
||||||
fcPath, _ := filepath.Abs(path.Join(tc.ConfigPath, "active_config"))
|
fcPath := path.Join("config", "active_config")
|
||||||
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
|
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
|
||||||
|
cfgpath := path.Join("config", "configurations", "config_production")
|
||||||
|
env.On("FileContent", cfgpath).Return(tc.CfgData)
|
||||||
env.On("Log", environment.Error, "Gcp.Enabled()", mock2.Anything).Return()
|
env.On("Log", environment.Error, "Gcp.Enabled()", mock2.Anything).Return()
|
||||||
g := &Gcp{
|
g := &Gcp{
|
||||||
env: env,
|
env: env,
|
||||||
}
|
}
|
||||||
assert.Equal(t, tc.ExpectedEnabled, g.Enabled(), tc.Case)
|
assert.Equal(t, tc.ExpectedEnabled, g.Enabled(), tc.Case)
|
||||||
if tc.ExpectedEnabled {
|
if tc.ExpectedEnabled {
|
||||||
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, g), tc.Case)
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, "{{.Project}} :: {{.Region}} :: {{.Account}}", g), tc.Case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetConfigDirectory(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
GOOS string
|
||||||
|
Home string
|
||||||
|
AppData string
|
||||||
|
CloudSDKConfig string
|
||||||
|
Expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "CLOUDSDK_CONFIG",
|
||||||
|
CloudSDKConfig: "/Users/posh/.config/gcloud",
|
||||||
|
Expected: "/Users/posh/.config/gcloud",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "Windows",
|
||||||
|
GOOS: environment.WINDOWS,
|
||||||
|
AppData: "/Users/posh/.config",
|
||||||
|
Expected: "/Users/posh/.config/gcloud",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "default",
|
||||||
|
Home: "/Users/posh2/",
|
||||||
|
Expected: "/Users/posh2/.config/gcloud",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
env := new(mock.MockedEnvironment)
|
||||||
|
env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.CloudSDKConfig)
|
||||||
|
env.On("Getenv", "APPDATA").Return(tc.AppData)
|
||||||
|
env.On("Home").Return(tc.Home)
|
||||||
|
env.On("GOOS").Return(tc.GOOS)
|
||||||
|
g := &Gcp{
|
||||||
|
env: env,
|
||||||
|
}
|
||||||
|
assert.Equal(t, tc.Expected, g.getConfigDirectory(), tc.Case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetActiveConfig(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
ActiveConfig string
|
||||||
|
ExpectedString string
|
||||||
|
ExpectedError string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "No active config",
|
||||||
|
ExpectedError: GCPNOACTIVECONFIG,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "No active config",
|
||||||
|
ActiveConfig: "production",
|
||||||
|
ExpectedString: "production",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
env := new(mock.MockedEnvironment)
|
||||||
|
env.On("FileContent", "active_config").Return(tc.ActiveConfig)
|
||||||
|
g := &Gcp{
|
||||||
|
env: env,
|
||||||
|
}
|
||||||
|
got, err := g.getActiveConfig("")
|
||||||
|
assert.Equal(t, tc.ExpectedString, got, tc.Case)
|
||||||
|
if len(tc.ExpectedError) > 0 {
|
||||||
|
assert.EqualError(t, err, tc.ExpectedError, tc.Case)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err, tc.Case)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
[core]
|
|
||||||
account = test@example.com
|
|
||||||
project = test-test-test
|
|
||||||
|
|
||||||
[compute]
|
|
||||||
region = europe-test1
|
|
Loading…
Reference in a new issue