Skip to content

Commit 642120f

Browse files
authored
use ini pkg, rm regex (#27)
1 parent e56474a commit 642120f

File tree

7 files changed

+61
-40
lines changed

7 files changed

+61
-40
lines changed

CHANGELOG.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
## v0.1.1 (June 28, 2024)
2+
* Replaced regular expressions with the `ini` package for extracting profiles from AWS config files.
3+
* Fixed an issue where extra spaces between the "profile" keyword and the profile name could prevent the profile from being set.
4+
15
## v0.1.0 (May 2, 2024)
2-
* Increase zsh autocompletion compatibilty.
6+
* Increase zsh autocompletion compatibility.
37

48
## v0.0.9 (April 4, 2024)
5-
* Fixes issue with help command shorthand flag `-h`. [#20] thanks @Masamerc
9+
* Fixes issue with help command shorthand flag `-h`. [#20] thanks, @Masamerc
610

711
## v0.0.8 (December 23, 2023)
812
* Added autocomplete script to install.
@@ -14,7 +18,7 @@
1418
* Refactored codebase.
1519

1620
## v0.0.5 (October 6, 2023)
17-
* Added support for passing arbitrary profile names as arguments. [#4] thanks @withakay
21+
* Added support for passing arbitrary profile names as arguments. [#4] thanks, @withakay
1822
* Added `awsd list` command to simply list all profiles.
1923

2024
## v0.0.4 (October 4, 2023)
@@ -24,7 +28,7 @@
2428
* Added additional error checking.
2529

2630
## v0.0.2 (May 27, 2022)
27-
* Added ability to search AWS profiles. [#4] thanks @M1kep
31+
* Added ability to search AWS profiles. [#4] thanks, @M1kep
2832

2933
## v0.0.1 (November 30, 2021)
3034
* Initial Release

README.md

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
# awsd - AWS Profile Switcher in Go
22

3+
---
4+
35
<img src="assets/awsd.png" width="200">
46

5-
awsd is a command-line utility that allows you to easily switch between AWS Profiles
7+
awsd is a command-line utility that allows you to easily switch between AWS Profiles.
68

79
<img src="assets/demo.gif" width="500">
810

11+
## Table of Contents
12+
13+
- [Installation](#installation)
14+
- [Homebrew](#homebrew)
15+
- [Makefile](#makefile)
16+
- [To Finish Installation](#to-finish-installation)
17+
- [Usage](#usage)
18+
- [Switching AWS Profiles](#switching-aws-profiles)
19+
- [Persist Profile across new shells](#persist-profile-across-new-shells)
20+
- [Show your AWS Profile in your shell prompt](#show-your-aws-profile-in-your-shell-prompt)
21+
- [Add autocompletion](#add-autocompletion)
22+
- [TL;DR (full config example)](#tldr-full-config-example)
23+
- [Contributing](#contributing)
24+
- [License](#license)
25+
926
## Installation
1027

1128
Make sure you have Go installed. You can download it from [here](https://golang.org/dl/).
@@ -49,16 +66,17 @@ To switch between different profiles files using the menu, use the following com
4966
awsd
5067
```
5168

52-
This command will display a list of available profiles files in your `~/aws/config` file. Select the one you want to use.
69+
This command will display a list of available profiles files in your `~/.aws/config` file or from `AWS_CONFIG_FILE`
70+
if you have that set. It expects for you to have named profiles in your AWS config file. Select the one you want to use.
5371

54-
## Persist Profile across new shells
72+
### Persist Profile across new shells
5573
To persist the set profile when you open new terminal windows, you can add the following to your bash profile or zshrc.
5674

5775
```bash
5876
export AWS_PROFILE=$(cat ~/.awsd)
5977
```
6078

61-
## Show your AWS Profile in your shell prompt
79+
### Show your AWS Profile in your shell prompt
6280
For better visibility into what your shell is set to it can be helpful to configure your prompt to show the value of the env variable `AWS_PROFILE`.
6381

6482
<img src="assets/screenshot.png" width="700">
@@ -78,7 +96,7 @@ function aws_prof {
7896
PROMPT='OTHER_PROMPT_STUFF $(aws_info)'
7997
```
8098

81-
## Add autocompletion
99+
### Add autocompletion
82100
You can add autocompletion when passing config as argument by adding the following to your bash profile or zshrc file.
83101
`source _awsd_autocomplete`
84102

@@ -95,7 +113,7 @@ complete -o nospace -F _awsd_completion "${AWSD_CMD}"
95113

96114
Now you can do `awsd my-p` and hit tab and if you had a profile `my-profile` it would autocomplete and find it.
97115

98-
## TL;DR (full config example)
116+
### TL;DR (full config example)
99117
```bash
100118
alias awsd="source _awsd"
101119
source _awsd_autocomplete

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ require (
1212
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1313
github.com/spf13/pflag v1.0.5 // indirect
1414
golang.org/x/sys v0.12.0 // indirect
15+
gopkg.in/ini.v1 v1.67.0 // indirect
1516
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBc
2828
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
2929
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3030
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
31+
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
32+
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
3133
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

renovate.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
{
22
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
33
"extends": [
4-
"config:base"
4+
"config:recommended"
5+
],
6+
"schedule": ["after 8am on Tuesday"],
7+
"packageRules": [
8+
{
9+
"updateTypes": ["major", "minor", "patch"],
10+
"schedule": ["after 8am on Tuesday"]
11+
}
512
]
613
}

src/cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"github.com/spf13/cobra"
66
)
77

8-
var version string = "v0.1.0"
8+
var version string = "v0.1.1"
99

1010
var versionCmd = &cobra.Command{
1111
Use: "version",

src/utils/aws.go

+17-28
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
11
package utils
22

33
import (
4-
"bufio"
4+
"gopkg.in/ini.v1"
55
"log"
6-
"os"
7-
"regexp"
86
"sort"
7+
"strings"
8+
)
9+
10+
const (
11+
profilePrefix = "profile"
12+
defaultProfile = "default"
913
)
1014

1115
func GetProfiles() []string {
1216
profileFileLocation := GetCurrentProfileFile()
13-
profiles := make([]string, 0)
14-
15-
file, err := os.Open(profileFileLocation)
17+
cfg, err := ini.Load(profileFileLocation)
1618
if err != nil {
17-
log.Fatal(err)
19+
log.Fatalf("Failed to load profiles: %v", err)
1820
}
19-
defer file.Close()
20-
21-
scanner := bufio.NewScanner(file)
22-
r, err := regexp.Compile(`\[profile .*]`)
23-
24-
if err != nil {
25-
log.Fatal(err)
26-
}
27-
28-
for scanner.Scan() {
29-
if r.MatchString(scanner.Text()) {
30-
s := scanner.Text()
31-
reg := regexp.MustCompile(`(\[profile )|(])`)
32-
res := reg.ReplaceAllString(s, "")
33-
profiles = append(profiles, res)
21+
sections := cfg.SectionStrings()
22+
profiles := make([]string, 0, len(sections)+1)
23+
for _, section := range sections {
24+
if strings.HasPrefix(section, profilePrefix) {
25+
trimmedProfile := strings.TrimPrefix(section, profilePrefix)
26+
trimmedProfile = strings.TrimSpace(trimmedProfile)
27+
profiles = append(profiles, trimmedProfile)
3428
}
3529
}
36-
37-
if err := scanner.Err(); err != nil {
38-
log.Fatal(err)
39-
}
40-
41-
profiles = AppendIfNotExists(profiles, "default")
30+
profiles = AppendIfNotExists(profiles, defaultProfile)
4231
sort.Strings(profiles)
4332
return profiles
4433
}

0 commit comments

Comments
 (0)