feat(helm): add segment

This commit is contained in:
Ernesto R. C. Pereda 2023-09-04 15:00:04 -04:00 committed by GitHub
parent 8f8dd04ac7
commit 43aca8e9ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 217 additions and 0 deletions

View file

@ -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{} },

55
src/segments/helm.go Normal file
View 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
View 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)
}
}
}

View file

@ -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": {

View 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

View file

@ -79,6 +79,7 @@ module.exports = {
"segments/gitversion",
"segments/golang",
"segments/haskell",
"segments/helm",
"segments/ipify",
"segments/iterm",
"segments/java",