Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
add tests by pjaudiomv · Pull Request #48 · radiusmethod/awsd · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' add tests by pjaudiomv · Pull Request #48 · radiusmethod/awsd · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' add tests by pjaudiomv · Pull Request #48 · radiusmethod/awsd · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' add tests by pjaudiomv · Pull Request #48 · radiusmethod/awsd · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' add tests by pjaudiomv · Pull Request #48 · radiusmethod/awsd · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' add tests by pjaudiomv · Pull Request #48 · radiusmethod/awsd · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); add tests by pjaudiomv · Pull Request #48 · radiusmethod/awsd · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion .github/workflows/pull-request.yml
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
name: Lint
name: PR

on:
pull_request:
Expand All@@ -14,3 +14,20 @@ jobs:
go-version: '1.26'
- name: golangci-lint
uses: golangci/golangci-lint-action@v9

test:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v6
- uses: actions/setup-go@v6
with:
go-version: '1.26'
check-latest: true
cache: true
cache-dependency-path: |
**/go.sum
**/go.mod
- name: Run tests with coverage
run: |
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
1 change: 1 addition & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
coverage.*

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,3 +23,11 @@ uninstall: ## Uninstall Target
rm -f ${BINDIR}/_awsd
rm -f ${BINDIR}/_awsd_autocomplete
rm -f ${BINDIR}/_awsd_prompt

.PHONY: test test-coverage
test: ## Run tests
go test ./...

test-coverage: ## Run tests with coverage report
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
5 changes: 4 additions & 1 deletion go.mod
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,13 +5,16 @@ go 1.23.5
require (
github.com/radiusmethod/promptui v0.10.3
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
gopkg.in/ini.v1 v1.67.1
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/testify v1.11.1 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 2 additions & 10 deletions go.sum
Original file line numberDiff line numberDiff line change
Expand Up@@ -15,14 +15,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/radiusmethod/promptui v0.10.3 h1:JeayJuCR/bPvZp5cGqd+sAk4NDZG9CCt0vBVTFIiCo8=
github.com/radiusmethod/promptui v0.10.3/go.mod h1:DYozY3lsgSlf+M+LXX4QF7/QY246KqP69zhdIvPBg+8=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All@@ -32,16 +26,14 @@ github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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/ini.v1 v1.67.1 h1:tVBILHy0R6e4wkYOn3XmiITt/hEVH4TFMYvAX2Ytz6k=
gopkg.in/ini.v1 v1.67.1/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/list.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -26,7 +26,10 @@ func init() {
}

func runProfileLister() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
for _, p := range profiles {
fmt.Println(p)
}
Expand Down
67 changes: 67 additions & 0 deletions src/cmd/list_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
package cmd

import (
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestListCommand(t *testing.T) {
cmd := listCmd
assert.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
assert.Equal(t, "List AWS profiles command.", cmd.Short)
assert.Equal(t, "This lists all your AWS profiles.", cmd.Long)
assert.Equal(t, []string{"l"}, cmd.Aliases)
}

func TestRunProfileLister(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)

err := runProfileLister()
assert.NoError(t, err)
}

func TestListCommandIntegration(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

cmd := &cobra.Command{
Use: "awsd",
Short: "awsd - switch between AWS profiles.",
Long: "Allows for switching AWS profiles files.",
}

listCmd := &cobra.Command{
Use: "list",
Short: "List AWS profiles command.",
Aliases: []string{"l"},
Long: "This lists all your AWS profiles.",
Run: func(cmd *cobra.Command, args []string) {
err := runProfileLister()
if err != nil {
t.Fatal(err)
}
},
}
cmd.AddCommand(listCmd)

aliases := []string{"list", "l"}
for _, alias := range aliases {
t.Run(alias, func(t *testing.T) {
cmd.SetArgs([]string{alias})
err := cmd.Execute()
assert.NoError(t, err)
})
}
}
27 changes: 21 additions & 6 deletions src/cmd/root.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,12 @@ package cmd

import (
"fmt"
"log"
"os"

"github.com/radiusmethod/awsd/src/utils"
"github.com/radiusmethod/promptui"
"github.com/spf13/cobra"
"log"
"os"
)

var rootCmd = &cobra.Command{
Expand DownExpand Up@@ -39,7 +40,10 @@ func runRootCmd() {
}

func runProfileSwitcher() error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
fmt.Printf(utils.NoticeColor, "AWS Profile Switcher\n")
profile, err := getProfileFromPrompt(profiles)
if err != nil {
Expand All@@ -49,7 +53,11 @@ func runProfileSwitcher() error {
fmt.Printf(utils.NoticeColor, "? ")
fmt.Printf(utils.CyanColor, profile)
fmt.Println()
return utils.WriteFile(profile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(profile, homeDir)
}

func shouldRunDirectProfileSwitch() bool {
Expand All@@ -58,12 +66,19 @@ func shouldRunDirectProfileSwitch() bool {
}

func directProfileSwitch(desiredProfile string) error {
profiles := utils.GetProfiles()
profiles, err := utils.GetProfiles()
if err != nil {
return err
}
if utils.Contains(profiles, desiredProfile) {
printColoredMessage("Profile ", utils.PromptColor)
printColoredMessage(desiredProfile, utils.CyanColor)
printColoredMessage(" set.\n", utils.PromptColor)
return utils.WriteFile(desiredProfile, utils.GetHomeDir())
homeDir, err := utils.GetHomeDir()
if err != nil {
return err
}
return utils.WriteFile(desiredProfile, homeDir)
}
printColoredMessage("WARNING: Profile ", utils.NoticeColor)
printColoredMessage(desiredProfile, utils.CyanColor)
Expand Down
126 changes: 126 additions & 0 deletions src/cmd/root_test.go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
package cmd

import (
"os"
"path/filepath"
"testing"

"github.com/radiusmethod/awsd/src/utils/testutils"
"github.com/stretchr/testify/assert"
)

func TestShouldRunDirectProfileSwitch(t *testing.T) {
tests := []struct {
name string
args []string
expected bool
}{
{
name: "Direct profile switch",
args: []string{"awsd", "dev"},
expected: true,
},
{
name: "List command",
args: []string{"awsd", "list"},
expected: false,
},
{
name: "Help command",
args: []string{"awsd", "--help"},
expected: false,
},
{
name: "Version command",
args: []string{"awsd", "version"},
expected: false,
},
{
name: "No arguments",
args: []string{"awsd"},
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
os.Args = tt.args
result := shouldRunDirectProfileSwitch()
assert.Equal(t, tt.expected, result)
})
}
}

func TestDirectProfileSwitch(t *testing.T) {
tempDir := testutils.CreateTempDir(t)
defer testutils.CleanupTempDir(t, tempDir)

configPath := testutils.CreateMockAWSConfig(t, tempDir)
t.Setenv("AWS_CONFIG_FILE", configPath)
t.Setenv("HOME", tempDir)

tests := []struct {
name string
profile string
expectError bool
expectFile bool
expectContent string
}{
{
name: "Valid profile",
profile: "dev",
expectError: false,
expectFile: true,
expectContent: "dev",
},
{
name: "Invalid profile",
profile: "invalid",
expectError: false,
expectFile: false,
expectContent: "",
},
{
name: "Default profile",
profile: "default",
expectError: false,
expectFile: true,
expectContent: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
awsdFile := filepath.Join(tempDir, ".awsd")
_ = os.Remove(awsdFile)

err := directProfileSwitch(tt.profile)
if tt.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)

if tt.expectFile {
content, err := os.ReadFile(awsdFile)
assert.NoError(t, err)
assert.Equal(t, tt.expectContent, string(content))
} else {
_, err := os.Stat(awsdFile)
assert.True(t, os.IsNotExist(err), "File should not exist for invalid profile")
}
})
}
}

func TestRootCommand(t *testing.T) {
cmd := rootCmd
assert.NotNil(t, cmd)
assert.Equal(t, "awsd", cmd.Use)
assert.Equal(t, "awsd - switch between AWS profiles.", cmd.Short)
assert.Equal(t, "Allows for switching AWS profiles files.", cmd.Long)
}

func TestPrintColoredMessage(t *testing.T) {
printColoredMessage("test", "test")
}
Loading