Skip to content

Commit f5265bf

Browse files
authored
Merge pull request #415 from yurhasko/main
feat: segment-azure for displaying current active Azure subscription and resource group
2 parents 2766098 + 99ec84d commit f5265bf

File tree

6 files changed

+141
-6
lines changed

6 files changed

+141
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ Usage of powerline-go:
250250
(default "patched")
251251
-modules string
252252
The list of modules to load, separated by ','
253-
(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl)
253+
(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl, azure)
254254
Unrecognized modules will be invoked as 'powerline-go-MODULE' executable plugins and should output a (possibly empty) list of JSON objects that unmarshal to powerline-go's Segment structs.
255255
(default "venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root")
256256
-modules-right string
257257
The list of modules to load anchored to the right, for shells that support it, separated by ','
258-
(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, wsl)
258+
(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, wsl, azure)
259259
Unrecognized modules will be invoked as 'powerline-go-MODULE' executable plugins and should output a (possibly empty) list of JSON objects that unmarshal to powerline-go's Segment structs.
260260
-newline
261261
Show the prompt on a new line
@@ -268,7 +268,7 @@ Usage of powerline-go:
268268
Use '~' for your home dir. You may need to escape this character to avoid shell substitution.
269269
-priority string
270270
Segments sorted by priority, if not enough space exists, the least priorized segments are removed first. Separate with ','
271-
(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl)
271+
(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl, azure)
272272
(default "root,cwd,user,host,ssh,perms,git-branch,git-status,hg,jobs,exit,cwd-path")
273273
-shell string
274274
Set this to your shell type

args.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,19 @@ var args = arguments{
132132
"modules",
133133
strings.Join(defaults.Modules, ","),
134134
commentsWithDefaults("The list of modules to load, separated by ','",
135-
"(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl)",
135+
"(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl, azure)",
136136
"Unrecognized modules will be invoked as 'powerline-go-MODULE' executable plugins and should output a (possibly empty) list of JSON objects that unmarshal to powerline-go's Segment structs.")),
137137
ModulesRight: flag.String(
138138
"modules-right",
139139
strings.Join(defaults.ModulesRight, ","),
140140
comments("The list of modules to load anchored to the right, for shells that support it, separated by ','",
141-
"(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, wsl)",
141+
"(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, wsl, azure)",
142142
"Unrecognized modules will be invoked as 'powerline-go-MODULE' executable plugins and should output a (possibly empty) list of JSON objects that unmarshal to powerline-go's Segment structs.")),
143143
Priority: flag.String(
144144
"priority",
145145
strings.Join(defaults.Priority, ","),
146146
commentsWithDefaults("Segments sorted by priority, if not enough space exists, the least priorized segments are removed first. Separate with ','",
147-
"(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl)")),
147+
"(valid choices: aws, bzr, cwd, direnv, docker, docker-context, dotenv, duration, exit, fossil, gcp, git, gitlite, goenv, hg, host, jobs, kube, load, newline, nix-shell, node, perlbrew, perms, plenv, rbenv, root, rvm, shell-var, shenv, ssh, svn, termtitle, terraform-workspace, time, user, venv, vgo, vi-mode, wsl, azure)")),
148148
MaxWidthPercentage: flag.Int(
149149
"max-width",
150150
defaults.MaxWidthPercentage,

defaults.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ var defaults = Config{
208208
DotEnvFg: 15, // white
209209
DotEnvBg: 55, // purple
210210

211+
AzureFg: 15, // white
212+
AzureBg: 26, // Azure blue
213+
211214
AWSFg: 15, // white
212215
AWSBg: 172, // AWS orange
213216

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ var modules = map[string]func(*powerline) []pwl.Segment{
7878
"docker": segmentDocker,
7979
"docker-context": segmentDockerContext,
8080
"dotenv": segmentDotEnv,
81+
"azure": segmentAzure,
8182
"duration": segmentDuration,
8283
"exit": segmentExitCode,
8384
"fossil": segmentFossil,

segment-azure.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
10+
pwl "github.com/justjanne/powerline-go/powerline"
11+
)
12+
13+
// utf8BOM is the UTF-8 byte order mark that Azure CLI may add to JSON files
14+
var utf8BOM = []byte{0xEF, 0xBB, 0xBF}
15+
16+
type azureSubscription struct {
17+
ID string `json:"id"`
18+
Name string `json:"name"`
19+
State string `json:"state"`
20+
IsDefault bool `json:"isDefault"`
21+
}
22+
23+
type azureProfile struct {
24+
Subscriptions []azureSubscription `json:"subscriptions"`
25+
}
26+
27+
func getAzureSubscription() string {
28+
envSubID := os.Getenv("AZURE_SUBSCRIPTION_ID")
29+
30+
home, err := os.UserHomeDir()
31+
if err != nil {
32+
return envSubID
33+
}
34+
35+
data, err := os.ReadFile(filepath.Join(home, ".azure", "azureProfile.json"))
36+
if err != nil {
37+
return envSubID
38+
}
39+
40+
data = bytes.TrimPrefix(data, utf8BOM)
41+
42+
var profile azureProfile
43+
if err := json.Unmarshal(data, &profile); err != nil {
44+
return envSubID
45+
}
46+
47+
if envSubID != "" {
48+
for _, sub := range profile.Subscriptions {
49+
if sub.ID == envSubID {
50+
return sub.Name
51+
}
52+
}
53+
return envSubID
54+
}
55+
56+
var firstEnabled string
57+
for _, sub := range profile.Subscriptions {
58+
if sub.State != "Enabled" {
59+
continue
60+
}
61+
if sub.IsDefault {
62+
return sub.Name
63+
}
64+
if firstEnabled == "" {
65+
firstEnabled = sub.Name
66+
}
67+
}
68+
69+
return firstEnabled
70+
}
71+
72+
func getAzureResourceGroup() string {
73+
if rg := os.Getenv("AZURE_DEFAULTS_GROUP"); rg != "" {
74+
return rg
75+
}
76+
77+
home, err := os.UserHomeDir()
78+
if err != nil {
79+
return ""
80+
}
81+
82+
data, err := os.ReadFile(filepath.Join(home, ".azure", "config"))
83+
if err != nil {
84+
return ""
85+
}
86+
87+
inDefaults := false
88+
for _, line := range strings.Split(string(data), "\n") {
89+
line = strings.TrimSpace(line)
90+
91+
if line == "[defaults]" {
92+
inDefaults = true
93+
continue
94+
}
95+
96+
if strings.HasPrefix(line, "[") {
97+
inDefaults = false
98+
continue
99+
}
100+
101+
if inDefaults && (strings.HasPrefix(line, "group ") || strings.HasPrefix(line, "group=")) {
102+
if parts := strings.SplitN(line, "=", 2); len(parts) == 2 {
103+
return strings.TrimSpace(parts[1])
104+
}
105+
}
106+
}
107+
108+
return ""
109+
}
110+
111+
func segmentAzure(p *powerline) []pwl.Segment {
112+
subscription := getAzureSubscription()
113+
if subscription == "" {
114+
return []pwl.Segment{}
115+
}
116+
117+
content := subscription
118+
if rg := getAzureResourceGroup(); rg != "" {
119+
content += " (" + rg + ")"
120+
}
121+
122+
return []pwl.Segment{{
123+
Name: "azure",
124+
Content: content,
125+
Foreground: p.theme.AzureFg,
126+
Background: p.theme.AzureBg,
127+
}}
128+
}

themes.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ type Theme struct {
7878
DotEnvFg uint8
7979
DotEnvBg uint8
8080

81+
AzureFg uint8
82+
AzureBg uint8
83+
8184
AWSFg uint8
8285
AWSBg uint8
8386

0 commit comments

Comments
 (0)