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/properties"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
GCPNOACTIVECONFIG = "NO ACTIVE CONFIG FOUND"
|
||||
)
|
||||
|
||||
type Gcp struct {
|
||||
props properties.Properties
|
||||
env environment.Environment
|
||||
|
@ -37,39 +40,42 @@ func (g *Gcp) Enabled() bool {
|
|||
}
|
||||
|
||||
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 {
|
||||
g.env.Log(environment.Error, "Gcp.Enabled()", err.Error())
|
||||
return false
|
||||
}
|
||||
|
||||
g.Project = cfg.Section("core").Key("project").String()
|
||||
g.Account = cfg.Section("core").Key("account").String()
|
||||
g.Region = cfg.Section("compute").Key("region").String()
|
||||
g.Project = data.Section("core").Key("project").String()
|
||||
g.Account = data.Section("core").Key("account").String()
|
||||
g.Region = data.Section("compute").Key("region").String()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *Gcp) getActiveConfig(cfgDir string) (string, error) {
|
||||
ap := path.Join(cfgDir, "active_config")
|
||||
absolutePath, err := filepath.Abs(ap)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
fileContent := g.env.FileContent(absolutePath)
|
||||
fileContent := g.env.FileContent(ap)
|
||||
if len(fileContent) == 0 {
|
||||
return "", errors.New("NO ACTIVE CONFIG FOUND")
|
||||
return "", errors.New(GCPNOACTIVECONFIG)
|
||||
}
|
||||
return fileContent, nil
|
||||
}
|
||||
|
||||
func (g *Gcp) getConfigDirectory() string {
|
||||
cfgDir := g.env.Getenv("CLOUDSDK_CONFIG")
|
||||
if len(cfgDir) == 0 {
|
||||
cfgDir = path.Join(g.env.Home(), ".config", "gcloud")
|
||||
if len(cfgDir) != 0 {
|
||||
return cfgDir
|
||||
}
|
||||
|
||||
return cfgDir
|
||||
if g.env.GOOS() == environment.WINDOWS {
|
||||
return path.Join(g.env.Getenv("APPDATA"), "gcloud")
|
||||
}
|
||||
return path.Join(g.env.Home(), ".config", "gcloud")
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package segments
|
|||
|
||||
import (
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"oh-my-posh/environment"
|
||||
|
@ -13,50 +12,132 @@ import (
|
|||
)
|
||||
|
||||
func TestGcpSegment(t *testing.T) {
|
||||
standardTemplate := "{{ if .Error }}{{ .Error }}{{ else }}{{ .Project }}{{ end }}"
|
||||
allTemplate := "{{.Project}} :: {{.Region}} :: {{.Account}}"
|
||||
|
||||
cases := []struct {
|
||||
Case string
|
||||
Template string
|
||||
ConfigPath string
|
||||
CfgData string
|
||||
ActiveConfig string
|
||||
ExpectedEnabled bool
|
||||
ExpectedString string
|
||||
}{
|
||||
{
|
||||
Case: "all information",
|
||||
Template: allTemplate,
|
||||
ConfigPath: "../test/",
|
||||
ActiveConfig: "gcptest",
|
||||
Case: "happy path",
|
||||
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",
|
||||
Template: standardTemplate,
|
||||
ConfigPath: "../invalid/",
|
||||
ActiveConfig: "nofile",
|
||||
Case: "no active config",
|
||||
ExpectedEnabled: false,
|
||||
},
|
||||
{
|
||||
Case: "invalid active config file",
|
||||
Template: standardTemplate,
|
||||
ConfigPath: "../invalid/",
|
||||
Case: "empty config",
|
||||
ActiveConfig: "production",
|
||||
ExpectedEnabled: false,
|
||||
},
|
||||
{
|
||||
Case: "bad config",
|
||||
ActiveConfig: "production",
|
||||
CfgData: "{bad}",
|
||||
ExpectedEnabled: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.ConfigPath)
|
||||
fcPath, _ := filepath.Abs(path.Join(tc.ConfigPath, "active_config"))
|
||||
env.On("Getenv", "CLOUDSDK_CONFIG").Return("config")
|
||||
fcPath := path.Join("config", "active_config")
|
||||
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()
|
||||
g := &Gcp{
|
||||
env: env,
|
||||
}
|
||||
assert.Equal(t, tc.ExpectedEnabled, g.Enabled(), tc.Case)
|
||||
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