Skip to content

Commit

Permalink
feat(gcp): remove .Error
Browse files Browse the repository at this point in the history
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 }} "`
  • Loading branch information
JanDeDobbeleer committed Sep 23, 2022
1 parent 086260a commit 7ea9213
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
11 changes: 5 additions & 6 deletions src/segments/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand Down
26 changes: 13 additions & 13 deletions src/segments/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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/",
},
}

Expand All @@ -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)
}
}
}

0 comments on commit 7ea9213

Please sign in to comment.