feat(gcp): remove .Error

aligns the segment with az and aws, relates to #2810

BREAKING CHANGE: this removes the .Error field from the segment template
which means anyone with a custom template in the gcp segment needs to
remove it from their template. The new default template is:

`" {{ .Project }} "`
This commit is contained in:
Jan De Dobbeleer 2022-09-22 19:52:22 +02:00 committed by Jan De Dobbeleer
parent 086260a5af
commit 7ea921351f
2 changed files with 18 additions and 19 deletions

View file

@ -17,11 +17,10 @@ type Gcp struct {
Account string Account string
Project string Project string
Region string Region string
Error string
} }
func (g *Gcp) Template() string { func (g *Gcp) Template() string {
return " {{ if not .Error }}{{ .Project }}{{ end }} " return " {{ .Project }} "
} }
func (g *Gcp) Init(props properties.Properties, env environment.Environment) { func (g *Gcp) Init(props properties.Properties, env environment.Environment) {
@ -33,16 +32,16 @@ func (g *Gcp) Enabled() bool {
cfgDir := g.getConfigDirectory() cfgDir := g.getConfigDirectory()
configFile, err := g.getActiveConfig(cfgDir) configFile, err := g.getActiveConfig(cfgDir)
if err != nil { if err != nil {
g.Error = err.Error() g.env.Log(environment.Error, "Gcp.Enabled()", err.Error())
return true return false
} }
cfgpath := path.Join(cfgDir, "configurations", "config_"+configFile) cfgpath := path.Join(cfgDir, "configurations", "config_"+configFile)
cfg, err := ini.Load(cfgpath) cfg, err := ini.Load(cfgpath)
if err != nil { if err != nil {
g.Error = "GCLOUD CONFIG ERROR" g.env.Log(environment.Error, "Gcp.Enabled()", err.Error())
return true return false
} }
g.Project = cfg.Section("core").Key("project").String() g.Project = cfg.Section("core").Key("project").String()

View file

@ -5,9 +5,11 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"oh-my-posh/environment"
"oh-my-posh/mock" "oh-my-posh/mock"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
mock2 "github.com/stretchr/testify/mock"
) )
func TestGcpSegment(t *testing.T) { func TestGcpSegment(t *testing.T) {
@ -31,20 +33,15 @@ func TestGcpSegment(t *testing.T) {
ExpectedString: "test-test-test :: europe-test1 :: test@example.com", ExpectedString: "test-test-test :: europe-test1 :: test@example.com",
}, },
{ {
Case: "non-existent config file", Case: "non-existent config file",
Template: standardTemplate, Template: standardTemplate,
ConfigPath: "../invalid/", ConfigPath: "../invalid/",
ActiveConfig: "nofile", ActiveConfig: "nofile",
ExpectedEnabled: true,
ExpectedString: "GCLOUD CONFIG ERROR",
}, },
{ {
Case: "invalid active config file", Case: "invalid active config file",
Template: standardTemplate, Template: standardTemplate,
ConfigPath: "../invalid/", ConfigPath: "../invalid/",
ActiveConfig: "",
ExpectedEnabled: true,
ExpectedString: "NO ACTIVE CONFIG FOUND",
}, },
} }
@ -53,10 +50,13 @@ func TestGcpSegment(t *testing.T) {
env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.ConfigPath) env.On("Getenv", "CLOUDSDK_CONFIG").Return(tc.ConfigPath)
fcPath, _ := filepath.Abs(path.Join(tc.ConfigPath, "active_config")) fcPath, _ := filepath.Abs(path.Join(tc.ConfigPath, "active_config"))
env.On("FileContent", fcPath).Return(tc.ActiveConfig) env.On("FileContent", fcPath).Return(tc.ActiveConfig)
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)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, g), tc.Case) if tc.ExpectedEnabled {
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, g), tc.Case)
}
} }
} }