feat: add buf segment

This commit is contained in:
Jan De Dobbeleer 2023-01-21 09:25:04 +01:00 committed by Jan De Dobbeleer
parent 25b29bc5bd
commit ca57311ef5
5 changed files with 123 additions and 0 deletions

View file

@ -107,6 +107,8 @@ const (
BATTERY SegmentType = "battery"
// Brewfather segment
BREWFATHER SegmentType = "brewfather"
// Buf segment writes the active buf version
BUF SegmentType = "buf"
// cds (SAP CAP) version
CDS SegmentType = "cds"
// Cloud Foundry segment
@ -242,6 +244,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
AZFUNC: func() SegmentWriter { return &segments.AzFunc{} },
BATTERY: func() SegmentWriter { return &segments.Battery{} },
BREWFATHER: func() SegmentWriter { return &segments.Brewfather{} },
BUF: func() SegmentWriter { return &segments.Buf{} },
CDS: func() SegmentWriter { return &segments.Cds{} },
CF: func() SegmentWriter { return &segments.Cf{} },
CFTARGET: func() SegmentWriter { return &segments.CfTarget{} },

34
src/segments/buf.go Normal file
View file

@ -0,0 +1,34 @@
package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)
type Buf struct {
language
}
func (b *Buf) Template() string {
return languageTemplate
}
func (b *Buf) Init(props properties.Properties, env platform.Environment) {
b.language = language{
env: env,
props: props,
extensions: []string{"buf.yaml", "buf.gen.yaml", "buf.work.yaml"},
commands: []*cmd{
{
executable: "buf",
args: []string{"--version"},
regex: `(?:(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
},
},
versionURLTemplate: "https://github.com/bufbuild/buf/releases/tag/v{{.Full}}",
}
}
func (b *Buf) Enabled() bool {
return b.language.Enabled()
}

31
src/segments/buf_test.go Normal file
View file

@ -0,0 +1,31 @@
package segments
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuf(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Version string
}{
{Case: "Buf 1.12.0", ExpectedString: "1.12.0", Version: "1.12.0"},
}
for _, tc := range cases {
params := &mockedLanguageParams{
cmd: "buf",
versionParam: "--version",
versionOutput: tc.Version,
extension: "buf.yaml",
}
env, props := getMockedLanguageEnv(params)
b := &Buf{}
b.Init(props, env)
assert.True(t, b.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, renderTemplate(env, b.Template(), b), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}

View file

@ -0,0 +1,54 @@
---
id: buf
title: Buf
sidebar_label: Buf
---
## What
Display the currently active [Buf CLI][buf-docs] version.
## Sample Configuration
```json
{
"type": "buf",
"style": "plain",
"foreground": "#1000D6",
"template": " 🐃 {{ .Full }} "
}
```
## Properties
| Name | Type | Description |
| ---------------------- | --------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `home_enabled` | `boolean` | display the segment in the HOME folder or not - defaults to `false` |
| `fetch_version` | `boolean` | fetch the active version or not; useful if all you need is an icon indicating `buf` |
| `display_mode` | `string` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when `buf.yaml`, `buf.gen.yaml` or `buf.work.yaml` files are present (**default**)</li></ul> |
| `version_url_template` | `string` | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
## Template ([info][templates])
:::note default template
```template
{{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }}
```
:::
### Properties
| Name | Type | Description |
| -------- | -------- | -------------------------------------------------- |
| `.Full` | `string` | the full version |
| `.Major` | `string` | major number |
| `.Minor` | `string` | minor number |
| `.Patch` | `string` | patch number |
| `.URL` | `string` | URL of the version info / release notes |
| `.Error` | `string` | error encountered when fetching the version string |
[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates
[buf-docs]: https://buf.build/

View file

@ -57,6 +57,7 @@ module.exports = {
"segments/azfunc",
"segments/battery",
"segments/brewfather",
"segments/buf",
"segments/cds",
"segments/cf",
"segments/cftarget",