From 43aca8e9eae8f60bf5a6603a311eb1b309841d87 Mon Sep 17 00:00:00 2001 From: "Ernesto R. C. Pereda" Date: Mon, 4 Sep 2023 15:00:04 -0400 Subject: [PATCH] feat(helm): add segment --- src/engine/segment.go | 3 ++ src/segments/helm.go | 55 +++++++++++++++++++++ src/segments/helm_test.go | 89 ++++++++++++++++++++++++++++++++++ themes/schema.json | 23 +++++++++ website/docs/segments/helm.mdx | 46 ++++++++++++++++++ website/sidebars.js | 1 + 6 files changed, 217 insertions(+) create mode 100644 src/segments/helm.go create mode 100644 src/segments/helm_test.go create mode 100644 website/docs/segments/helm.mdx diff --git a/src/engine/segment.go b/src/engine/segment.go index 43cb83f5..26f76ef2 100644 --- a/src/engine/segment.go +++ b/src/engine/segment.go @@ -150,6 +150,8 @@ const ( GOLANG SegmentType = "go" // HASKELL segment HASKELL SegmentType = "haskell" + // HELM segment + HELM SegmentType = "helm" // IPIFY segment IPIFY SegmentType = "ipify" // 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{} }, GOLANG: func() SegmentWriter { return &segments.Golang{} }, HASKELL: func() SegmentWriter { return &segments.Haskell{} }, + HELM: func() SegmentWriter { return &segments.Helm{} }, IPIFY: func() SegmentWriter { return &segments.IPify{} }, ITERM: func() SegmentWriter { return &segments.ITerm{} }, JAVA: func() SegmentWriter { return &segments.Java{} }, diff --git a/src/segments/helm.go b/src/segments/helm.go new file mode 100644 index 00000000..9df1e745 --- /dev/null +++ b/src/segments/helm.go @@ -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 +} diff --git a/src/segments/helm_test.go b/src/segments/helm_test.go new file mode 100644 index 00000000..83e5d4a3 --- /dev/null +++ b/src/segments/helm_test.go @@ -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) + } + } +} diff --git a/themes/schema.json b/themes/schema.json index d8f41a5d..3b70bbb3 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -272,6 +272,7 @@ "gitversion", "go", "haskell", + "helm", "ipify", "iterm", "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": { "properties": { diff --git a/website/docs/segments/helm.mdx b/website/docs/segments/helm.mdx new file mode 100644 index 00000000..3edff120 --- /dev/null +++ b/website/docs/segments/helm.mdx @@ -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'; + + + +## Properties + +| Name | Type | Description | +| -------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `display_mode` | `string` | | + +## Template ([info][templates]) + +:::note default template + +```template + Helm {{ .Version }} +``` + +::: + +### Properties + +| Name | Type | Description | +| ------------ | -------- | -------------------------- | +| `.Version` | `string` | Helm cli version | + +[templates]: /docs/configuration/templates diff --git a/website/sidebars.js b/website/sidebars.js index c0d2d845..22bfabad 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -79,6 +79,7 @@ module.exports = { "segments/gitversion", "segments/golang", "segments/haskell", + "segments/helm", "segments/ipify", "segments/iterm", "segments/java",