Skip to content

Commit

Permalink
feat: skip flags users already input
Browse files Browse the repository at this point in the history
Signed-off-by: zychen5186 <brianchen5197@gmail.com>
  • Loading branch information
zychen5186 committed May 15, 2024
1 parent db07597 commit fe80a86
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
7 changes: 6 additions & 1 deletion flytectl/pkg/bubbletea/bubbletea_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (m listModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
newArgs = append(newArgs, string(item))
err := genListModel(&m, string(item))
if err != nil || m.quitting {
listErrMsg = err
return m, tea.Quit
}
return m, nil
Expand Down Expand Up @@ -149,7 +150,11 @@ func ShowCmdList(_rootCmd *cobra.Command) {
}
}

if listErrMsg != nil {
fmt.Println(listErrMsg)
}

// fmt.Println(append(newArgs, existingFlags...))
// exist flags need to be append at last, so if any user input is wrong, it can be caught in the main logic
// Originally existed flags need to be append at last, so if any user input is wrong, it can be caught in the main logic
rootCmd.SetArgs(append(newArgs, existingFlags...))
}
35 changes: 19 additions & 16 deletions flytectl/pkg/bubbletea/bubbletea_list_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ var (
rootCmd *cobra.Command
newArgs []string
isCommand = true
unhandleflags []string
unhandleFlags []string
existingFlags []string
listErrMsg error = nil
)
var (
domainName = [3]string{"development", "staging", "production"}
Expand Down Expand Up @@ -132,28 +133,31 @@ func genProjectListModel(m *listModel) error {

// Generate list.Model of options for different flags
func genFlagListModel(m *listModel) error {
f := unhandleflags[0]
newArgs = append(newArgs, f)
unhandleflags = unhandleflags[1:]
// TODO check if some flags are already input as arguments by user
var err error
var i int
var flag string
for i, flag = range unhandleFlags {
if !rootCmd.Flags().ShorthandLookup(strings.TrimPrefix(flag, "-")).Changed {
break
}
}
newArgs = append(newArgs, unhandleFlags[i])
unhandleFlags = unhandleFlags[i+1:]

switch f {
switch flag {
case "-p":
err = genProjectListModel(m)
err := genProjectListModel(m)
if err != nil {
return err
}
case "-d":
genDomainListModel(m)
}

return err
return nil
}

// Generate list.Model of subcommands from a given command
func genCmdListModel(m *listModel, c *cobra.Command) {
// if len(nameToCommand[c].Cmd.Commands()) == 0 {
// return m
// }

items := generateSubCmdItems(c)
l := genList(items, "")
m.list = l
Expand All @@ -162,20 +166,19 @@ func genCmdListModel(m *listModel, c *cobra.Command) {

// Generate list.Model after user chose one of the item
func genListModel(m *listModel, item string) error {

// Still in the stage of handling subcommands
if isCommand {
var ok bool
// check if we reach a runnable command
if unhandleflags, ok = commandFlagMap[sliceToString(newArgs)]; !ok {
if unhandleFlags, ok = commandFlagMap[sliceToString(newArgs)]; !ok {
genCmdListModel(m, nameToCommand[item].Cmd)
return nil
}
isCommand = false
}

// Handled all flags, quit.
if len(unhandleflags) == 0 {
if len(unhandleFlags) == 0 {
m.quitting = true
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions flytectl/pkg/bubbletea/bubbletea_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func Paginator(_listHeader []printer.Column, _callback DataCallback, _filter fil
log.Fatal(err)
}

if errMsg != nil {
return errMsg
if pageErrMsg != nil {
return pageErrMsg
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions flytectl/pkg/bubbletea/bubbletea_pagination_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (
// Avoid fetching back and forward at the same time
mutex sync.Mutex
// Used to catch error happened while running paginator
errMsg error = nil
pageErrMsg error = nil
)

func (p printTableProto) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -135,7 +135,7 @@ func fetchDataCmd(batchIndex int, fetchDirection direction) tea.Cmd {
return func() tea.Msg {
newItems, err := getMessageList(batchIndex)
if err != nil {
errMsg = err
pageErrMsg = err
return err
}
msg := newDataMsg{
Expand Down

0 comments on commit fe80a86

Please sign in to comment.