Skip to content

Commit

Permalink
feat: golang segment
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 22, 2020
1 parent 9ccf376 commit ab1e812
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 1 deletion.
28 changes: 28 additions & 0 deletions 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`
4 changes: 4 additions & 0 deletions docs/docs/segment-node.md
Expand Up @@ -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`
1 change: 1 addition & 0 deletions docs/sidebars.js
Expand Up @@ -21,6 +21,7 @@ module.exports = {
"environment",
"exit",
"git",
"golang",
"kubectl",
"node",
"os",
Expand Down
3 changes: 3 additions & 0 deletions segment.go
Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down
35 changes: 35 additions & 0 deletions 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<version>[0-9]+.[0-9]+.[0-9]+)`)
values := groupDict(r, versionInfo)
g.golangVersion = values["version"]
return true
}
90 changes: 90 additions & 0 deletions 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())
}
3 changes: 2 additions & 1 deletion settings.go
Expand Up @@ -126,7 +126,8 @@ func getDefaultSettings() *Settings {
Background: "#6CA35E",
Foreground: "#ffffff",
Properties: map[Property]interface{}{
Prefix: " \uE718 ",
Prefix: " \uE718",
DisplayVersion: false,
},
},
{
Expand Down

0 comments on commit ab1e812

Please sign in to comment.