Skip to content

Commit

Permalink
refactor: reuse aws configs
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Mar 8, 2024
1 parent d390021 commit a9eb25a
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ one deployment environment but not on others. 🤷
</p>


Install
💾 Installation
---

**homebrew**:
Expand Down
2 changes: 0 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"log"
"os"
"os/user"
"strings"
Expand All @@ -28,7 +27,6 @@ func expandTilde(path string) string {
if strings.HasPrefix(path, "~") {
usr, err := user.Current()
if err != nil {
log.Println("Failure reading config")
os.Exit(1)
}
return strings.Replace(path, "~", usr.HomeDir, 1)
Expand Down
26 changes: 19 additions & 7 deletions model/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@ import (
"context"
"os"

"github.com/aws/aws-sdk-go-v2/aws"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudformation"
tea "github.com/charmbracelet/bubbletea"
)

func GetCFTemplateBody(index int, stack Stack) tea.Cmd {
func getAWSConfigKey(stack Stack) string {
return stack.AwsProfile + ":" + stack.AwsRegion
}

func getAWSConfig(profile string, region string) (aws.Config, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
config.WithSharedConfigProfile(profile))
return cfg, err

}

func GetCFTemplateBody(awsConfig awsConfig, index int, stack Stack) tea.Cmd {
return func() tea.Msg {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(stack.AwsRegion),
config.WithSharedConfigProfile(stack.AwsProfile))
if err != nil {
return TemplateFetchedMsg{index, stack, "", false, err}

if awsConfig.err != nil {
return TemplateFetchedMsg{index, stack, "", false, awsConfig.err}
}

svc := cloudformation.NewFromConfig(cfg)
svc := cloudformation.NewFromConfig(awsConfig.config)

templateInput := cloudformation.GetTemplateInput{
StackName: &stack.Name,
Expand Down
11 changes: 11 additions & 0 deletions model/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ import (

func InitialModel(stacks []Stack) model {
stackItems := make([]list.Item, 0, len(stacks))
awsCfgs := make(map[string]awsConfig)

seen := make(map[string]bool)
for _, stack := range stacks {
stackItems = append(stackItems, stack)
configKey := getAWSConfigKey(stack)
if !seen[configKey] {
cfg, err := getAWSConfig(stack.AwsProfile, stack.AwsRegion)
awsCfgs[configKey] = awsConfig{cfg, err}
seen[configKey] = true
}
}

var appDelegateKeys = newAppDelegateKeyMap()
appDelegate := newAppItemDelegate(appDelegateKeys)

m := model{
awsConfigs: awsCfgs,
stacksList: list.New(stackItems, appDelegate, listWidth, 0),
}
m.stacksList.Title = "Stacks"
Expand Down
8 changes: 8 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import (
"github.com/aws/aws-sdk-go-v2/aws"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
)
Expand All @@ -11,7 +13,13 @@ const (
cfStacksList stateView = iota
)

type awsConfig struct {
config aws.Config
err error
}

type model struct {
awsConfigs map[string]awsConfig
state stateView
stacksList list.Model
message string
Expand Down
2 changes: 1 addition & 1 deletion model/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case CheckStackStatus:
msg.stack.FetchStatus = StatusFetching
m.stacksList.SetItem(msg.index, msg.stack)
return m, GetCFTemplateBody(msg.index, msg.stack)
return m, GetCFTemplateBody(m.awsConfigs[getAWSConfigKey(msg.stack)], msg.index, msg.stack)
case TemplateFetchedMsg:
if msg.err != nil {
msg.stack.Err = msg.err
Expand Down
9 changes: 9 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
)

func RenderUI(stacks []model.Stack) {
if len(os.Getenv("DEBUG")) > 0 {
f, err := tea.LogToFile("debug.log", "debug")
if err != nil {
fmt.Println("fatal:", err)
os.Exit(1)
}
defer f.Close()
}

p := tea.NewProgram(model.InitialModel(stacks), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there has been an error: %v", err)
Expand Down

0 comments on commit a9eb25a

Please sign in to comment.