Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ccf376
commit ab1e812
Showing
7 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ module.exports = { | |
"environment", | ||
"exit", | ||
"git", | ||
"golang", | ||
"kubectl", | ||
"node", | ||
"os", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<version>[0-9]+.[0-9]+.[0-9]+)`) | ||
values := groupDict(r, versionInfo) | ||
g.golangVersion = values["version"] | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters