Skip to content

Commit

Permalink
feat: add regex filtering for stack names
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed May 8, 2024
1 parent 483a9e9 commit 5b38f9a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ CLI mode (ideal for running in a CI pipeline). TUI mode is the default.
outtasync
outtasync -config-file /path/to/config.yml
outtasync -profiles qa,prod
outtasync -p '<regex-pattern-for-stack-names>'
outtasync -p '.*(qa|staging)$'
```

### CLI Mode
Expand Down
6 changes: 5 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"os"
"os/user"
"regexp"
"strings"

"github.com/dhth/outtasync/ui"
Expand Down Expand Up @@ -34,7 +35,7 @@ func expandTilde(path string) string {
return path
}

func ReadConfig(configFilePath string, profilesToFetch []string) ([]ui.Stack, error) {
func ReadConfig(configFilePath string, profilesToFetch []string, pattern *regexp.Regexp) ([]ui.Stack, error) {
localFile, err := os.ReadFile(expandTilde(configFilePath))
if err != nil {
os.Exit(1)
Expand All @@ -56,6 +57,9 @@ func ReadConfig(configFilePath string, profilesToFetch []string) ([]ui.Stack, er
continue
}
for _, stack := range profile.Stacks {
if pattern != nil && !pattern.MatchString(stack.Name) {
continue
}
var refreshCmd string
if stack.RefreshCommand != nil {
refreshCmd = *stack.RefreshCommand
Expand Down
17 changes: 14 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"os/user"
"regexp"
"strings"

"flag"
Expand All @@ -18,7 +19,8 @@ func die(msg string, args ...any) {
}

var (
mode = flag.String("mode", "tui", "the mode to use; possible values: tui/cli")
mode = flag.String("mode", "tui", "the mode to use; possible values: tui/cli")
pattern = flag.String("p", "", "regex pattern to filter stack names")
)

func Execute() {
Expand All @@ -45,6 +47,15 @@ func Execute() {
die("config-file cannot be empty")
}

var regexPattern *regexp.Regexp

if *pattern != "" {
regexPattern, err = regexp.Compile(*pattern)
if err != nil {
die("Incorrect regex pattern provided: %q\n", err)
}
}

_, err = os.Stat(*configFilePath)
if os.IsNotExist(err) {
die(cfgErrSuggestion(fmt.Sprintf("Error: file doesn't exist at %q", *configFilePath)))
Expand All @@ -55,12 +66,12 @@ func Execute() {
profilesToFetch = strings.Split(*profiles, ",")
}

stacks, err := ReadConfig(*configFilePath, profilesToFetch)
stacks, err := ReadConfig(*configFilePath, profilesToFetch, regexPattern)
if err != nil {
die(cfgErrSuggestion(fmt.Sprintf("Error reading config: %v", *configFilePath)))
}
if len(stacks) == 0 {
die(cfgErrSuggestion(fmt.Sprintf("No stacks found for the requested parameters")))
die("No stacks found for the requested parameters")
}

awsCfgs := make(map[string]ui.AwsConfig)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/dhth/outtasync
go 1.22.0

require (
github.com/aws/aws-sdk-go-v2 v1.25.2
github.com/aws/aws-sdk-go-v2/config v1.27.4
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.46.1
github.com/charmbracelet/bubbles v0.18.0
Expand All @@ -13,7 +14,6 @@ require (

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aws/aws-sdk-go-v2 v1.25.2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.4 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.2 // indirect
Expand Down

0 comments on commit 5b38f9a

Please sign in to comment.