diff --git a/docs/docs/segment-golang.md b/docs/docs/segment-golang.md new file mode 100644 index 000000000000..0c265b44f4b4 --- /dev/null +++ b/docs/docs/segment-golang.md @@ -0,0 +1,28 @@ +--- +id: golang +title: Golang +sidebar_label: Golang +--- + +## What + +Display the currently active golang version when a folder contains `.go` files. + +## Sample Configuration + +```json +{ + "type": "go", + "style": "powerline", + "powerline_symbol": "\uE0B0", + "foreground": "#ffffff", + "background": "#7FD5EA", + "properties": { + "prefix": " \uFCD1 " + } +} +``` + +## Properties + +- display_version: `boolean` - display the golang version - defaults to `true` diff --git a/docs/docs/segment-node.md b/docs/docs/segment-node.md index dffb22ede8c7..40e5d737e10c 100644 --- a/docs/docs/segment-node.md +++ b/docs/docs/segment-node.md @@ -22,3 +22,7 @@ Display the currently active node version when a folder contains `.js` or `.ts` } } ``` + +## Properties + +- display_version: `boolean` - display the node version - defaults to `true` diff --git a/docs/sidebars.js b/docs/sidebars.js index de8759b1c7d8..68c97d2e287e 100644 --- a/docs/sidebars.js +++ b/docs/sidebars.js @@ -21,6 +21,7 @@ module.exports = { "environment", "exit", "git", + "golang", "kubectl", "node", "os", diff --git a/segment.go b/segment.go index 03e8631866c0..8a8f440b0bb2 100644 --- a/segment.go +++ b/segment.go @@ -71,6 +71,8 @@ const ( Dotnet SegmentType = "dotnet" //Terraform writes the terraform workspace we're currently in Terraform SegmentType = "terraform" + //Golang writes which go version is currently active + Golang SegmentType = "go" //Powerline writes it Powerline style Powerline SegmentStyle = "powerline" //Plain writes it without ornaments @@ -129,6 +131,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error { Kubectl: &kubectl{}, Dotnet: &dotnet{}, Terraform: &terraform{}, + Golang: &golang{}, } if writer, ok := functions[segment.Type]; ok { props := &properties{ diff --git a/segment_golang.go b/segment_golang.go new file mode 100644 index 000000000000..f7b659fce57c --- /dev/null +++ b/segment_golang.go @@ -0,0 +1,35 @@ +package main + +import "regexp" + +type golang struct { + props *properties + env environmentInfo + golangVersion string +} + +func (g *golang) string() string { + if g.props.getBool(DisplayVersion, true) { + return g.golangVersion + } + return "" +} + +func (g *golang) init(props *properties, env environmentInfo) { + g.props = props + g.env = env +} + +func (g *golang) enabled() bool { + if !g.env.hasFiles("*.go") { + return false + } + if !g.env.hasCommand("go") { + return false + } + versionInfo, _ := g.env.runCommand("go", "version") + r := regexp.MustCompile(`go(?P[0-9]+.[0-9]+.[0-9]+)`) + values := groupDict(r, versionInfo) + g.golangVersion = values["version"] + return true +} diff --git a/segment_golang_test.go b/segment_golang_test.go new file mode 100755 index 000000000000..a3959df2eebd --- /dev/null +++ b/segment_golang_test.go @@ -0,0 +1,90 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type golangArgs struct { + enabled bool + goVersion string + hasFiles bool + displayVersion bool +} + +func bootStrapGolangTest(args *golangArgs) *golang { + env := new(MockedEnvironment) + env.On("hasCommand", "go").Return(args.enabled) + env.On("runCommand", "go", []string{"version"}).Return(args.goVersion, nil) + env.On("hasFiles", "*.go").Return(args.hasFiles) + props := &properties{ + values: map[Property]interface{}{ + DisplayVersion: args.displayVersion, + }, + } + g := &golang{ + env: env, + props: props, + } + return g +} + +func TestGolangNoGoInstalled(t *testing.T) { + args := &golangArgs{ + enabled: false, + } + golang := bootStrapGolangTest(args) + assert.False(t, golang.enabled()) +} + +func TestGolangGoInstalledNoFiles(t *testing.T) { + args := &golangArgs{ + enabled: true, + hasFiles: false, + } + golang := bootStrapGolangTest(args) + assert.False(t, golang.enabled()) +} + +func TestGolangFilesNoGo(t *testing.T) { + args := &golangArgs{ + enabled: false, + hasFiles: true, + } + golang := bootStrapGolangTest(args) + assert.False(t, golang.enabled()) +} + +func TestGolangGoEnabled(t *testing.T) { + args := &golangArgs{ + enabled: true, + hasFiles: true, + } + golang := bootStrapGolangTest(args) + assert.True(t, golang.enabled()) +} + +func TestGolangGoEnabledWithVersion(t *testing.T) { + args := &golangArgs{ + enabled: true, + hasFiles: true, + displayVersion: true, + goVersion: "go version go1.15.3 darwin/amd64", + } + golang := bootStrapGolangTest(args) + assert.True(t, golang.enabled()) + assert.Equal(t, "1.15.3", golang.string()) +} + +func TestGolangGoEnabledWithoutVersion(t *testing.T) { + args := &golangArgs{ + enabled: true, + hasFiles: true, + displayVersion: false, + goVersion: "go version go1.15.3 darwin/amd64", + } + golang := bootStrapGolangTest(args) + assert.True(t, golang.enabled()) + assert.Equal(t, "", golang.string()) +} diff --git a/settings.go b/settings.go index c4a99493bb24..3152da708087 100755 --- a/settings.go +++ b/settings.go @@ -126,7 +126,8 @@ func getDefaultSettings() *Settings { Background: "#6CA35E", Foreground: "#ffffff", Properties: map[Property]interface{}{ - Prefix: " \uE718 ", + Prefix: " \uE718", + DisplayVersion: false, }, }, {