mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-27 20:09:39 -08:00
feat(helm): add segment
This commit is contained in:
parent
8f8dd04ac7
commit
43aca8e9ea
|
@ -150,6 +150,8 @@ const (
|
||||||
GOLANG SegmentType = "go"
|
GOLANG SegmentType = "go"
|
||||||
// HASKELL segment
|
// HASKELL segment
|
||||||
HASKELL SegmentType = "haskell"
|
HASKELL SegmentType = "haskell"
|
||||||
|
// HELM segment
|
||||||
|
HELM SegmentType = "helm"
|
||||||
// IPIFY segment
|
// IPIFY segment
|
||||||
IPIFY SegmentType = "ipify"
|
IPIFY SegmentType = "ipify"
|
||||||
// ITERM inserts the Shell Integration prompt mark on iTerm zsh/bash/fish
|
// ITERM inserts the Shell Integration prompt mark on iTerm zsh/bash/fish
|
||||||
|
@ -280,6 +282,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
|
||||||
GITVERSION: func() SegmentWriter { return &segments.GitVersion{} },
|
GITVERSION: func() SegmentWriter { return &segments.GitVersion{} },
|
||||||
GOLANG: func() SegmentWriter { return &segments.Golang{} },
|
GOLANG: func() SegmentWriter { return &segments.Golang{} },
|
||||||
HASKELL: func() SegmentWriter { return &segments.Haskell{} },
|
HASKELL: func() SegmentWriter { return &segments.Haskell{} },
|
||||||
|
HELM: func() SegmentWriter { return &segments.Helm{} },
|
||||||
IPIFY: func() SegmentWriter { return &segments.IPify{} },
|
IPIFY: func() SegmentWriter { return &segments.IPify{} },
|
||||||
ITERM: func() SegmentWriter { return &segments.ITerm{} },
|
ITERM: func() SegmentWriter { return &segments.ITerm{} },
|
||||||
JAVA: func() SegmentWriter { return &segments.Java{} },
|
JAVA: func() SegmentWriter { return &segments.Java{} },
|
||||||
|
|
55
src/segments/helm.go
Normal file
55
src/segments/helm.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package segments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Helm struct {
|
||||||
|
props properties.Properties
|
||||||
|
env platform.Environment
|
||||||
|
|
||||||
|
Version string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Helm) Enabled() bool {
|
||||||
|
displayMode := h.props.GetString(DisplayMode, DisplayModeAlways)
|
||||||
|
if displayMode != DisplayModeFiles {
|
||||||
|
return h.getVersion()
|
||||||
|
}
|
||||||
|
|
||||||
|
inChart := false
|
||||||
|
files := []string{"Chart.yml", "Chart.yaml"}
|
||||||
|
for _, file := range files {
|
||||||
|
if _, err := h.env.HasParentFilePath(file); err == nil {
|
||||||
|
inChart = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return inChart && h.getVersion()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Helm) Template() string {
|
||||||
|
return " Helm {{.Version}}"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Helm) Init(props properties.Properties, env platform.Environment) {
|
||||||
|
h.props = props
|
||||||
|
h.env = env
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Helm) getVersion() bool {
|
||||||
|
cmd := "helm"
|
||||||
|
if !h.env.HasCommand(cmd) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := h.env.RunCommand(cmd, "version", "--short", "--template={{.Version}}")
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
h.Version = result[1:]
|
||||||
|
return true
|
||||||
|
}
|
89
src/segments/helm_test.go
Normal file
89
src/segments/helm_test.go
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
package segments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
testify_mock "github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHelmSegment(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
HelmExists bool
|
||||||
|
ExpectedEnabled bool
|
||||||
|
ExpectedString string
|
||||||
|
Template string
|
||||||
|
DisplayMode string
|
||||||
|
ChartFile string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "Helm not installed",
|
||||||
|
HelmExists: false,
|
||||||
|
ExpectedEnabled: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "DisplayMode always inside chart",
|
||||||
|
HelmExists: true,
|
||||||
|
ExpectedEnabled: true,
|
||||||
|
ExpectedString: "Helm 3.12.3",
|
||||||
|
DisplayMode: "always",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "DisplayMode always outside chart",
|
||||||
|
HelmExists: true,
|
||||||
|
ExpectedEnabled: true,
|
||||||
|
ExpectedString: "Helm 3.12.3",
|
||||||
|
DisplayMode: "always",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "DisplayMode files inside chart. Chart file Chart.yml",
|
||||||
|
HelmExists: true,
|
||||||
|
ExpectedEnabled: true,
|
||||||
|
ExpectedString: "Helm 3.12.3",
|
||||||
|
DisplayMode: "files",
|
||||||
|
ChartFile: "Chart.yml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "DisplayMode always inside chart. Chart file Chart.yaml",
|
||||||
|
HelmExists: true,
|
||||||
|
ExpectedEnabled: true,
|
||||||
|
ExpectedString: "Helm 3.12.3",
|
||||||
|
DisplayMode: "files",
|
||||||
|
ChartFile: "Chart.yaml",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "DisplayMode always outside chart",
|
||||||
|
HelmExists: true,
|
||||||
|
ExpectedEnabled: false,
|
||||||
|
DisplayMode: "files",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
env := new(mock.MockedEnvironment)
|
||||||
|
env.On("HasCommand", "helm").Return(tc.HelmExists)
|
||||||
|
env.On("RunCommand", "helm", []string{"version", "--short", "--template={{.Version}}"}).Return("v3.12.3", nil)
|
||||||
|
|
||||||
|
env.On("HasParentFilePath", tc.ChartFile).Return(&platform.FileInfo{}, nil)
|
||||||
|
env.On("HasParentFilePath", testify_mock.Anything).Return(&platform.FileInfo{}, errors.New("no such file or directory"))
|
||||||
|
|
||||||
|
props := properties.Map{
|
||||||
|
DisplayMode: tc.DisplayMode,
|
||||||
|
}
|
||||||
|
|
||||||
|
h := &Helm{
|
||||||
|
env: env,
|
||||||
|
props: props,
|
||||||
|
}
|
||||||
|
assert.Equal(t, tc.ExpectedEnabled, h.Enabled(), tc.Case)
|
||||||
|
if tc.ExpectedEnabled {
|
||||||
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, h.Template(), h), tc.Case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -272,6 +272,7 @@
|
||||||
"gitversion",
|
"gitversion",
|
||||||
"go",
|
"go",
|
||||||
"haskell",
|
"haskell",
|
||||||
|
"helm",
|
||||||
"ipify",
|
"ipify",
|
||||||
"iterm",
|
"iterm",
|
||||||
"julia",
|
"julia",
|
||||||
|
@ -2866,6 +2867,28 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"if": {
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"const": "helm"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"then": {
|
||||||
|
"title": "Helm segment",
|
||||||
|
"description": "https://ohmyposh.dev/docs/segments/helm",
|
||||||
|
"properties": {
|
||||||
|
"properties": {
|
||||||
|
"properties": {
|
||||||
|
"display_mode": {
|
||||||
|
"$ref": "#/definitions/display_mode"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"if": {
|
"if": {
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|
46
website/docs/segments/helm.mdx
Normal file
46
website/docs/segments/helm.mdx
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
---
|
||||||
|
id: helm
|
||||||
|
title: Helm
|
||||||
|
sidebar_label: Helm
|
||||||
|
---
|
||||||
|
|
||||||
|
## What
|
||||||
|
|
||||||
|
Display the version of helm
|
||||||
|
|
||||||
|
## Sample Configuration
|
||||||
|
|
||||||
|
import Config from '@site/src/components/Config.js';
|
||||||
|
|
||||||
|
<Config data={{
|
||||||
|
"background": "#a7cae1",
|
||||||
|
"foreground": "#100e23",
|
||||||
|
"powerline_symbol": "\ue0b0",
|
||||||
|
"template": " Helm {{ .Version }}",
|
||||||
|
"style": "powerline",
|
||||||
|
"type": "helm"
|
||||||
|
}}/>
|
||||||
|
|
||||||
|
## Properties
|
||||||
|
|
||||||
|
| Name | Type | Description |
|
||||||
|
| -------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| `display_mode` | `string` | <ul><li>`always`: the segment is always displayed (**default**)</li><li>`files`: the segment is only displayed when a `Chart.yaml` (or `Chart.yml`) file is present </li></ul> |
|
||||||
|
|
||||||
|
## Template ([info][templates])
|
||||||
|
|
||||||
|
:::note default template
|
||||||
|
|
||||||
|
```template
|
||||||
|
Helm {{ .Version }}
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
|
||||||
|
### Properties
|
||||||
|
|
||||||
|
| Name | Type | Description |
|
||||||
|
| ------------ | -------- | -------------------------- |
|
||||||
|
| `.Version` | `string` | Helm cli version |
|
||||||
|
|
||||||
|
[templates]: /docs/configuration/templates
|
|
@ -79,6 +79,7 @@ module.exports = {
|
||||||
"segments/gitversion",
|
"segments/gitversion",
|
||||||
"segments/golang",
|
"segments/golang",
|
||||||
"segments/haskell",
|
"segments/haskell",
|
||||||
|
"segments/helm",
|
||||||
"segments/ipify",
|
"segments/ipify",
|
||||||
"segments/iterm",
|
"segments/iterm",
|
||||||
"segments/java",
|
"segments/java",
|
||||||
|
|
Loading…
Reference in a new issue