mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-02 05:41:10 -08:00
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:
parent
14100e7dce
commit
6e9d072b5f
|
@ -2,39 +2,64 @@ package segments
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"path"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||
)
|
||||
|
||||
const (
|
||||
sitecoreFileName = "sitecore.json"
|
||||
sitecoreFolderName = ".sitecore"
|
||||
userFileName = "user.json"
|
||||
defaultEnpointName = "default"
|
||||
)
|
||||
|
||||
type Sitecore struct {
|
||||
props properties.Properties
|
||||
env platform.Environment
|
||||
|
||||
Environment string
|
||||
Cloud bool
|
||||
EndpointName string
|
||||
CmHost string
|
||||
}
|
||||
|
||||
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"`
|
||||
type EndpointConfig struct {
|
||||
Host string `json:"host"`
|
||||
}
|
||||
|
||||
type UserConfig struct {
|
||||
DefaultEndpoint string `json:"defaultEndpoint"`
|
||||
Endpoints map[string]EndpointConfig `json:"endpoints"`
|
||||
}
|
||||
|
||||
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 {
|
||||
return " {{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }} "
|
||||
return "{{ .EndpointName }} {{ if .CmHost }}({{ .CmHost }}){{ end }}"
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *Sitecore) shouldDisplay() bool {
|
||||
sitecoreDir, err := s.env.HasParentFilePath(".sitecore")
|
||||
if err != nil {
|
||||
return false
|
||||
func getUserConfig(s *Sitecore) (*UserConfig, error) {
|
||||
userJSON := s.env.FileContent(path.Join(sitecoreFolderName, userFileName))
|
||||
var userConfig UserConfig
|
||||
|
||||
if err := json.Unmarshal([]byte(userJSON), &userConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
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
|
||||
return &userConfig, nil
|
||||
}
|
||||
|
||||
func (u *UserConfig) getDefaultEndpoint() string {
|
||||
if len(u.DefaultEndpoint) > 0 {
|
||||
return u.DefaultEndpoint
|
||||
}
|
||||
|
||||
return defaultEnpointName
|
||||
}
|
||||
|
||||
func (u *UserConfig) getEndpoint(name string) *EndpointConfig {
|
||||
endpoint, exists := u.Endpoints[name]
|
||||
|
||||
if exists {
|
||||
return &endpoint
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,93 +1,162 @@
|
|||
package segments
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
func TestSitecoreSegment(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
ExpectedEnabled bool
|
||||
ExpectedString string
|
||||
Template string
|
||||
IsCloud bool
|
||||
Case string
|
||||
ExpectedString string
|
||||
ExpectedEnabled bool
|
||||
SitecoreFileExists 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",
|
||||
ExpectedEnabled: false,
|
||||
Case: "Disabled, user.json is empty",
|
||||
ExpectedString: "",
|
||||
ExpectedEnabled: false,
|
||||
SitecoreFileExists: true,
|
||||
UserFileExists: true,
|
||||
UserFileContent: "",
|
||||
},
|
||||
{
|
||||
Case: "Sitecore local config",
|
||||
ExpectedEnabled: true,
|
||||
ExpectedString: "https://xmcloudcm.local false",
|
||||
Template: "{{ .Environment }} {{ .Cloud }}",
|
||||
IsCloud: false,
|
||||
Case: "Disabled, user.json contains non-json text",
|
||||
ExpectedString: "",
|
||||
ExpectedEnabled: false,
|
||||
SitecoreFileExists: true,
|
||||
UserFileExists: true,
|
||||
UserFileContent: testUserJSONNotJSONFormat,
|
||||
},
|
||||
{
|
||||
Case: "Sitecore cloud config",
|
||||
ExpectedEnabled: true,
|
||||
ExpectedString: "https://xmc-sitecore<someID>-projectName-environmentName.sitecorecloud.io true",
|
||||
Template: "{{ .Environment }} {{ .Cloud }}",
|
||||
IsCloud: true,
|
||||
Case: "Disabled with default endpoint",
|
||||
ExpectedString: "default",
|
||||
ExpectedEnabled: false,
|
||||
SitecoreFileExists: true,
|
||||
UserFileExists: true,
|
||||
UserFileContent: testUserJSONOnlyDefaultEnv,
|
||||
DisplayDefault: false,
|
||||
},
|
||||
{
|
||||
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,
|
||||
Case: "Enabled, user.json initial state",
|
||||
ExpectedString: "default",
|
||||
ExpectedEnabled: true,
|
||||
SitecoreFileExists: 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 {
|
||||
env := new(mock.MockedEnvironment)
|
||||
env.On("Home").Return(poshHome)
|
||||
var sitecoreConfigFile string
|
||||
env.On("HasFiles", "sitecore.json").Return(tc.SitecoreFileExists)
|
||||
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 {
|
||||
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,
|
||||
props := properties.Map{
|
||||
properties.DisplayDefault: tc.DisplayDefault,
|
||||
}
|
||||
|
||||
sitecore := &Sitecore{}
|
||||
sitecore.Init(props, env)
|
||||
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"
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
|
|
@ -292,6 +292,7 @@
|
|||
"r",
|
||||
"sapling",
|
||||
"session",
|
||||
"sitecore",
|
||||
"spotify",
|
||||
"shell",
|
||||
"sysinfo",
|
||||
|
@ -1944,7 +1945,19 @@
|
|||
},
|
||||
"then": {
|
||||
"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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -6,52 +6,39 @@ sidebar_label: Sitecore
|
|||
|
||||
## 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
|
||||
|
||||
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 }} ",
|
||||
}}
|
||||
/>
|
||||
```json
|
||||
{
|
||||
"type": "sitecore",
|
||||
"style": "plain",
|
||||
"foreground": "#000000",
|
||||
"background": "#FFFFFF",
|
||||
"template": "Env: {{ .EndpointName }}{{ if .CmHost }} CM: {{ .CmHost }}{{ end }}"
|
||||
}
|
||||
```
|
||||
|
||||
## 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])
|
||||
|
||||
:::note default template
|
||||
|
||||
```template
|
||||
{{ if .Cloud }}\uf65e{{ else }}\uf98a{{ end }} {{ .Environment }}
|
||||
{{ .EndpointName }} {{ if .CmHost }}({{ .CmHost }}){{ end }}
|
||||
```
|
||||
|
||||
:::
|
||||
|
||||
:::note advanced xmcloud template
|
||||
When using xmcloud, the hostname can become very long. You can shorten `.Environment` by using the following template:
|
||||
## Properties
|
||||
|
||||
```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
|
||||
| Name | Type | Description |
|
||||
| -------------- | -------- | ---------------------------------------- |
|
||||
| `EndpointName` | `string` | name of the current Sitecore environment |
|
||||
| `CmHost` | `string` | host of the current Sitecore environment |
|
||||
|
|
Loading…
Reference in a new issue