Skip to content

Commit

Permalink
Merge pull request #11 from riweston/improve-output
Browse files Browse the repository at this point in the history
Improve general UX with nicer colours and error handling
  • Loading branch information
riweston authored Jun 21, 2022
2 parents 6da513e + 79d41fb commit a3006e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.18.3
37 changes: 32 additions & 5 deletions cmd/aztx/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ import (
"github.com/ktr0731/go-fuzzyfinder"
)

const (
InfoColor = "\033[0;32m%s\033[0m"
NoticeColor = "\033[0;36m%s\033[0m"
WarningColor = "\033[1;33m%s\033[0m"
ErrorColor = "\033[1;31m%s\033[0m"
DebugColor = "\033[0;36m%s\033[0m"
)

type Subscription struct {
EnvironmentName string `json:"environmentName"`
HomeTenantID uuid.UUID `json:"homeTenantId"`
Expand Down Expand Up @@ -54,21 +62,27 @@ func SelectAzureAccountsDisplayName() {
}
azureProfile := home + "/.azure/azureProfile.json"
d := ReadAzureProfile(azureProfile)
currentCtx := ReadAzureProfileDefault(d)

idx, errFind := fuzzyfinder.Find(
idx, err := fuzzyfinder.Find(
d.Subscriptions,
func(i int) string {
return d.Subscriptions[i].Name
})
if errFind != nil {
panic(errFind)
},
fuzzyfinder.WithHeader(currentCtx))
if err != nil {
fmt.Printf(NoticeColor, "cancelled\n")
msg := fmt.Sprintf("%s\n", currentCtx)
fmt.Printf(InfoColor, msg)
return
}

errWrite := WriteAzureProfile(d, d.Subscriptions[idx].ID, azureProfile)
if errWrite != nil {
panic(errWrite)
}
fmt.Print(d.Subscriptions[idx].Name, "\n", d.Subscriptions[idx].ID, "\n")
msg := fmt.Sprintf("Set Context: %s (%s)\n", d.Subscriptions[idx].Name, d.Subscriptions[idx].ID)
fmt.Printf(InfoColor, msg)
}

func ReadAzureProfile(file string) File {
Expand All @@ -90,6 +104,19 @@ func ReadAzureProfile(file string) File {
return jsonData
}

func ReadAzureProfileDefault(file File) (subscription string) {
var subscriptionName string
var subscriptionID uuid.UUID

for idx := range file.Subscriptions {
if file.Subscriptions[idx].IsDefault {
subscriptionName = file.Subscriptions[idx].Name
subscriptionID = file.Subscriptions[idx].ID
}
}
return fmt.Sprintf("Current Context: %s (%s)", subscriptionName, subscriptionID)
}

func WriteAzureProfile(file File, id uuid.UUID, outFile string) error {
for idx := range file.Subscriptions {
if file.Subscriptions[idx].ID == id {
Expand Down

0 comments on commit a3006e9

Please sign in to comment.