mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
feat(segment): add talosctl
This commit is contained in:
parent
4179476fe4
commit
f03c6cb00c
|
@ -240,6 +240,8 @@ const (
|
||||||
SWIFT SegmentType = "swift"
|
SWIFT SegmentType = "swift"
|
||||||
// SYSTEMINFO writes system information (memory, cpu, load)
|
// SYSTEMINFO writes system information (memory, cpu, load)
|
||||||
SYSTEMINFO SegmentType = "sysinfo"
|
SYSTEMINFO SegmentType = "sysinfo"
|
||||||
|
// TALOSCTL writes the talosctl context
|
||||||
|
TALOSCTL SegmentType = "talosctl"
|
||||||
// TERRAFORM writes the terraform workspace we're currently in
|
// TERRAFORM writes the terraform workspace we're currently in
|
||||||
TERRAFORM SegmentType = "terraform"
|
TERRAFORM SegmentType = "terraform"
|
||||||
// TEXT writes a text
|
// TEXT writes a text
|
||||||
|
@ -345,6 +347,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
|
||||||
SVN: func() SegmentWriter { return &segments.Svn{} },
|
SVN: func() SegmentWriter { return &segments.Svn{} },
|
||||||
SWIFT: func() SegmentWriter { return &segments.Swift{} },
|
SWIFT: func() SegmentWriter { return &segments.Swift{} },
|
||||||
SYSTEMINFO: func() SegmentWriter { return &segments.SystemInfo{} },
|
SYSTEMINFO: func() SegmentWriter { return &segments.SystemInfo{} },
|
||||||
|
TALOSCTL: func() SegmentWriter { return &segments.TalosCTL{} },
|
||||||
TERRAFORM: func() SegmentWriter { return &segments.Terraform{} },
|
TERRAFORM: func() SegmentWriter { return &segments.Terraform{} },
|
||||||
TEXT: func() SegmentWriter { return &segments.Text{} },
|
TEXT: func() SegmentWriter { return &segments.Text{} },
|
||||||
TIME: func() SegmentWriter { return &segments.Time{} },
|
TIME: func() SegmentWriter { return &segments.Time{} },
|
||||||
|
|
56
src/segments/talosctl.go
Normal file
56
src/segments/talosctl.go
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
package segments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TalosCTL struct {
|
||||||
|
props properties.Properties
|
||||||
|
env platform.Environment
|
||||||
|
|
||||||
|
Context string `yaml:"context"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TalosCTL) Template() string {
|
||||||
|
return " {{ .Context}} "
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TalosCTL) Init(props properties.Properties, env platform.Environment) {
|
||||||
|
t.props = props
|
||||||
|
t.env = env
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TalosCTL) Enabled() bool {
|
||||||
|
cfgDir := filepath.Join(t.env.Home(), ".talos")
|
||||||
|
configFile, err := t.getActiveConfig(cfgDir)
|
||||||
|
if err != nil {
|
||||||
|
t.env.Error(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
err = yaml.Unmarshal([]byte(configFile), t)
|
||||||
|
if err != nil {
|
||||||
|
t.env.Error(err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(t.Context) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TalosCTL) getActiveConfig(cfgDir string) (string, error) {
|
||||||
|
activeConfigFile := filepath.Join(cfgDir, "config")
|
||||||
|
activeConfigData := t.env.FileContent(activeConfigFile)
|
||||||
|
if len(activeConfigData) == 0 {
|
||||||
|
return "", errors.New("NO ACTIVE CONFIG FOUND")
|
||||||
|
}
|
||||||
|
return activeConfigData, nil
|
||||||
|
}
|
95
src/segments/talosctl_test.go
Normal file
95
src/segments/talosctl_test.go
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
package segments
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
mock2 "github.com/stretchr/testify/mock"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTalosctlSegment(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
ActiveConfig string
|
||||||
|
ExpectedEnabled bool
|
||||||
|
ExpectedString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "happy path",
|
||||||
|
ExpectedEnabled: true,
|
||||||
|
ActiveConfig: "context: context-name",
|
||||||
|
ExpectedString: "context-name",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "no active config",
|
||||||
|
ExpectedEnabled: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "empty config",
|
||||||
|
ActiveConfig: "",
|
||||||
|
ExpectedEnabled: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "bad config",
|
||||||
|
ActiveConfig: "other-yaml: not-expected",
|
||||||
|
ExpectedEnabled: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
env := new(mock.MockedEnvironment)
|
||||||
|
env.On("Home").Return("home")
|
||||||
|
fcPath := filepath.Join("home", ".talos", "config")
|
||||||
|
env.On("FileContent", fcPath).Return(tc.ActiveConfig)
|
||||||
|
env.On("Error", mock2.Anything).Return()
|
||||||
|
talos := TalosCTL{
|
||||||
|
env: env,
|
||||||
|
}
|
||||||
|
talos.Enabled()
|
||||||
|
assert.Equal(t, tc.ExpectedEnabled, talos.Enabled())
|
||||||
|
if tc.ExpectedEnabled {
|
||||||
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, talos.Template(), talos), tc.Case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetTalosctlActiveConfig(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
ActiveConfig string
|
||||||
|
ExpectedString string
|
||||||
|
ExpectedError string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "happy path",
|
||||||
|
ActiveConfig: "context: context-name",
|
||||||
|
ExpectedString: "context: context-name",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "no active config",
|
||||||
|
ActiveConfig: "",
|
||||||
|
ExpectedError: "NO ACTIVE CONFIG FOUND",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
env := new(mock.MockedEnvironment)
|
||||||
|
env.On("Home").Return("home")
|
||||||
|
configPath := filepath.Join("home", ".talos")
|
||||||
|
contentPath := filepath.Join(configPath, "config")
|
||||||
|
env.On("FileContent", contentPath).Return(tc.ActiveConfig)
|
||||||
|
env.On("Error", mock2.Anything).Return()
|
||||||
|
talos := TalosCTL{
|
||||||
|
env: env,
|
||||||
|
}
|
||||||
|
got, err := talos.getActiveConfig(configPath)
|
||||||
|
assert.Equal(t, tc.ExpectedString, got, tc.Case)
|
||||||
|
if len(tc.ExpectedError) > 0 {
|
||||||
|
assert.EqualError(t, err, tc.ExpectedError, tc.Case)
|
||||||
|
} else {
|
||||||
|
assert.NoError(t, err, tc.Case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,7 @@
|
||||||
"error-text": "#242424",
|
"error-text": "#242424",
|
||||||
"git-text": "#238636",
|
"git-text": "#238636",
|
||||||
"kubernetes-text": "#FFBB00",
|
"kubernetes-text": "#FFBB00",
|
||||||
|
"talosctl-text": "#FF6C00",
|
||||||
"symbol-color": "#ffffff",
|
"symbol-color": "#ffffff",
|
||||||
"timer-text": "#dd0033"
|
"timer-text": "#dd0033"
|
||||||
},
|
},
|
||||||
|
@ -39,6 +40,14 @@
|
||||||
"template": "<p:symbol-color>\uf308</> {{.Context}}{{if .Namespace}} \uf061 {{.Namespace}}{{end}}",
|
"template": "<p:symbol-color>\uf308</> {{.Context}}{{if .Namespace}} \uf061 {{.Namespace}}{{end}}",
|
||||||
"type": "kubectl"
|
"type": "kubectl"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"background": "p:background-color",
|
||||||
|
"foreground": "p:talosctl-text",
|
||||||
|
"style": "powerline",
|
||||||
|
"powerline_symbol": "\ue0b4",
|
||||||
|
"template": "<p:symbol-color>\udb84\udcfe</> {{.Context}}",
|
||||||
|
"type": "talosctl"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"background": "p:background-color",
|
"background": "p:background-color",
|
||||||
"foreground": "p:cloud-text-amazon",
|
"foreground": "p:cloud-text-amazon",
|
||||||
|
|
|
@ -360,6 +360,7 @@
|
||||||
"strava",
|
"strava",
|
||||||
"svn",
|
"svn",
|
||||||
"swift",
|
"swift",
|
||||||
|
"talosctl",
|
||||||
"time",
|
"time",
|
||||||
"text",
|
"text",
|
||||||
"terraform",
|
"terraform",
|
||||||
|
@ -2779,6 +2780,19 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"if": {
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"const": "talosctl"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"then": {
|
||||||
|
"title": "Talosctl Segment",
|
||||||
|
"description": "https://ohmyposh.dev/docs/segments/talosctl"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"if": {
|
"if": {
|
||||||
"properties": {
|
"properties": {
|
||||||
|
|
47
website/docs/segments/talosctl.mdx
Normal file
47
website/docs/segments/talosctl.mdx
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
---
|
||||||
|
id: talosctl
|
||||||
|
title: Talosctl Context
|
||||||
|
sidebar_label: Talosctl
|
||||||
|
---
|
||||||
|
|
||||||
|
## What
|
||||||
|
|
||||||
|
Displays the currently active Talosctl context name.
|
||||||
|
|
||||||
|
This segment leverages the `.talos/config` file generated by the [Talosctl CLI][talosctl].
|
||||||
|
The CLI docs can be found by going to Documentation > Reference > CLI.
|
||||||
|
For example, [v1.7 CLI][CLI].
|
||||||
|
|
||||||
|
## Sample Configuration
|
||||||
|
|
||||||
|
import Config from '@site/src/components/Config.js';
|
||||||
|
|
||||||
|
<Config data={{
|
||||||
|
"type": "talosctl",
|
||||||
|
"style": "powerline",
|
||||||
|
"powerline_symbol": "\uE0B0",
|
||||||
|
"foreground": "#FFFFFF",
|
||||||
|
"background": "#FF6C00",
|
||||||
|
"template": " \udb84\udcfe {{ .Context }}"
|
||||||
|
}}/>
|
||||||
|
|
||||||
|
## Template ([info][templates])
|
||||||
|
|
||||||
|
:::note default template
|
||||||
|
|
||||||
|
```template
|
||||||
|
{{ .Context }}
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
|
||||||
|
### Properties
|
||||||
|
|
||||||
|
| Name | Type | Description |
|
||||||
|
| ------------ | -------- | ------------------------------------- |
|
||||||
|
| `.Context` | `string` | the current talosctl context |
|
||||||
|
|
||||||
|
|
||||||
|
[templates]: /docs/configuration/templates
|
||||||
|
[talosctl]: https://www.talos.dev/
|
||||||
|
[CLI]: https://www.talos.dev/v1.7/reference/cli/
|
|
@ -123,6 +123,7 @@ module.exports = {
|
||||||
"segments/svn",
|
"segments/svn",
|
||||||
"segments/swift",
|
"segments/swift",
|
||||||
"segments/sysinfo",
|
"segments/sysinfo",
|
||||||
|
"segments/talosctl",
|
||||||
"segments/terraform",
|
"segments/terraform",
|
||||||
"segments/text",
|
"segments/text",
|
||||||
"segments/time",
|
"segments/time",
|
||||||
|
|
Loading…
Reference in a new issue