Skip to content

Commit

Permalink
fix some issues relate to dynamic commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Nov 7, 2023
1 parent 7db965a commit 73f1765
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
22 changes: 16 additions & 6 deletions cmd/custom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -24,6 +25,13 @@ func NewCmdCustom(alias string, extension extensions.Extension) (*cobra.Command,
Args: cobra.NoArgs,
GroupID: CommandGroupExtension,
RunE: func(cmd *cobra.Command, args []string) error {
var jsonOutput bool
if cmd.Flags().Changed("json") {
jsonOutput, _ = cmd.Flags().GetBool("json")
} else {
jsonOutput = !isatty.IsTerminal(os.Stdout.Fd())
}

cfg, err := config.Load()
if err != nil {
return err
Expand All @@ -44,10 +52,10 @@ func NewCmdCustom(alias string, extension extensions.Extension) (*cobra.Command,
return err
}

return runExtension(extension, input, cfg.Env)
return runExtension(extension, input, cfg.Env, jsonOutput)
}

if !isatty.IsTerminal(os.Stdout.Fd()) {
if jsonOutput {
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
return encoder.Encode(extension)
Expand All @@ -74,6 +82,8 @@ func NewCmdCustom(alias string, extension extensions.Extension) (*cobra.Command,
return tui.Draw(page)
},
}

rootCmd.Flags().Bool("json", false, "output as json")
rootCmd.CompletionOptions.DisableDefaultCmd = true
rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

Expand Down Expand Up @@ -129,10 +139,10 @@ func NewCmdCustom(alias string, extension extensions.Extension) (*cobra.Command,
return err
}

input.Query = string(stdin)
input.Query = string(bytes.Trim(stdin, "\n"))
}

return runExtension(extension, input, cfg.Env)
return runExtension(extension, input, cfg.Env, !isatty.IsTerminal(os.Stdout.Fd()))
},
}

Expand All @@ -153,7 +163,7 @@ func NewCmdCustom(alias string, extension extensions.Extension) (*cobra.Command,
return rootCmd, nil
}

func runExtension(extension extensions.Extension, input types.CommandInput, environ map[string]string) error {
func runExtension(extension extensions.Extension, input types.CommandInput, environ map[string]string, jsonOutput bool) error {
if err := extension.CheckRequirements(); err != nil {
return tui.Draw(tui.NewErrorPage(fmt.Errorf("missing requirements: %w", err)))
}
Expand All @@ -164,7 +174,7 @@ func runExtension(extension extensions.Extension, input types.CommandInput, envi
}

missing := tui.FindMissingParams(command.Params, input.Params)
if !isatty.IsTerminal(os.Stdout.Fd()) {
if jsonOutput {
if len(missing) > 0 {
names := make([]string, len(missing))
for i, param := range missing {
Expand Down
4 changes: 3 additions & 1 deletion internal/tui/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ func (c *List) Update(msg tea.Msg) (Page, tea.Cmd) {
if c.input.Value() != "" {
c.input.SetValue("")
c.FilterItems("")
return c, nil
return c, func() tea.Msg {
return QueryChangeMsg("")
}
}

return c, PopPageCmd
Expand Down
13 changes: 12 additions & 1 deletion internal/tui/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os/exec"

"github.com/acarl005/stripansi"
"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
"github.com/muesli/termenv"
Expand Down Expand Up @@ -34,7 +35,12 @@ func NewRunner(extension extensions.Extension, input types.CommandInput, environ
if ok {
switch command.Mode {
case types.CommandModeList:
embed = NewList()
list := NewList()
if input.Query != "" {
list.SetQuery(input.Query)
}

embed = list
case types.CommandModeDetail:
embed = NewDetail("")
default:
Expand Down Expand Up @@ -327,6 +333,11 @@ func (c *Runner) Reload() tea.Cmd {
if errors.Is(ctx.Err(), context.Canceled) {
return nil
}
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return fmt.Errorf("command failed: %s", stripansi.Strip(string(exitErr.Stderr)))
}

return err
}

Expand Down

0 comments on commit 73f1765

Please sign in to comment.