Skip to content

Commit

Permalink
feat(flags): add profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Mar 6, 2024
1 parent 154f965 commit 789fc87
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 51 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ TUI directly.*

```bash
outtasync
# or
outtasync --config-file /path/to/config.yml
# or
outtasync -c /path/to/config.yml
outtasync -config-file /path/to/config.yml
outtasync -profiles qa,prod
```

4. Press `?` to view keyboard shortcuts to use the TUI.
Expand Down
9 changes: 8 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func expandTilde(path string) string {
return path
}

func ReadConfig(configFilePath string) ([]model.Stack, error) {
func ReadConfig(configFilePath string, profilesToFetch []string) ([]model.Stack, error) {
localFile, err := os.ReadFile(expandTilde(configFilePath))
if err != nil {
os.Exit(1)
Expand All @@ -46,10 +46,17 @@ func ReadConfig(configFilePath string) ([]model.Stack, error) {
if err != nil {
return nil, err
}
profilesMap := make(map[string]bool)
for _, p := range profilesToFetch {
profilesMap[p] = true
}

globalRefreshCmd := t.GlobalRefreshCommand
var rows []model.Stack
for _, profile := range t.Profiles {
if len(profilesToFetch) > 0 && !profilesMap[profile.Name] {
continue
}
for _, stack := range profile.Stacks {
var refreshCmd string
if stack.RefreshCommand != nil {
Expand Down
65 changes: 30 additions & 35 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,45 @@ import (
"fmt"
"os"
"os/user"
"strings"

"github.com/dhth/outtasync/ui"
"github.com/spf13/cobra"
)
"flag"

var (
rootCmd = &cobra.Command{
Use: "outtasync",
Short: "A TUI to identify cloudformation stacks that have gone out of sync with the state represented by their stack files.",
Version: "0.1",
Run: func(cmd *cobra.Command, args []string) {
configFilePath, err := cmd.Flags().GetString("config-file")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
_, err = os.Stat(configFilePath)
if os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Error: file not found at %s\n", configFilePath)
os.Exit(1)
}
stacks, err := ReadConfig(configFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading config: %v\n", err)
os.Exit(1)
}
ui.RenderUI(stacks)
},
}
"github.com/dhth/outtasync/ui"
)

func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
func die(msg string, args ...any) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}

func init() {
var configFilePath string
func Execute() {
currentUser, err := user.Current()
var defaultConfigFilePath string
if err == nil {
defaultConfigFilePath = fmt.Sprintf("%s/.config/outtasync.yml", currentUser.HomeDir)
}
rootCmd.Flags().StringVarP(&configFilePath, "config-file", "c", defaultConfigFilePath, "Path of the config file")
configFilePath := flag.String("config-file", defaultConfigFilePath, "path of the config file")
profiles := flag.String("profiles", "", "profiles to filter for")

flag.Parse()

_, err = os.Stat(*configFilePath)
if os.IsNotExist(err) {
die("Error: file not found at %s\n", *configFilePath)
}
var profilesToFetch []string
if *profiles != "" {
profilesToFetch = strings.Split(*profiles, ",")
}

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

}
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -28,7 +27,6 @@ require (
github.com/aws/smithy-go v1.20.1 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
Expand All @@ -40,7 +38,6 @@ require (
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.6 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.6.0 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down Expand Up @@ -74,13 +71,8 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down

0 comments on commit 789fc87

Please sign in to comment.