feat(sitecore): support CLI context switching

BREAKING CHANGE: you will need to migrate your current sitecore segment template
 in case you have a custom one. Have a look at the documentation to see what changed.
This commit is contained in:
Bohdan Dorokhin 2023-06-08 08:40:05 +03:00 committed by GitHub
parent 14100e7dce
commit 6e9d072b5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 230 additions and 142 deletions

View file

@ -2,39 +2,64 @@ package segments
import ( import (
"encoding/json" "encoding/json"
"path/filepath" "path"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties" "github.com/jandedobbeleer/oh-my-posh/src/properties"
) )
const (
sitecoreFileName = "sitecore.json"
sitecoreFolderName = ".sitecore"
userFileName = "user.json"
defaultEnpointName = "default"
)
type Sitecore struct { type Sitecore struct {
props properties.Properties props properties.Properties
env platform.Environment env platform.Environment
Environment string EndpointName string
Cloud bool CmHost string
} }
type SitecoreConfig struct { type EndpointConfig struct {
Endpoints struct { Host string `json:"host"`
Default struct { }
Ref string `json:"ref"`
AllowWrite bool `json:"allowWrite"` type UserConfig struct {
Host string `json:"host"` DefaultEndpoint string `json:"defaultEndpoint"`
Variables struct { Endpoints map[string]EndpointConfig `json:"endpoints"`
} `json:"variables"`
} `json:"default"`
} `json:"endpoints"`
} }
func (s *Sitecore) Enabled() bool { func (s *Sitecore) Enabled() bool {
return s.shouldDisplay() if !s.env.HasFiles(sitecoreFileName) || !s.env.HasFiles(path.Join(sitecoreFolderName, userFileName)) {
return false
}
var userConfig, err = getUserConfig(s)
if err != nil {
return false
}
s.EndpointName = userConfig.getDefaultEndpoint()
displayDefault := s.props.GetBool(properties.DisplayDefault, true)
if !displayDefault && s.EndpointName == defaultEnpointName {
return false
}
if endpoint := userConfig.getEndpoint(s.EndpointName); endpoint != nil && len(endpoint.Host) > 0 {
s.CmHost = endpoint.Host
}
return true
} }
func (s *Sitecore) Template() string { func (s *Sitecore) Template() string {
return " {{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }} " return "{{ .EndpointName }} {{ if .CmHost }}({{ .CmHost }}){{ end }}"
} }
func (s *Sitecore) Init(props properties.Properties, env platform.Environment) { func (s *Sitecore) Init(props properties.Properties, env platform.Environment) {
@ -42,37 +67,31 @@ func (s *Sitecore) Init(props properties.Properties, env platform.Environment) {
s.env = env s.env = env
} }
func (s *Sitecore) shouldDisplay() bool { func getUserConfig(s *Sitecore) (*UserConfig, error) {
sitecoreDir, err := s.env.HasParentFilePath(".sitecore") userJSON := s.env.FileContent(path.Join(sitecoreFolderName, userFileName))
if err != nil { var userConfig UserConfig
return false
if err := json.Unmarshal([]byte(userJSON), &userConfig); err != nil {
return nil, err
} }
if !sitecoreDir.IsDir { return &userConfig, nil
return false }
}
func (u *UserConfig) getDefaultEndpoint() string {
if !s.env.HasFilesInDir(sitecoreDir.Path, "user.json") { if len(u.DefaultEndpoint) > 0 {
return false return u.DefaultEndpoint
} }
sitecoreConfigFile := filepath.Join(sitecoreDir.Path, "user.json") return defaultEnpointName
}
if len(sitecoreConfigFile) == 0 {
return false func (u *UserConfig) getEndpoint(name string) *EndpointConfig {
} endpoint, exists := u.Endpoints[name]
content := s.env.FileContent(sitecoreConfigFile) if exists {
return &endpoint
var config SitecoreConfig }
if err := json.Unmarshal([]byte(content), &config); err != nil {
return false return nil
}
// 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

@ -1,93 +1,162 @@
package segments package segments
import ( import (
"errors" "path"
"os"
"path/filepath"
"testing" "testing"
"github.com/jandedobbeleer/oh-my-posh/src/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"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSitecoreSegment(t *testing.T) { func TestSitecoreSegment(t *testing.T) {
cases := []struct { cases := []struct {
Case string Case string
ExpectedEnabled bool ExpectedString string
ExpectedString string ExpectedEnabled bool
Template string SitecoreFileExists bool
IsCloud bool UserFileExists bool
UserFileContent string
DisplayDefault bool
}{ }{
{Case: "Disabled, no sitecore.json file and user.json file", ExpectedString: "", ExpectedEnabled: false, SitecoreFileExists: false, UserFileExists: false},
{Case: "Disabled, only sitecore.json file exists", ExpectedString: "", ExpectedEnabled: false, SitecoreFileExists: true, UserFileExists: false},
{Case: "Disabled, only user.json file exists", ExpectedString: "", ExpectedEnabled: false, SitecoreFileExists: false, UserFileExists: true},
{ {
Case: "no config files found", Case: "Disabled, user.json is empty",
ExpectedEnabled: false, ExpectedString: "",
ExpectedEnabled: false,
SitecoreFileExists: true,
UserFileExists: true,
UserFileContent: "",
}, },
{ {
Case: "Sitecore local config", Case: "Disabled, user.json contains non-json text",
ExpectedEnabled: true, ExpectedString: "",
ExpectedString: "https://xmcloudcm.local false", ExpectedEnabled: false,
Template: "{{ .Environment }} {{ .Cloud }}", SitecoreFileExists: true,
IsCloud: false, UserFileExists: true,
UserFileContent: testUserJSONNotJSONFormat,
}, },
{ {
Case: "Sitecore cloud config", Case: "Disabled with default endpoint",
ExpectedEnabled: true, ExpectedString: "default",
ExpectedString: "https://xmc-sitecore<someID>-projectName-environmentName.sitecorecloud.io true", ExpectedEnabled: false,
Template: "{{ .Environment }} {{ .Cloud }}", SitecoreFileExists: true,
IsCloud: true, UserFileExists: true,
UserFileContent: testUserJSONOnlyDefaultEnv,
DisplayDefault: false,
}, },
{ {
Case: "Sitecore cloud config - advanced template", Case: "Enabled, user.json initial state",
ExpectedEnabled: true, ExpectedString: "default",
ExpectedString: "sitecore<someID> - projectName - environmentName", ExpectedEnabled: true,
Template: "{{ if .Cloud }} {{ $splittedHostName := split \".\" .Environment }} {{ $myList := split \"-\" $splittedHostName._0 }} {{ $myList._1 }} - {{ $myList._2 }} - {{ $myList._3 }} {{ end }}", //nolint:lll SitecoreFileExists: true,
IsCloud: true, UserFileExists: true,
UserFileContent: testUserJSONDefaultEmpty,
DisplayDefault: true,
},
{
Case: "Enabled, user.json with custom default endpoint and without endpoints",
ExpectedString: "MySuperEnv",
ExpectedEnabled: true,
SitecoreFileExists: true,
UserFileExists: true,
UserFileContent: testUserJSONCustomDefaultEnvWithoutEndpoints,
},
{
Case: "Enabled, user.json with custom default endpoint and configured endpoints",
ExpectedString: "myEnv (https://host.com)",
ExpectedEnabled: true,
SitecoreFileExists: true,
UserFileExists: true,
UserFileContent: testUserJSONCustomDefaultEnv,
},
{
Case: "Enabled, user.json with custom default endpoint and empty host",
ExpectedString: "envWithEmptyHost",
ExpectedEnabled: true,
SitecoreFileExists: true,
UserFileExists: true,
UserFileContent: testUserJSONCustomDefaultEnvAndEmptyHost,
}, },
} }
for _, tc := range cases { for _, tc := range cases {
env := new(mock.MockedEnvironment) env := new(mock.MockedEnvironment)
env.On("Home").Return(poshHome) env.On("HasFiles", "sitecore.json").Return(tc.SitecoreFileExists)
var sitecoreConfigFile string env.On("HasFiles", path.Join(".sitecore", "user.json")).Return(tc.UserFileExists)
env.On("FileContent", path.Join(".sitecore", "user.json")).Return(tc.UserFileContent)
if tc.IsCloud { props := properties.Map{
content, _ := os.ReadFile("../test/sitecoreUser1.json") properties.DisplayDefault: tc.DisplayDefault,
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,
} }
sitecore := &Sitecore{}
sitecore.Init(props, env)
assert.Equal(t, tc.ExpectedEnabled, sitecore.Enabled(), tc.Case) assert.Equal(t, tc.ExpectedEnabled, sitecore.Enabled(), tc.Case)
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, sitecore), tc.Case) assert.Equal(t, tc.ExpectedString, renderTemplate(env, sitecore.Template(), sitecore), tc.Case)
} }
} }
var testUserJSONDefaultEmpty = `
{
"endpoints": {}
}`
var testUserJSONCustomDefaultEnvWithoutEndpoints = `
{
"endpoints": {},
"defaultEndpoint": "MySuperEnv"
}`
var testUserJSONCustomDefaultEnv = `
{
"endpoints": {
"myEnv": {
"host": "https://host.com"
}
},
"defaultEndpoint": "myEnv"
}`
var testUserJSONCustomDefaultEnvAndEmptyHost = `
{
"endpoints": {
"myEnv": {
"host": ""
}
},
"defaultEndpoint": "envWithEmptyHost"
}`
var testUserJSONNotJSONFormat = `
---
doe: "a deer, a female deer"
ray: "a drop of golden sun"
pi: 3.14159
xmas: true
french-hens: 3
calling-birds:
- huey
- dewey
- louie
- fred
xmas-fifth-day:
calling-birds: four
french-hens: 3
golden-rings: 5
partridges:
count: 1
location: "a pear tree"
turtle-doves: two`
var testUserJSONOnlyDefaultEnv = `
{
"endpoints": {
"default": {
"host": "https://host.com"
}
}
}`

View file

@ -292,6 +292,7 @@
"r", "r",
"sapling", "sapling",
"session", "session",
"sitecore",
"spotify", "spotify",
"shell", "shell",
"sysinfo", "sysinfo",
@ -1944,7 +1945,19 @@
}, },
"then": { "then": {
"title": "Sitecore Segment", "title": "Sitecore Segment",
"description": "https://ohmyposh.dev/docs/segments/sitecore" "description": "https://ohmyposh.dev/docs/segments/sitecore",
"properties": {
"properties": {
"properties": {
"display_default": {
"type": "boolean",
"title": "Display Default",
"description": "Display the segment or not when the Sitecore environment name matches `default`",
"default": true
}
}
}
}
} }
}, },
{ {

View file

@ -6,52 +6,39 @@ sidebar_label: Sitecore
## What ## What
Show the current active Sitecore environment Display current Sitecore environment. Will not be active when sitecore.json and user.json don't exist.
## Sample Configuration ## Sample Configuration
import Config from "@site/src/components/Config.js"; ```json
{
<Config "type": "sitecore",
data={{ "style": "plain",
type: "sitecore", "foreground": "#000000",
style: "powerline", "background": "#FFFFFF",
powerline_symbol: "\uE0B0", "template": "Env: {{ .EndpointName }}{{ if .CmHost }} CM: {{ .CmHost }}{{ end }}"
foreground: "#ffffff", }
background: "#0077c2", ```
template:
" {{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }} ",
}}
/>
## Properties ## Properties
There are no properties that can be set | Name | Type | Description |
| ----------------- | --------- | -------------------------------------------------------------------------------------------------- |
| `display_default` | `boolean` | display the segment or not when the Sitecore environment name matches `default` - defaults to true |
## Template ([info][templates]) ## Template ([info][templates])
:::note default template :::note default template
```template ```template
{{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }} {{ .EndpointName }} {{ if .CmHost }}({{ .CmHost }}){{ end }}
``` ```
::: :::
:::note advanced xmcloud template ## Properties
When using xmcloud, the hostname can become very long. You can shorten `.Environment` by using the following template:
```template | Name | Type | Description |
{{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ if .Cloud }}{{ $splittedHostName := split \".\" .Environment }}{{ $myList := split \"-\" $splittedHostName._0 }} {{ $myList._1 }} - {{ $myList._2 }} - {{ $myList._3 }}{{ else }} {{ .Environment }} {{ end }} | -------------- | -------- | ---------------------------------------- |
``` | `EndpointName` | `string` | name of the current Sitecore environment |
| `CmHost` | `string` | host of the current Sitecore environment |
:::
### 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