Skip to content

Commit

Permalink
feat(azd): add Azure Developer CLI segment
Browse files Browse the repository at this point in the history
resolves #4702
  • Loading branch information
aaronpowell authored and JanDeDobbeleer committed Jun 12, 2024
1 parent 85ca9c2 commit 59ebe57
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/engine/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const (
AWS SegmentType = "aws"
// AZ writes the Azure subscription info we're currently in
AZ SegmentType = "az"
// AZD writes the Azure Developer CLI environment info we're current in
AZD SegmentType = "azd"
// AZFUNC writes current AZ func version
AZFUNC SegmentType = "azfunc"
// BATTERY writes the battery percentage
Expand Down Expand Up @@ -278,6 +280,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
ARGOCD: func() SegmentWriter { return &segments.Argocd{} },
AWS: func() SegmentWriter { return &segments.Aws{} },
AZ: func() SegmentWriter { return &segments.Az{} },
AZD: func() SegmentWriter { return &segments.Azd{} },
AZFUNC: func() SegmentWriter { return &segments.AzFunc{} },
BATTERY: func() SegmentWriter { return &segments.Battery{} },
BAZEL: func() SegmentWriter { return &segments.Bazel{} },
Expand Down
78 changes: 78 additions & 0 deletions src/segments/azd.go
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
}
72 changes: 72 additions & 0 deletions src/segments/azd_test.go
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)
}
}
19 changes: 19 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@
"description": "https://ohmyposh.dev/docs/configuration/segment",
"enum": [
"az",
"azd",
"aws",
"azfunc",
"argocd",
Expand Down Expand Up @@ -651,6 +652,24 @@
}
}
},
{
"if": {
"properties": {
"type": {
"const": "azd"
}
}
},
"then": {
"title": "Azure Developer CLI Segment",
"description": "https://ohmyposh.dev/docs/segments/azd",
"properties": {
"properties": {
"properties": { }
}
}
}
},
{
"if": {
"properties": {
Expand Down
44 changes: 44 additions & 0 deletions website/docs/segments/azd.mdx
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

0 comments on commit 59ebe57

Please sign in to comment.