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

View file

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