-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(azd): add Azure Developer CLI segment
resolves #4702
- Loading branch information
1 parent
85ca9c2
commit 59ebe57
Showing
5 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 Azd struct { | ||
props properties.Properties | ||
env platform.Environment | ||
|
||
azdConfig | ||
} | ||
|
||
type azdConfig struct { | ||
Version int `json:"version"` | ||
DefaultEnvironment string `json:"defaultEnvironment"` | ||
} | ||
|
||
func (t *Azd) Template() string { | ||
return " \uebd8 {{ .DefaultEnvironment }} " | ||
} | ||
|
||
func (t *Azd) Init(props properties.Properties, env platform.Environment) { | ||
t.props = props | ||
t.env = env | ||
} | ||
|
||
func (t *Azd) Enabled() bool { | ||
var parentFilePath string | ||
|
||
folders := t.props.GetStringArray(LanguageFolders, []string{".azure"}) | ||
for _, folder := range folders { | ||
if file, err := t.env.HasParentFilePath(folder); err == nil { | ||
parentFilePath = file.ParentFolder | ||
break | ||
} | ||
} | ||
|
||
if len(parentFilePath) == 0 { | ||
t.env.Debug("No .azure folder found in parent directories") | ||
return false | ||
} | ||
|
||
dotAzureFolder := filepath.Join(parentFilePath, ".azure") | ||
files := t.env.LsDir(dotAzureFolder) | ||
|
||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
|
||
if strings.EqualFold(file.Name(), "config.json") { | ||
return t.TryReadConfigJSON(filepath.Join(dotAzureFolder, file.Name())) | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (t *Azd) TryReadConfigJSON(file string) bool { | ||
if len(file) == 0 { | ||
return false | ||
} | ||
|
||
content := t.env.FileContent(file) | ||
var config azdConfig | ||
if err := json.Unmarshal([]byte(content), &config); err != nil { | ||
return false | ||
} | ||
|
||
t.azdConfig = config | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package segments | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"path/filepath" | ||
"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" | ||
mock2 "github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestAzdSegment(t *testing.T) { | ||
cases := []struct { | ||
Case string | ||
ExpectedEnabled bool | ||
ExpectedString string | ||
Template string | ||
IsInited bool | ||
}{ | ||
{ | ||
Case: "no .azure directory found", | ||
ExpectedEnabled: false, | ||
}, | ||
{ | ||
Case: "Environment located", | ||
ExpectedEnabled: true, | ||
ExpectedString: "TestEnvironment", | ||
Template: "{{ .DefaultEnvironment }}", | ||
IsInited: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
env := new(mock.MockedEnvironment) | ||
env.On("Debug", mock2.Anything) | ||
|
||
if tc.IsInited { | ||
fileInfo := &platform.FileInfo{ | ||
Path: "test/.azure", | ||
ParentFolder: "test", | ||
IsDir: true, | ||
} | ||
env.On("HasParentFilePath", ".azure").Return(fileInfo, nil) | ||
dirEntries := []fs.DirEntry{ | ||
&MockDirEntry{ | ||
name: "config.json", | ||
isDir: false, | ||
}, &MockDirEntry{ | ||
name: "TestEnvironment", | ||
isDir: true, | ||
}, | ||
} | ||
env.On("LsDir", filepath.Join("test", ".azure")).Return(dirEntries, nil) | ||
|
||
env.On("FileContent", filepath.Join("test", ".azure", "config.json")).Return(`{"version": 1, "defaultEnvironment": "TestEnvironment"}`, nil) | ||
} else { | ||
env.On("HasParentFilePath", ".azure").Return(&platform.FileInfo{}, errors.New("no such file or directory")) | ||
} | ||
|
||
azd := Azd{ | ||
env: env, | ||
props: properties.Map{}, | ||
} | ||
|
||
assert.Equal(t, tc.ExpectedEnabled, azd.Enabled(), tc.Case) | ||
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, azd), tc.Case) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
id: azd | ||
title: Azure Developer CLI | ||
sidebar_label: azd | ||
--- | ||
|
||
## What | ||
|
||
Display the currently active environment in the Azure Developer CLI. | ||
|
||
## Sample Configuration | ||
|
||
import Config from "@site/src/components/Config.js"; | ||
|
||
<Config | ||
data={{ | ||
type: "azd", | ||
style: "powerline", | ||
powerline_symbol: "\uE0B0", | ||
foreground: "#000000", | ||
background: "#9ec3f0", | ||
template: " \uebd8 {{ .DefaultEnvironment }} ", | ||
}} | ||
/> | ||
|
||
## Template ([info][templates]) | ||
|
||
:::note default template | ||
|
||
```template | ||
\uebd8 {{ .DefaultEnvironment }} | ||
``` | ||
|
||
::: | ||
|
||
### Properties | ||
|
||
| Name | Type | Description | | ||
| --------------------- | -------- | ------------------------------------ | | ||
| `.DefaultEnvironment` | `string` | Azure Developer CLI environment name | | ||
| `.Version` | `number` | Config version number | | ||
|
||
[templates]: /docs/configuration/templates | ||
[azd]: https://aka.ms/azd |