dynamo-browse/internal/ssm-browse/controllers/ssmcontroller.go
Leon Mika 79692302af
Issue 23: Added progress indicators and cancellation (#34)
- Wrapped all table operations in a new foreground job context, which mediates foreground tasks.
- Added cancellation support and partial results for table read operations.
- Added the "mark" command, which can mark, unmark & toggle marked items
- Added support for alias arguments.
- Removed the "unmark" command, and replaced it as an alias to the "marked" command
- Fixed seg faults raised when there is no table shown in the result set.
2022-10-10 10:15:25 +11:00

97 lines
2 KiB
Go

package controllers
import (
"context"
tea "github.com/charmbracelet/bubbletea"
"github.com/lmika/audax/internal/common/ui/events"
"github.com/lmika/audax/internal/ssm-browse/models"
"github.com/lmika/audax/internal/ssm-browse/services/ssmparameters"
"sync"
)
type SSMController struct {
service *ssmparameters.Service
// state
mutex *sync.Mutex
prefix string
}
func New(service *ssmparameters.Service) *SSMController {
return &SSMController{
service: service,
prefix: "/",
mutex: new(sync.Mutex),
}
}
func (c *SSMController) Fetch() tea.Cmd {
return func() tea.Msg {
res, err := c.service.List(context.Background(), c.prefix)
if err != nil {
return events.Error(err)
}
return NewParameterListMsg{
Prefix: c.prefix,
Parameters: res,
}
}
}
func (c *SSMController) ChangePrefix(newPrefix string) tea.Msg {
res, err := c.service.List(context.Background(), newPrefix)
if err != nil {
return events.Error(err)
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.prefix = newPrefix
return NewParameterListMsg{
Prefix: c.prefix,
Parameters: res,
}
}
func (c *SSMController) Clone(param models.SSMParameter) tea.Msg {
return events.PromptForInput("New key: ", func(value string) tea.Msg {
return func() tea.Msg {
ctx := context.Background()
if err := c.service.Clone(ctx, param, value); err != nil {
return events.Error(err)
}
res, err := c.service.List(context.Background(), c.prefix)
if err != nil {
return events.Error(err)
}
return NewParameterListMsg{
Prefix: c.prefix,
Parameters: res,
}
}
})
}
func (c *SSMController) DeleteParameter(param models.SSMParameter) tea.Msg {
return events.ConfirmYes("delete parameter? ", func() tea.Msg {
ctx := context.Background()
if err := c.service.Delete(ctx, param); err != nil {
return events.Error(err)
}
res, err := c.service.List(context.Background(), c.prefix)
if err != nil {
return events.Error(err)
}
return NewParameterListMsg{
Prefix: c.prefix,
Parameters: res,
}
})
}