Merge pull request #12 from lmika/chore/ctrlret
ctrlret: replaced return types of controllers from tea.Cmd to tea.Msg
This commit is contained in:
commit
f0bd3022cc
|
@ -50,7 +50,7 @@ func main() {
|
|||
cmdController := commandctrl.NewCommandController()
|
||||
cmdController.AddCommands(&commandctrl.CommandContext{
|
||||
Commands: map[string]commandctrl.Command{
|
||||
"cd": func(args []string) tea.Cmd {
|
||||
"cd": func(args []string) tea.Msg {
|
||||
return ctrl.ChangePrefix(args[0])
|
||||
},
|
||||
},
|
||||
|
|
|
@ -25,18 +25,16 @@ func (c *CommandController) AddCommands(ctx *CommandContext) {
|
|||
c.commandList = ctx
|
||||
}
|
||||
|
||||
func (c *CommandController) Prompt() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *CommandController) Prompt() tea.Msg {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: ":",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
return c.Execute(value)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CommandController) Execute(commandInput string) tea.Cmd {
|
||||
func (c *CommandController) Execute(commandInput string) tea.Msg {
|
||||
input := strings.TrimSpace(commandInput)
|
||||
if input == "" {
|
||||
return nil
|
||||
|
@ -46,18 +44,18 @@ func (c *CommandController) Execute(commandInput string) tea.Cmd {
|
|||
command := c.lookupCommand(tokens[0])
|
||||
if command == nil {
|
||||
log.Println("No such command: ", tokens)
|
||||
return events.SetError(errors.New("no such command: " + tokens[0]))
|
||||
return events.Error(errors.New("no such command: " + tokens[0]))
|
||||
}
|
||||
|
||||
return command(tokens[1:])
|
||||
}
|
||||
|
||||
func (c *CommandController) Alias(commandName string) Command {
|
||||
return func(args []string) tea.Cmd {
|
||||
return func(args []string) tea.Msg {
|
||||
command := c.lookupCommand(commandName)
|
||||
if command == nil {
|
||||
log.Println("No such command: ", commandName)
|
||||
return events.SetError(errors.New("no such command: " + commandName))
|
||||
return events.Error(errors.New("no such command: " + commandName))
|
||||
}
|
||||
|
||||
return command(args)
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package commandctrl_test
|
||||
|
||||
import (
|
||||
"github.com/lmika/audax/internal/common/ui/events"
|
||||
"testing"
|
||||
|
||||
"github.com/lmika/audax/internal/common/ui/commandctrl"
|
||||
"github.com/lmika/audax/internal/common/ui/events"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -12,7 +12,7 @@ func TestCommandController_Prompt(t *testing.T) {
|
|||
t.Run("prompt user for a command", func(t *testing.T) {
|
||||
cmd := commandctrl.NewCommandController()
|
||||
|
||||
res := cmd.Prompt()()
|
||||
res := cmd.Prompt()
|
||||
|
||||
promptForInputMsg, ok := res.(events.PromptForInputMsg)
|
||||
assert.True(t, ok)
|
||||
|
|
|
@ -2,11 +2,11 @@ package commandctrl
|
|||
|
||||
import tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
type Command func(args []string) tea.Cmd
|
||||
type Command func(args []string) tea.Msg
|
||||
|
||||
func NoArgCommand(cmd tea.Cmd) Command {
|
||||
return func(args []string) tea.Cmd {
|
||||
return cmd
|
||||
return func(args []string) tea.Msg {
|
||||
return cmd()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,29 +10,19 @@ func Error(err error) tea.Msg {
|
|||
return ErrorMsg(err)
|
||||
}
|
||||
|
||||
func SetError(err error) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
return Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func SetStatus(msg string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func SetStatus(msg string) tea.Msg {
|
||||
return StatusMsg(msg)
|
||||
}
|
||||
}
|
||||
|
||||
func PromptForInput(prompt string, onDone func(value string) tea.Cmd) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func PromptForInput(prompt string, onDone func(value string) tea.Msg) tea.Msg {
|
||||
return PromptForInputMsg{
|
||||
Prompt: prompt,
|
||||
OnDone: onDone,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Confirm(prompt string, onYes func() tea.Cmd) tea.Cmd {
|
||||
return PromptForInput(prompt, func(value string) tea.Cmd {
|
||||
func Confirm(prompt string, onYes func() tea.Msg) tea.Msg {
|
||||
return PromptForInput(prompt, func(value string) tea.Msg {
|
||||
if value == "y" {
|
||||
return onYes()
|
||||
}
|
||||
|
|
|
@ -16,5 +16,5 @@ type ModeMessage string
|
|||
// PromptForInput indicates that the context is requesting a line of input
|
||||
type PromptForInputMsg struct {
|
||||
Prompt string
|
||||
OnDone func(value string) tea.Cmd
|
||||
OnDone func(value string) tea.Msg
|
||||
}
|
||||
|
|
|
@ -15,9 +15,9 @@ func (ps *promptSequence) next() tea.Msg {
|
|||
if len(ps.receivedValues) < len(ps.prompts) {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: ps.prompts[len(ps.receivedValues)],
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
ps.receivedValues = append(ps.receivedValues, value)
|
||||
return ps.next
|
||||
return ps.next()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ type SetReadWrite struct {
|
|||
|
||||
type PromptForTableMsg struct {
|
||||
Tables []string
|
||||
OnSelected func(tableName string) tea.Cmd
|
||||
OnSelected func(tableName string) tea.Msg
|
||||
}
|
||||
|
||||
type ResultSetUpdated struct {
|
||||
|
|
|
@ -35,7 +35,7 @@ func NewTableReadController(state *State, tableService TableReadService, workspa
|
|||
}
|
||||
|
||||
// Init does an initial scan of the table. If no table is specified, it prompts for a table, then does a scan.
|
||||
func (c *TableReadController) Init() tea.Cmd {
|
||||
func (c *TableReadController) Init() tea.Msg {
|
||||
if c.tableName == "" {
|
||||
return c.ListTables()
|
||||
} else {
|
||||
|
@ -43,8 +43,7 @@ func (c *TableReadController) Init() tea.Cmd {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) ListTables() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) ListTables() tea.Msg {
|
||||
tables, err := c.tableService.ListTables(context.Background())
|
||||
if err != nil {
|
||||
return events.Error(err)
|
||||
|
@ -52,15 +51,13 @@ func (c *TableReadController) ListTables() tea.Cmd {
|
|||
|
||||
return PromptForTableMsg{
|
||||
Tables: tables,
|
||||
OnSelected: func(tableName string) tea.Cmd {
|
||||
OnSelected: func(tableName string) tea.Msg {
|
||||
return c.ScanTable(tableName)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) ScanTable(name string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) ScanTable(name string) tea.Msg {
|
||||
ctx := context.Background()
|
||||
|
||||
tableInfo, err := c.tableService.Describe(ctx, name)
|
||||
|
@ -75,20 +72,15 @@ func (c *TableReadController) ScanTable(name string) tea.Cmd {
|
|||
resultSet = c.tableService.Filter(resultSet, c.state.Filter())
|
||||
|
||||
return c.setResultSetAndFilter(resultSet, c.state.Filter(), true)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) PromptForQuery() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) PromptForQuery() tea.Msg {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "query: ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
return c.runQuery(c.state.ResultSet().TableInfo, value, "", true)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) runQuery(tableInfo *models.TableInfo, query, newFilter string, pushSnapshot bool) tea.Msg {
|
||||
|
@ -107,7 +99,7 @@ func (c *TableReadController) runQuery(tableInfo *models.TableInfo, query, newFi
|
|||
|
||||
expr, err := queryexpr.Parse(query)
|
||||
if err != nil {
|
||||
return events.SetError(err)
|
||||
return events.Error(err)
|
||||
}
|
||||
|
||||
return c.doIfNoneDirty(func() tea.Msg {
|
||||
|
@ -135,27 +127,24 @@ func (c *TableReadController) doIfNoneDirty(cmd tea.Cmd) tea.Msg {
|
|||
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "reset modified items? ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
if value != "y" {
|
||||
return events.SetStatus("operation aborted")
|
||||
}
|
||||
|
||||
return cmd
|
||||
return cmd()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) Rescan() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) Rescan() tea.Msg {
|
||||
return c.doIfNoneDirty(func() tea.Msg {
|
||||
resultSet := c.state.ResultSet()
|
||||
return c.doScan(context.Background(), resultSet, resultSet.Query, true)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) ExportCSV(filename string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) ExportCSV(filename string) tea.Msg {
|
||||
resultSet := c.state.ResultSet()
|
||||
if resultSet == nil {
|
||||
return events.Error(errors.New("no result set"))
|
||||
|
@ -186,7 +175,6 @@ func (c *TableReadController) ExportCSV(filename string) tea.Cmd {
|
|||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) doScan(ctx context.Context, resultSet *models.ResultSet, query models.Queryable, pushBackstack bool) tea.Msg {
|
||||
|
@ -211,35 +199,28 @@ func (c *TableReadController) setResultSetAndFilter(resultSet *models.ResultSet,
|
|||
return c.state.buildNewResultSetMessage("")
|
||||
}
|
||||
|
||||
func (c *TableReadController) Unmark() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) Unmark() tea.Msg {
|
||||
c.state.withResultSet(func(resultSet *models.ResultSet) {
|
||||
for i := range resultSet.Items() {
|
||||
resultSet.SetMark(i, false)
|
||||
}
|
||||
})
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) Filter() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) Filter() tea.Msg {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "filter: ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
resultSet := c.state.ResultSet()
|
||||
newResultSet := c.tableService.Filter(resultSet, value)
|
||||
|
||||
return c.setResultSetAndFilter(newResultSet, value, true)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TableReadController) ViewBack() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *TableReadController) ViewBack() tea.Msg {
|
||||
viewSnapshot, err := c.workspaceService.PopSnapshot()
|
||||
if err != nil {
|
||||
return events.Error(err)
|
||||
|
@ -272,5 +253,4 @@ func (c *TableReadController) ViewBack() tea.Cmd {
|
|||
log.Printf("backstack: running query: table = '%v', query = '%v', filter = '%v'",
|
||||
tableInfo.Name, viewSnapshot.Query, viewSnapshot.Filter)
|
||||
return c.runQuery(tableInfo, viewSnapshot.Query, viewSnapshot.Filter, false)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,8 +29,7 @@ func TestTableReadController_InitTable(t *testing.T) {
|
|||
t.Run("should prompt for table if no table name provided", func(t *testing.T) {
|
||||
readController := controllers.NewTableReadController(controllers.NewState(), service, workspaceService, "")
|
||||
|
||||
cmd := readController.Init()
|
||||
event := cmd()
|
||||
event := readController.Init()
|
||||
|
||||
assert.IsType(t, controllers.PromptForTableMsg{}, event)
|
||||
})
|
||||
|
@ -38,8 +37,7 @@ func TestTableReadController_InitTable(t *testing.T) {
|
|||
t.Run("should scan table if table name provided", func(t *testing.T) {
|
||||
readController := controllers.NewTableReadController(controllers.NewState(), service, workspaceService, "")
|
||||
|
||||
cmd := readController.Init()
|
||||
event := cmd()
|
||||
event := readController.Init()
|
||||
|
||||
assert.IsType(t, controllers.PromptForTableMsg{}, event)
|
||||
})
|
||||
|
@ -56,13 +54,11 @@ func TestTableReadController_ListTables(t *testing.T) {
|
|||
readController := controllers.NewTableReadController(controllers.NewState(), service, workspaceService, "")
|
||||
|
||||
t.Run("returns a list of tables", func(t *testing.T) {
|
||||
cmd := readController.ListTables()
|
||||
event := cmd().(controllers.PromptForTableMsg)
|
||||
event := readController.ListTables().(controllers.PromptForTableMsg)
|
||||
|
||||
assert.Equal(t, []string{"alpha-table", "bravo-table"}, event.Tables)
|
||||
|
||||
selectedCmd := event.OnSelected("alpha-table")
|
||||
selectedEvent := selectedCmd()
|
||||
selectedEvent := event.OnSelected("alpha-table")
|
||||
|
||||
resultSet := selectedEvent.(controllers.NewResultSet)
|
||||
assert.Equal(t, "alpha-table", resultSet.ResultSet.TableInfo.Name)
|
||||
|
@ -208,9 +204,7 @@ func testWorkspace(t *testing.T) *workspaces.Workspace {
|
|||
return ws
|
||||
}
|
||||
|
||||
func invokeCommand(t *testing.T, cmd tea.Cmd) tea.Msg {
|
||||
msg := cmd()
|
||||
|
||||
func invokeCommand(t *testing.T, msg tea.Msg) tea.Msg {
|
||||
err, isErr := msg.(events.ErrorMsg)
|
||||
if isErr {
|
||||
assert.Fail(t, fmt.Sprintf("expected no error but got one: %v", err))
|
||||
|
@ -218,9 +212,7 @@ func invokeCommand(t *testing.T, cmd tea.Cmd) tea.Msg {
|
|||
return msg
|
||||
}
|
||||
|
||||
func invokeCommandWithPrompt(t *testing.T, cmd tea.Cmd, promptValue string) {
|
||||
msg := cmd()
|
||||
|
||||
func invokeCommandWithPrompt(t *testing.T, msg tea.Msg, promptValue string) {
|
||||
pi, isPi := msg.(events.PromptForInputMsg)
|
||||
if !isPi {
|
||||
assert.Fail(t, fmt.Sprintf("expected prompt for input but didn't get one"))
|
||||
|
@ -229,22 +221,18 @@ func invokeCommandWithPrompt(t *testing.T, cmd tea.Cmd, promptValue string) {
|
|||
invokeCommand(t, pi.OnDone(promptValue))
|
||||
}
|
||||
|
||||
func invokeCommandWithPrompts(t *testing.T, cmd tea.Cmd, promptValues ...string) {
|
||||
msg := cmd()
|
||||
|
||||
func invokeCommandWithPrompts(t *testing.T, msg tea.Msg, promptValues ...string) {
|
||||
for _, promptValue := range promptValues {
|
||||
pi, isPi := msg.(events.PromptForInputMsg)
|
||||
if !isPi {
|
||||
assert.Fail(t, fmt.Sprintf("expected prompt for input but didn't get one"))
|
||||
assert.Fail(t, fmt.Sprintf("expected prompt for input but didn't get one: %T", msg))
|
||||
}
|
||||
|
||||
msg = invokeCommand(t, pi.OnDone(promptValue))
|
||||
}
|
||||
}
|
||||
|
||||
func invokeCommandWithPromptsExpectingError(t *testing.T, cmd tea.Cmd, promptValues ...string) {
|
||||
msg := cmd()
|
||||
|
||||
func invokeCommandWithPromptsExpectingError(t *testing.T, msg tea.Msg, promptValues ...string) {
|
||||
for _, promptValue := range promptValues {
|
||||
pi, isPi := msg.(events.PromptForInputMsg)
|
||||
if !isPi {
|
||||
|
@ -258,9 +246,7 @@ func invokeCommandWithPromptsExpectingError(t *testing.T, cmd tea.Cmd, promptVal
|
|||
assert.True(t, isErr)
|
||||
}
|
||||
|
||||
func invokeCommandExpectingError(t *testing.T, cmd tea.Cmd) {
|
||||
msg := cmd()
|
||||
|
||||
func invokeCommandExpectingError(t *testing.T, msg tea.Msg) {
|
||||
_, isErr := msg.(events.ErrorMsg)
|
||||
assert.True(t, isErr)
|
||||
}
|
||||
|
|
|
@ -27,18 +27,15 @@ func NewTableWriteController(state *State, tableService *tables.Service, tableRe
|
|||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) ToggleMark(idx int) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) ToggleMark(idx int) tea.Msg {
|
||||
twc.state.withResultSet(func(resultSet *models.ResultSet) {
|
||||
resultSet.SetMark(idx, !resultSet.Marked(idx))
|
||||
})
|
||||
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) NewItem() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) NewItem() tea.Msg {
|
||||
// Work out which keys we need to prompt for
|
||||
rs := twc.state.ResultSet()
|
||||
|
||||
|
@ -67,10 +64,9 @@ func (twc *TableWriteController) NewItem() tea.Cmd {
|
|||
}
|
||||
|
||||
return keyPrompts.next()
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) SetAttributeValue(idx int, itemType models.ItemType, key string) tea.Cmd {
|
||||
func (twc *TableWriteController) SetAttributeValue(idx int, itemType models.ItemType, key string) tea.Msg {
|
||||
apPath := newAttrPath(key)
|
||||
|
||||
var attrValue types.AttributeValue
|
||||
|
@ -78,7 +74,7 @@ func (twc *TableWriteController) SetAttributeValue(idx int, itemType models.Item
|
|||
attrValue, err = apPath.follow(set.Items()[idx])
|
||||
return err
|
||||
}); err != nil {
|
||||
return events.SetError(err)
|
||||
return events.Error(err)
|
||||
}
|
||||
|
||||
switch itemType {
|
||||
|
@ -91,7 +87,7 @@ func (twc *TableWriteController) SetAttributeValue(idx int, itemType models.Item
|
|||
case *types.AttributeValueMemberBOOL:
|
||||
return twc.setBoolValue(idx, apPath)
|
||||
default:
|
||||
return events.SetError(errors.New("attribute type for key must be set"))
|
||||
return events.Error(errors.New("attribute type for key must be set"))
|
||||
}
|
||||
case models.StringItemType:
|
||||
return twc.setStringValue(idx, apPath)
|
||||
|
@ -102,16 +98,14 @@ func (twc *TableWriteController) SetAttributeValue(idx int, itemType models.Item
|
|||
case models.NullItemType:
|
||||
return twc.setNullValue(idx, apPath)
|
||||
default:
|
||||
return events.SetError(errors.New("unsupported attribute type"))
|
||||
return events.Error(errors.New("unsupported attribute type"))
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) setStringValue(idx int, attr attrPath) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) setStringValue(idx int, attr attrPath) tea.Msg {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "string value: ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
if err := twc.state.withResultSetReturningError(func(set *models.ResultSet) error {
|
||||
if err := twc.applyToItems(set, idx, func(idx int, item models.Item) error {
|
||||
if err := attr.setAt(item, &types.AttributeValueMemberS{Value: value}); err != nil {
|
||||
|
@ -128,10 +122,8 @@ func (twc *TableWriteController) setStringValue(idx int, attr attrPath) tea.Cmd
|
|||
return events.Error(err)
|
||||
}
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) applyToItems(rs *models.ResultSet, selectedIndex int, applyFn func(idx int, item models.Item) error) error {
|
||||
|
@ -147,12 +139,10 @@ func (twc *TableWriteController) applyToItems(rs *models.ResultSet, selectedInde
|
|||
return applyFn(selectedIndex, rs.Items()[selectedIndex])
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) setNumberValue(idx int, attr attrPath) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) setNumberValue(idx int, attr attrPath) tea.Msg {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "number value: ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
if err := twc.state.withResultSetReturningError(func(set *models.ResultSet) error {
|
||||
if err := twc.applyToItems(set, idx, func(idx int, item models.Item) error {
|
||||
if err := attr.setAt(item, &types.AttributeValueMemberN{Value: value}); err != nil {
|
||||
|
@ -169,18 +159,14 @@ func (twc *TableWriteController) setNumberValue(idx int, attr attrPath) tea.Cmd
|
|||
return events.Error(err)
|
||||
}
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) setBoolValue(idx int, attr attrPath) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) setBoolValue(idx int, attr attrPath) tea.Msg {
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "bool value: ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
b, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return events.Error(err)
|
||||
|
@ -202,14 +188,11 @@ func (twc *TableWriteController) setBoolValue(idx int, attr attrPath) tea.Cmd {
|
|||
return events.Error(err)
|
||||
}
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) setNullValue(idx int, attr attrPath) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) setNullValue(idx int, attr attrPath) tea.Msg {
|
||||
if err := twc.state.withResultSetReturningError(func(set *models.ResultSet) error {
|
||||
if err := twc.applyToItems(set, idx, func(idx int, item models.Item) error {
|
||||
if err := attr.setAt(item, &types.AttributeValueMemberNULL{Value: true}); err != nil {
|
||||
|
@ -226,11 +209,9 @@ func (twc *TableWriteController) setNullValue(idx int, attr attrPath) tea.Cmd {
|
|||
return events.Error(err)
|
||||
}
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) DeleteAttribute(idx int, key string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) DeleteAttribute(idx int, key string) tea.Msg {
|
||||
// Verify that the expression is valid
|
||||
apPath := newAttrPath(key)
|
||||
|
||||
|
@ -255,11 +236,9 @@ func (twc *TableWriteController) DeleteAttribute(idx int, key string) tea.Cmd {
|
|||
}
|
||||
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) PutItem(idx int) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) PutItem(idx int) tea.Msg {
|
||||
resultSet := twc.state.ResultSet()
|
||||
if !resultSet.IsDirty(idx) {
|
||||
return events.Error(errors.New("item is not dirty"))
|
||||
|
@ -267,8 +246,7 @@ func (twc *TableWriteController) PutItem(idx int) tea.Cmd {
|
|||
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "put item? ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
if value != "y" {
|
||||
return nil
|
||||
}
|
||||
|
@ -277,14 +255,11 @@ func (twc *TableWriteController) PutItem(idx int) tea.Cmd {
|
|||
return events.Error(err)
|
||||
}
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) PutItems() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) PutItems() tea.Msg {
|
||||
var (
|
||||
markedItemCount int
|
||||
)
|
||||
|
@ -324,12 +299,11 @@ func (twc *TableWriteController) PutItems() tea.Cmd {
|
|||
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: promptMessage,
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
if value != "y" {
|
||||
return events.SetStatus("operation aborted")
|
||||
}
|
||||
|
||||
return func() tea.Msg {
|
||||
if err := twc.state.withResultSetReturningError(func(rs *models.ResultSet) error {
|
||||
err := twc.tableService.PutSelectedItems(context.Background(), rs, itemsToPut)
|
||||
if err != nil {
|
||||
|
@ -343,14 +317,11 @@ func (twc *TableWriteController) PutItems() tea.Cmd {
|
|||
return ResultSetUpdated{
|
||||
statusMessage: applyToN("", len(itemsToPut), "item", "item", " put to table"),
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) TouchItem(idx int) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) TouchItem(idx int) tea.Msg {
|
||||
resultSet := twc.state.ResultSet()
|
||||
if resultSet.IsDirty(idx) {
|
||||
return events.Error(errors.New("cannot touch dirty items"))
|
||||
|
@ -358,8 +329,7 @@ func (twc *TableWriteController) TouchItem(idx int) tea.Cmd {
|
|||
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "touch item? ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
if value != "y" {
|
||||
return nil
|
||||
}
|
||||
|
@ -368,14 +338,11 @@ func (twc *TableWriteController) TouchItem(idx int) tea.Cmd {
|
|||
return events.Error(err)
|
||||
}
|
||||
return ResultSetUpdated{}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) NoisyTouchItem(idx int) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) NoisyTouchItem(idx int) tea.Msg {
|
||||
resultSet := twc.state.ResultSet()
|
||||
if resultSet.IsDirty(idx) {
|
||||
return events.Error(errors.New("cannot noisy touch dirty items"))
|
||||
|
@ -383,8 +350,7 @@ func (twc *TableWriteController) NoisyTouchItem(idx int) tea.Cmd {
|
|||
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: "noisy touch item? ",
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
ctx := context.Background()
|
||||
|
||||
if value != "y" {
|
||||
|
@ -401,14 +367,11 @@ func (twc *TableWriteController) NoisyTouchItem(idx int) tea.Cmd {
|
|||
}
|
||||
|
||||
return twc.tableReadControllers.doScan(ctx, resultSet, resultSet.Query, false)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (twc *TableWriteController) DeleteMarked() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (twc *TableWriteController) DeleteMarked() tea.Msg {
|
||||
resultSet := twc.state.ResultSet()
|
||||
markedItems := resultSet.MarkedItems()
|
||||
|
||||
|
@ -418,12 +381,11 @@ func (twc *TableWriteController) DeleteMarked() tea.Cmd {
|
|||
|
||||
return events.PromptForInputMsg{
|
||||
Prompt: applyToN("delete ", len(markedItems), "item", "items", "? "),
|
||||
OnDone: func(value string) tea.Cmd {
|
||||
OnDone: func(value string) tea.Msg {
|
||||
if value != "y" {
|
||||
return events.SetStatus("operation aborted")
|
||||
}
|
||||
|
||||
return func() tea.Msg {
|
||||
ctx := context.Background()
|
||||
if err := twc.tableService.Delete(ctx, resultSet.TableInfo, sliceutils.Map(markedItems, func(index models.ItemIndex) models.Item {
|
||||
return index.Item
|
||||
|
@ -432,10 +394,8 @@ func (twc *TableWriteController) DeleteMarked() tea.Cmd {
|
|||
}
|
||||
|
||||
return twc.tableReadControllers.doScan(ctx, resultSet, resultSet.Query, false)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func applyToN(prefix string, n int, singular, plural, suffix string) string {
|
||||
|
|
|
@ -46,27 +46,27 @@ func NewModel(rc *controllers.TableReadController, wc *controllers.TableWriteCon
|
|||
cc.AddCommands(&commandctrl.CommandContext{
|
||||
Commands: map[string]commandctrl.Command{
|
||||
"quit": commandctrl.NoArgCommand(tea.Quit),
|
||||
"table": func(args []string) tea.Cmd {
|
||||
"table": func(args []string) tea.Msg {
|
||||
if len(args) == 0 {
|
||||
return rc.ListTables()
|
||||
} else {
|
||||
return rc.ScanTable(args[0])
|
||||
}
|
||||
},
|
||||
"export": func(args []string) tea.Cmd {
|
||||
"export": func(args []string) tea.Msg {
|
||||
if len(args) == 0 {
|
||||
return events.SetError(errors.New("expected filename"))
|
||||
return events.Error(errors.New("expected filename"))
|
||||
}
|
||||
return rc.ExportCSV(args[0])
|
||||
},
|
||||
"unmark": commandctrl.NoArgCommand(rc.Unmark()),
|
||||
"delete": commandctrl.NoArgCommand(wc.DeleteMarked()),
|
||||
"unmark": commandctrl.NoArgCommand(rc.Unmark),
|
||||
"delete": commandctrl.NoArgCommand(wc.DeleteMarked),
|
||||
|
||||
// TEMP
|
||||
"new-item": commandctrl.NoArgCommand(wc.NewItem()),
|
||||
"set-attr": func(args []string) tea.Cmd {
|
||||
"new-item": commandctrl.NoArgCommand(wc.NewItem),
|
||||
"set-attr": func(args []string) tea.Msg {
|
||||
if len(args) == 0 {
|
||||
return events.SetError(errors.New("expected field"))
|
||||
return events.Error(errors.New("expected field"))
|
||||
}
|
||||
|
||||
var itemType = models.UnsetItemType
|
||||
|
@ -81,27 +81,27 @@ func NewModel(rc *controllers.TableReadController, wc *controllers.TableWriteCon
|
|||
case "-NULL":
|
||||
itemType = models.NullItemType
|
||||
default:
|
||||
return events.SetError(errors.New("unrecognised item type"))
|
||||
return events.Error(errors.New("unrecognised item type"))
|
||||
}
|
||||
args = args[1:]
|
||||
}
|
||||
|
||||
return wc.SetAttributeValue(dtv.SelectedItemIndex(), itemType, args[0])
|
||||
},
|
||||
"del-attr": func(args []string) tea.Cmd {
|
||||
"del-attr": func(args []string) tea.Msg {
|
||||
if len(args) == 0 {
|
||||
return events.SetError(errors.New("expected field"))
|
||||
return events.Error(errors.New("expected field"))
|
||||
}
|
||||
return wc.DeleteAttribute(dtv.SelectedItemIndex(), args[0])
|
||||
},
|
||||
|
||||
"put": func(args []string) tea.Cmd {
|
||||
"put": func(args []string) tea.Msg {
|
||||
return wc.PutItems()
|
||||
},
|
||||
"touch": func(args []string) tea.Cmd {
|
||||
"touch": func(args []string) tea.Msg {
|
||||
return wc.TouchItem(dtv.SelectedItemIndex())
|
||||
},
|
||||
"noisy-touch": func(args []string) tea.Cmd {
|
||||
"noisy-touch": func(args []string) tea.Msg {
|
||||
return wc.NoisyTouchItem(dtv.SelectedItemIndex())
|
||||
},
|
||||
|
||||
|
@ -129,7 +129,7 @@ func NewModel(rc *controllers.TableReadController, wc *controllers.TableWriteCon
|
|||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd {
|
||||
return m.tableReadController.Init()
|
||||
return m.tableReadController.Init
|
||||
}
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
@ -141,21 +141,21 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
switch msg.String() {
|
||||
case "m":
|
||||
if idx := m.tableView.SelectedItemIndex(); idx >= 0 {
|
||||
return m, m.tableWriteController.ToggleMark(idx)
|
||||
return m, func() tea.Msg { return m.tableWriteController.ToggleMark(idx) }
|
||||
}
|
||||
case "R":
|
||||
return m, m.tableReadController.Rescan()
|
||||
return m, m.tableReadController.Rescan
|
||||
case "?":
|
||||
return m, m.tableReadController.PromptForQuery()
|
||||
return m, m.tableReadController.PromptForQuery
|
||||
case "/":
|
||||
return m, m.tableReadController.Filter()
|
||||
return m, m.tableReadController.Filter
|
||||
case "backspace":
|
||||
return m, m.tableReadController.ViewBack()
|
||||
return m, m.tableReadController.ViewBack
|
||||
//case "e":
|
||||
// m.itemEdit.Visible()
|
||||
// return m, nil
|
||||
case ":":
|
||||
return m, m.commandController.Prompt()
|
||||
return m, m.commandController.Prompt
|
||||
case "ctrl+c", "esc":
|
||||
return m, tea.Quit
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ func (s *StatusAndPrompt) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
pendingInput := s.pendingInput
|
||||
s.pendingInput = nil
|
||||
|
||||
return s, pendingInput.OnDone(s.textInput.Value())
|
||||
return s, func() tea.Msg { return pendingInput.OnDone(s.textInput.Value()) }
|
||||
default:
|
||||
if msg.Type == tea.KeyRunes {
|
||||
msg.Runes = sliceutils.Filter(msg.Runes, func(r rune) bool { return r != '\x0d' && r != '\x0a' })
|
||||
|
|
|
@ -55,7 +55,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
var sel controllers.PromptForTableMsg
|
||||
sel, m.pendingSelection = *m.pendingSelection, nil
|
||||
|
||||
return m, sel.OnSelected(m.listController.list.SelectedItem().(tableItem).name)
|
||||
return m, func() tea.Msg { return sel.OnSelected(m.listController.list.SelectedItem().(tableItem).name) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
switch msg.String() {
|
||||
// TEMP
|
||||
case ":":
|
||||
return m, m.cmdController.Prompt()
|
||||
return m, func() tea.Msg { return m.cmdController.Prompt() }
|
||||
case "w":
|
||||
return m, m.controller.ViewLogLineFullScreen(m.logLines.SelectedLogLine())
|
||||
// END TEMP
|
||||
|
|
|
@ -39,8 +39,7 @@ func (c *SSMController) Fetch() tea.Cmd {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *SSMController) ChangePrefix(newPrefix string) tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *SSMController) ChangePrefix(newPrefix string) tea.Msg {
|
||||
res, err := c.service.List(context.Background(), newPrefix)
|
||||
if err != nil {
|
||||
return events.Error(err)
|
||||
|
@ -54,11 +53,10 @@ func (c *SSMController) ChangePrefix(newPrefix string) tea.Cmd {
|
|||
Prefix: c.prefix,
|
||||
Parameters: res,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *SSMController) Clone(param models.SSMParameter) tea.Cmd {
|
||||
return events.PromptForInput("New key: ", func(value string) tea.Cmd {
|
||||
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 {
|
||||
|
@ -78,9 +76,8 @@ func (c *SSMController) Clone(param models.SSMParameter) tea.Cmd {
|
|||
})
|
||||
}
|
||||
|
||||
func (c *SSMController) DeleteParameter(param models.SSMParameter) tea.Cmd {
|
||||
return events.Confirm("delete parameter? ", func() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
func (c *SSMController) DeleteParameter(param models.SSMParameter) tea.Msg {
|
||||
return events.Confirm("delete parameter? ", func() tea.Msg {
|
||||
ctx := context.Background()
|
||||
if err := c.service.Delete(ctx, param); err != nil {
|
||||
return events.Error(err)
|
||||
|
@ -95,6 +92,5 @@ func (c *SSMController) DeleteParameter(param models.SSMParameter) tea.Cmd {
|
|||
Prefix: c.prefix,
|
||||
Parameters: res,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -32,17 +32,17 @@ func NewModel(controller *controllers.SSMController, cmdController *commandctrl.
|
|||
|
||||
cmdController.AddCommands(&commandctrl.CommandContext{
|
||||
Commands: map[string]commandctrl.Command{
|
||||
"clone": func(args []string) tea.Cmd {
|
||||
"clone": func(args []string) tea.Msg {
|
||||
if currentParam := ssmList.CurrentParameter(); currentParam != nil {
|
||||
return controller.Clone(*currentParam)
|
||||
}
|
||||
return events.SetError(errors.New("no parameter selected"))
|
||||
return events.Error(errors.New("no parameter selected"))
|
||||
},
|
||||
"delete": func(args []string) tea.Cmd {
|
||||
"delete": func(args []string) tea.Msg {
|
||||
if currentParam := ssmList.CurrentParameter(); currentParam != nil {
|
||||
return controller.DeleteParameter(*currentParam)
|
||||
}
|
||||
return events.SetError(errors.New("no parameter selected"))
|
||||
return events.Error(errors.New("no parameter selected"))
|
||||
},
|
||||
},
|
||||
})
|
||||
|
@ -75,7 +75,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||
switch msg.String() {
|
||||
// TEMP
|
||||
case ":":
|
||||
return m, m.cmdController.Prompt()
|
||||
return m, func() tea.Msg { return m.cmdController.Prompt() }
|
||||
// END TEMP
|
||||
|
||||
case "ctrl+c", "q":
|
||||
|
|
Loading…
Reference in a new issue