mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 03:49:40 -08:00
feat: golang segment
This commit is contained in:
parent
9ccf376d61
commit
ab1e812c06
28
docs/docs/segment-golang.md
Normal file
28
docs/docs/segment-golang.md
Normal file
|
@ -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`
|
|
@ -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`
|
||||
|
|
|
@ -21,6 +21,7 @@ module.exports = {
|
|||
"environment",
|
||||
"exit",
|
||||
"git",
|
||||
"golang",
|
||||
"kubectl",
|
||||
"node",
|
||||
"os",
|
||||
|
|
|
@ -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{
|
||||
|
|
35
segment_golang.go
Normal file
35
segment_golang.go
Normal file
|
@ -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
segment_golang_test.go
Executable file
90
segment_golang_test.go
Executable file
|
@ -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())
|
||||
}
|
|
@ -126,7 +126,8 @@ func getDefaultSettings() *Settings {
|
|||
Background: "#6CA35E",
|
||||
Foreground: "#ffffff",
|
||||
Properties: map[Property]interface{}{
|
||||
Prefix: " \uE718 ",
|
||||
Prefix: " \uE718",
|
||||
DisplayVersion: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue