Skip to content
This repository has been archived by the owner on Aug 9, 2023. It is now read-only.

Commit

Permalink
Cleanup of helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
StiviiK committed Aug 9, 2022
1 parent b524a47 commit 5cb40c5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,20 @@ require (
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
)

require (
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2
github.com/chzyer/readline v1.5.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sys v0.0.0-20220702020025-31831981b65f // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
3 changes: 2 additions & 1 deletion pkg/azurecli.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ func GetAzureSubscriptionByName(profilesConfig AzureProfilesConfig, subscription
// TryFindAzureSubscription fuzzy searches for the azure subscription in the given AzureProfilesConfig
func TryFindAzureSubscription(profilesConfig AzureProfilesConfig, subscriptionName string) ([]Subscription, error) {
// Fuzzy search for the subscription name
results := fuzzy.FindNormalized(strings.ToLower(subscriptionName), utils.LowercaseStrings(GetAzureSubscriptionNames(profilesConfig.Subscriptions)))
var subscriptionNames utils.StringSlice = GetAzureSubscriptionNames(profilesConfig.Subscriptions)
results := fuzzy.FindNormalized(strings.ToLower(subscriptionName), subscriptionNames.ToLower())
switch len(results) {
case 0:
// No results found
Expand Down
4 changes: 2 additions & 2 deletions pkg/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func BuildPrompt(subscriptions []Subscription) promptui.Select {
sort.Sort(subscriptionSorter(subscriptions))

// Build the prompt
subscriptionNames := GetAzureSubscriptionNames(subscriptions)
maxContextLength := utils.GetLongestStringLength(subscriptionNames)
var subscriptionNames utils.StringSlice = GetAzureSubscriptionNames(subscriptions)
maxContextLength := subscriptionNames.LongestStringLength()

return promptui.Select{
Label: fmt.Sprint("Name" + strings.Repeat(" ", maxContextLength-4) + " | " + "SubscriptionId" + strings.Repeat(" ", 36-14) + " | " + "TenantId" + strings.Repeat(" ", 36-8)),
Expand Down
12 changes: 8 additions & 4 deletions utils/stdout.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package utils

import "github.com/chzyer/readline"
import "os"

// charBell is the ASCII code for the bell character (\a)
const charBell = 7

// noBellStdout is a wrapper around os.Stdout that suppresses the bell character (\a)
type noBellStdout struct{}

func (n *noBellStdout) Write(p []byte) (int, error) {
if len(p) == 1 && p[0] == readline.CharBell {
if len(p) == 1 && p[0] == charBell {
return 0, nil
}
return readline.Stdout.Write(p)
return os.Stdout.Write(p)
}

func (n *noBellStdout) Close() error {
return readline.Stdout.Close()
return os.Stdout.Close()
}

// NoBellStdout returns a stdout wrapper that doesn't ring the terminal bell
Expand Down
17 changes: 10 additions & 7 deletions utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ package utils

import "strings"

// GetLongestStringLength returns the length of the longest string in the given slice
func GetLongestStringLength(strings []string) int {
// StringSlice is a slice of strings, helper type used for extension methods
type StringSlice []string

// LongestStringLength returns the length of the longest string in the given slice
func (slice StringSlice) LongestStringLength() int {
longestLength := 0
for _, s := range strings {
for _, s := range slice {
if len(s) > longestLength {
longestLength = len(s)
}
}
return longestLength
}

// LowercaseStrings returns a copy of the given slice of strings with all strings lowercased
func LowercaseStrings(input []string) []string {
lowercaseStrings := make([]string, len(input))
for i, s := range input {
// ToLower returns a copy of the given slice of strings with all strings lowercased
func (slice StringSlice) ToLower() StringSlice {
lowercaseStrings := make(StringSlice, len(slice))
for i, s := range slice {
lowercaseStrings[i] = strings.ToLower(s)
}
return lowercaseStrings
Expand Down

0 comments on commit 5cb40c5

Please sign in to comment.