Skip to content

Commit

Permalink
use ini pkg, rm regex
Browse files Browse the repository at this point in the history
trim
  • Loading branch information
pjaudiomv committed Jun 28, 2024
1 parent e56474a commit a59238e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
45 changes: 17 additions & 28 deletions src/utils/aws.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,33 @@
package utils

import (
"bufio"
"gopkg.in/ini.v1"
"log"
"os"
"regexp"
"sort"
"strings"
)

const (
profilePrefix = "profile"
defaultProfile = "default"
)

func GetProfiles() []string {
profileFileLocation := GetCurrentProfileFile()
profiles := make([]string, 0)

file, err := os.Open(profileFileLocation)
cfg, err := ini.Load(profileFileLocation)
if err != nil {
log.Fatal(err)
log.Fatalf("Failed to load profiles: %v", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
r, err := regexp.Compile(`\[profile .*]`)

if err != nil {
log.Fatal(err)
}

for scanner.Scan() {
if r.MatchString(scanner.Text()) {
s := scanner.Text()
reg := regexp.MustCompile(`(\[profile )|(])`)
res := reg.ReplaceAllString(s, "")
profiles = append(profiles, res)
sections := cfg.SectionStrings()
profiles := make([]string, 0, len(sections)+1)
for _, section := range sections {
if strings.HasPrefix(section, profilePrefix) {
trimmedProfile := strings.TrimPrefix(section, profilePrefix)
trimmedProfile = strings.TrimSpace(trimmedProfile)
profiles = append(profiles, trimmedProfile)
}
}

if err := scanner.Err(); err != nil {
log.Fatal(err)
}

profiles = AppendIfNotExists(profiles, "default")
profiles = AppendIfNotExists(profiles, defaultProfile)
sort.Strings(profiles)
return profiles
}

0 comments on commit a59238e

Please sign in to comment.