feat: new Sitecore context segment

This commit is contained in:
Bas Lijten 2023-05-03 19:43:20 +00:00 committed by Jan De Dobbeleer
parent f938794ccd
commit c5193c9880
8 changed files with 265 additions and 0 deletions

View file

@ -211,6 +211,8 @@ const (
SESSION SegmentType = "session"
// SHELL writes which shell we're currently in
SHELL SegmentType = "shell"
// SITECORE displays the current context for the Sitecore CLI
SITECORE SegmentType = "sitecore"
// SPOTIFY writes the SPOTIFY status for Mac
SPOTIFY SegmentType = "spotify"
// STRAVA is a sports activity tracker
@ -307,6 +309,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
SAPLING: func() SegmentWriter { return &segments.Sapling{} },
SESSION: func() SegmentWriter { return &segments.Session{} },
SHELL: func() SegmentWriter { return &segments.Shell{} },
SITECORE: func() SegmentWriter { return &segments.Sitecore{} },
SPOTIFY: func() SegmentWriter { return &segments.Spotify{} },
STRAVA: func() SegmentWriter { return &segments.Strava{} },
SVN: func() SegmentWriter { return &segments.Svn{} },

78
src/segments/sitecore.go Normal file
View file

@ -0,0 +1,78 @@
package segments
import (
"encoding/json"
"path/filepath"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)
type Sitecore struct {
props properties.Properties
env platform.Environment
Environment string
Cloud bool
}
type SitecoreConfig struct {
Endpoints struct {
Default struct {
Ref string `json:"ref"`
AllowWrite bool `json:"allowWrite"`
Host string `json:"host"`
Variables struct {
} `json:"variables"`
} `json:"default"`
} `json:"endpoints"`
}
func (s *Sitecore) Enabled() bool {
return s.shouldDisplay()
}
func (s *Sitecore) Template() string {
return " {{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }} "
}
func (s *Sitecore) Init(props properties.Properties, env platform.Environment) {
s.props = props
s.env = env
}
func (s *Sitecore) shouldDisplay() bool {
sitecoreDir, err := s.env.HasParentFilePath(".sitecore")
if err != nil {
return false
}
if !sitecoreDir.IsDir {
return false
}
if !s.env.HasFilesInDir(sitecoreDir.Path, "user.json") {
return false
}
sitecoreConfigFile := filepath.Join(sitecoreDir.Path, "user.json")
if len(sitecoreConfigFile) == 0 {
return false
}
content := s.env.FileContent(sitecoreConfigFile)
var config SitecoreConfig
if err := json.Unmarshal([]byte(content), &config); err != nil {
return false
}
// sitecore xm cloud always has sitecorecloud.io as domain
s.Cloud = strings.Contains(config.Endpoints.Default.Host, "sitecorecloud.io")
s.Environment = config.Endpoints.Default.Host
return true
}

View file

@ -0,0 +1,93 @@
package segments
import (
"errors"
"os"
"path/filepath"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/mock"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/stretchr/testify/assert"
)
func TestSitecoreSegment(t *testing.T) {
cases := []struct {
Case string
ExpectedEnabled bool
ExpectedString string
Template string
IsCloud bool
}{
{
Case: "no config files found",
ExpectedEnabled: false,
},
{
Case: "Sitecore local config",
ExpectedEnabled: true,
ExpectedString: "https://xmcloudcm.local false",
Template: "{{ .Environment }} {{ .Cloud }}",
IsCloud: false,
},
{
Case: "Sitecore cloud config",
ExpectedEnabled: true,
ExpectedString: "https://xmc-sitecore<someID>-projectName-environmentName.sitecorecloud.io true",
Template: "{{ .Environment }} {{ .Cloud }}",
IsCloud: true,
},
{
Case: "Sitecore cloud config - advanced template",
ExpectedEnabled: true,
ExpectedString: "sitecore<someID> - projectName - environmentName",
Template: "{{ if .Cloud }} {{ $splittedHostName := split \".\" .Environment }} {{ $myList := split \"-\" $splittedHostName._0 }} {{ $myList._1 }} - {{ $myList._2 }} - {{ $myList._3 }} {{ end }}", //nolint:lll
IsCloud: true,
},
}
for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("Home").Return(poshHome)
var sitecoreConfigFile string
if tc.IsCloud {
content, _ := os.ReadFile("../test/sitecoreUser1.json")
sitecoreConfigFile = string(content)
}
if !tc.IsCloud {
content, _ := os.ReadFile("../test/sitecoreUser2.json")
sitecoreConfigFile = string(content)
}
var projectDir *platform.FileInfo
var err error
if !tc.ExpectedEnabled {
err = errors.New("no config directory")
projectDir = nil
}
if tc.ExpectedEnabled {
err = nil
projectDir = &platform.FileInfo{
ParentFolder: "SitecoreProjectRoot",
Path: filepath.Join("SitecoreProjectRoot", ".sitecore"),
IsDir: true,
}
}
env.On("HasParentFilePath", ".sitecore").Return(projectDir, err)
env.On("HasFilesInDir", filepath.Join("SitecoreProjectRoot", ".sitecore"), "user.json").Return(true)
env.On("FileContent", filepath.Join("SitecoreProjectRoot", ".sitecore", "user.json")).Return(sitecoreConfigFile)
sitecore := &Sitecore{
env: env,
}
assert.Equal(t, tc.ExpectedEnabled, sitecore.Enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, sitecore), tc.Case)
}
}

View file

@ -0,0 +1,10 @@
{
"endpoints": {
"default": {
"ref": "xmcloud",
"allowWrite": true,
"host": "https://xmc-sitecore<someID>-projectName-environmentName.sitecorecloud.io",
"variables": {}
}
}
}

View file

@ -0,0 +1,10 @@
{
"endpoints": {
"default": {
"ref": "local",
"allowWrite": true,
"host": "https://xmcloudcm.local",
"variables": {}
}
}
}

View file

@ -1927,6 +1927,19 @@
}
}
},
{
"if": {
"properties": {
"type": {
"const": "sitecore"
}
}
},
"then": {
"title": "Sitecore Segment",
"description": "https://ohmyposh.dev/docs/segments/sitecore"
}
},
{
"if": {
"properties": {

View file

@ -0,0 +1,57 @@
---
id: sitecore
title: Sitecore
sidebar_label: Sitecore
---
## What
Show the current active Sitecore environment
## Sample Configuration
import Config from "@site/src/components/Config.js";
<Config
data={{
type: "sitecore",
style: "powerline",
powerline_symbol: "\uE0B0",
foreground: "#ffffff",
background: "#0077c2",
template:
" {{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }} ",
}}
/>
## Properties
There are no properties that can be set
## Template ([info][templates])
:::note default template
```template
{{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }}
```
:::
:::note advanced xmcloud template
When using xmcloud, the hostname can become very long. You can shorten `.Environment` by using the following template:
```template
{{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ if .Cloud }}{{ $splittedHostName := split \".\" .Environment }}{{ $myList := split \"-\" $splittedHostName._0 }} {{ $myList._1 }} - {{ $myList._2 }} - {{ $myList._3 }}{{ else }} {{ .Environment }} {{ end }}
```
:::
### Properties
| Name | Type | Description |
| -------------- | -------- | ------------------------------------------------------------------------------------------------------------- |
| `.Environment` | `string` | The hostname of the environment |
| `.Cloud` | `bool` | Used to determine if the environment is a cloud environment. Is true when hostname contains "sitecorecloud.io |
[templates]: /docs/configuration/templates

View file

@ -108,6 +108,7 @@ module.exports = {
"segments/sapling",
"segments/session",
"segments/shell",
"segments/sitecore",
"segments/spotify",
"segments/strava",
"segments/svn",