From 1312b48fc5c10479f56a3875c7b52eca56282a37 Mon Sep 17 00:00:00 2001 From: Richard Weston Date: Tue, 21 Jun 2022 21:19:34 +0100 Subject: [PATCH 1/7] feat: Add asdf file to help with managing go version --- .tool-versions | 1 + 1 file changed, 1 insertion(+) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..00c8508 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +golang 1.18.3 From 5324d57b75093688e4ba2261dc8939d1d38a0169 Mon Sep 17 00:00:00 2001 From: Richard Weston Date: Tue, 21 Jun 2022 21:20:03 +0100 Subject: [PATCH 2/7] feat: Add some colours for text output --- cmd/aztx/contexts.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/aztx/contexts.go b/cmd/aztx/contexts.go index 7810347..12a21a6 100644 --- a/cmd/aztx/contexts.go +++ b/cmd/aztx/contexts.go @@ -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"` From 963d256237859554154f808088a18770257bc9e6 Mon Sep 17 00:00:00 2001 From: Richard Weston Date: Tue, 21 Jun 2022 21:23:06 +0100 Subject: [PATCH 3/7] feat: Add a helper function to return the current subscription context --- cmd/aztx/contexts.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cmd/aztx/contexts.go b/cmd/aztx/contexts.go index 12a21a6..3d6c374 100644 --- a/cmd/aztx/contexts.go +++ b/cmd/aztx/contexts.go @@ -98,6 +98,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 { From 1ae304d7a3fda9b17720785708a58428b24dd787 Mon Sep 17 00:00:00 2001 From: Richard Weston Date: Tue, 21 Jun 2022 21:23:46 +0100 Subject: [PATCH 4/7] feat: Improve output from a successful selection --- cmd/aztx/contexts.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/aztx/contexts.go b/cmd/aztx/contexts.go index 3d6c374..2ad8eb5 100644 --- a/cmd/aztx/contexts.go +++ b/cmd/aztx/contexts.go @@ -76,7 +76,8 @@ func SelectAzureAccountsDisplayName() { 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 { From 7f608fbc704d4cec6b772ed8e3b465e464f965c0 Mon Sep 17 00:00:00 2001 From: Richard Weston Date: Tue, 21 Jun 2022 21:24:28 +0100 Subject: [PATCH 5/7] feat: Add current context for general UX improvement --- cmd/aztx/contexts.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/aztx/contexts.go b/cmd/aztx/contexts.go index 2ad8eb5..e6a34ef 100644 --- a/cmd/aztx/contexts.go +++ b/cmd/aztx/contexts.go @@ -62,6 +62,7 @@ func SelectAzureAccountsDisplayName() { } azureProfile := home + "/.azure/azureProfile.json" d := ReadAzureProfile(azureProfile) + currentCtx := ReadAzureProfileDefault(d) idx, errFind := fuzzyfinder.Find( d.Subscriptions, @@ -70,6 +71,7 @@ func SelectAzureAccountsDisplayName() { }) if errFind != nil { panic(errFind) + fuzzyfinder.WithHeader(currentCtx)) } errWrite := WriteAzureProfile(d, d.Subscriptions[idx].ID, azureProfile) From 8d6e5d8d452f155d8c4d05a9e246619a0c55a2e6 Mon Sep 17 00:00:00 2001 From: Richard Weston Date: Tue, 21 Jun 2022 21:25:07 +0100 Subject: [PATCH 6/7] fix: Improve the experience when cancelling out --- cmd/aztx/contexts.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/aztx/contexts.go b/cmd/aztx/contexts.go index e6a34ef..fecc7ba 100644 --- a/cmd/aztx/contexts.go +++ b/cmd/aztx/contexts.go @@ -64,14 +64,17 @@ func SelectAzureAccountsDisplayName() { 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) From 79d41fbc4bbfb4c0849c3f0af2742da3f7f2420e Mon Sep 17 00:00:00 2001 From: Richard Weston Date: Tue, 21 Jun 2022 21:59:55 +0100 Subject: [PATCH 7/7] fix: Variable renamed to pass linting test --- cmd/aztx/contexts.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/aztx/contexts.go b/cmd/aztx/contexts.go index fecc7ba..fbeaf04 100644 --- a/cmd/aztx/contexts.go +++ b/cmd/aztx/contexts.go @@ -106,15 +106,15 @@ func ReadAzureProfile(file string) File { func ReadAzureProfileDefault(file File) (subscription string) { var subscriptionName string - var subscriptionId uuid.UUID + var subscriptionID uuid.UUID for idx := range file.Subscriptions { if file.Subscriptions[idx].IsDefault { subscriptionName = file.Subscriptions[idx].Name - subscriptionId = file.Subscriptions[idx].ID + subscriptionID = file.Subscriptions[idx].ID } } - return fmt.Sprintf("Current Context: %s (%s)", subscriptionName, subscriptionId) + return fmt.Sprintf("Current Context: %s (%s)", subscriptionName, subscriptionID) } func WriteAzureProfile(file File, id uuid.UUID, outFile string) error {