Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/pgxcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"os"

"github.com/balajz/pgxcli/internal/app/renderer"
"github.com/balajz/pgxcli/internal/cli"
"github.com/balajz/pgxcli/internal/cliio"
)
Expand All @@ -27,7 +26,7 @@ func main() {
)

if err := rootCmd.ExecuteContext(ctx); err != nil {
_ = renderer.Error(err, os.Stderr)
printer.PrintError(err)
os.Exit(1)
}
}
9 changes: 6 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package app

import (
"context"
"fmt"
"log/slog"
"os"
"time"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/balajz/pgxcli/internal/cliio"
"github.com/balajz/pgxcli/internal/config"
"github.com/balajz/pgxcli/internal/database"
"github.com/balajz/pgxcli/internal/perrors"
compDB "github.com/balajz/pgxls/pkg/database"
)

Expand Down Expand Up @@ -94,14 +94,17 @@ func (p *pgxCLI) Start(ctx context.Context) error {
p.getCompletions(),
)
if err != nil {
return fmt.Errorf("creating UI model: %w", err)
return err
}

p.model = m
p.program = tea.NewProgram(p.model, tea.WithContext(ctx))

if _, err := p.program.Run(); err != nil {
return fmt.Errorf("running UI program: %w", err)
return perrors.Wrap(
err,
perrors.WithMessage("failed to start UI"),
)
}

return nil
Expand Down
8 changes: 6 additions & 2 deletions internal/app/renderer/formatter/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"

"github.com/balajz/pgxcli/internal/config"
"github.com/balajz/pgxcli/internal/perrors"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
Expand Down Expand Up @@ -38,7 +39,7 @@ func (p *TableFormatter) Iter(_, ew io.Writer, row []string) error {
}

if err := p.table.Append(row); err != nil {
return err
return perrors.Wrap(err, perrors.WithMessage("failed to append row to table"))
}

p.rows++
Expand All @@ -61,7 +62,10 @@ func (p *TableFormatter) Caption(w io.Writer, caption string) error {
}

func (p *TableFormatter) Render(_ io.Writer, _ int) error {
return p.table.Render()
if err := p.table.Render(); err != nil {
return perrors.Wrap(err, perrors.WithMessage("failed to render table"))
}
return nil
}

func (p *TableFormatter) Done(_ io.Writer) error {
Expand Down
8 changes: 6 additions & 2 deletions internal/app/renderer/meta_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/balajz/pgxcli/internal/config"
"github.com/balajz/pgxcli/internal/perrors"
"github.com/balajz/pgxcli/pgxspecial"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
Expand All @@ -30,7 +31,7 @@ func Table(data Data, w io.Writer, c *config.Config) error {

t.Header(data.Columns())
if err := t.Bulk(rows); err != nil {
return err
return perrors.Wrap(err, perrors.WithMessage("failed to bulk append rows to table"))
}

if captionText := data.Caption(); captionText != "" {
Expand All @@ -41,7 +42,10 @@ func Table(data Data, w io.Writer, c *config.Config) error {
}
t.Caption(caption)
}
return t.Render()
if err := t.Render(); err != nil {
return perrors.Wrap(err, perrors.WithMessage("failed to render table"))
}
return nil
}

type rowsTableResult interface {
Expand Down
23 changes: 19 additions & 4 deletions internal/app/ui/components/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package components

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/alecthomas/chroma/v2/quick"
"github.com/balajz/bubbline/editline"
"github.com/balajz/bubbline/history"
"github.com/balajz/pgxcli/internal/perrors"
"github.com/muesli/termenv"
)

Expand All @@ -33,7 +33,7 @@ func NewInputModel(prompt, historyFile string, style string, autoCompleter editl
}

if err := applyEditlineConfig(el, historyFile, style); err != nil {
return nil, fmt.Errorf("applying input config: %w", err)
return nil, err
}

el.AutoComplete = autoCompleter
Expand Down Expand Up @@ -78,7 +78,16 @@ func (m *InputModel) SaveHistory() error {
if m.HistoryFile == "" {
return nil
}
return history.SaveHistory(m.Model.GetHistory(), m.HistoryFile)
if err := history.SaveHistory(m.Model.GetHistory(), m.HistoryFile); err != nil {
return perrors.Wrap(
err,
perrors.WithMessage("failed to save save history"),
perrors.WithDetails(
"path", m.HistoryFile,
),
)
}
return nil
}

func (m *InputModel) SetPrompt(prompt string) {
Expand Down Expand Up @@ -143,7 +152,13 @@ func applyEditlineConfig(el *editline.Model, historyFile string, style string) e

entries, err := history.LoadHistory(historyFile)
if err != nil {
return fmt.Errorf("loading history: %w", err)
return perrors.Wrap(
err,
perrors.WithMessage("failed to load history"),
perrors.WithDetails(
"path", historyFile,
),
)
}

el.SetHistory(entries)
Expand Down
17 changes: 7 additions & 10 deletions internal/app/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/alecthomas/chroma/v2/quick"
"github.com/balajz/bubbline/editline"
"github.com/balajz/pgxcli/internal/app/ui/components"
"github.com/balajz/pgxcli/internal/perrors"
"github.com/davecgh/go-spew/spew"
"github.com/muesli/termenv"
)
Expand Down Expand Up @@ -77,7 +78,7 @@ type Model struct {
func New(initialPrefix string, historyFile string, style string, version string, executeFunc execute, cancelFunc cancel, autocompleter editline.AutoCompleteFn) (*Model, error) {
inputModel, err := components.NewInputModel(initialPrefix, historyFile, style, autocompleter)
if err != nil {
return nil, fmt.Errorf("creating input model: %w", err)
return nil, err
}

styles := DefaultStyles()
Expand All @@ -92,7 +93,10 @@ func New(initialPrefix string, historyFile string, style string, version string,
if _, ok := os.LookupEnv("PGXCLI_DEBUG"); ok {
dump, err = os.OpenFile("pgxcli_messages.log", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
if err != nil {
return nil, fmt.Errorf("opening debug log: %w", err)
return nil, perrors.Wrap(
err,
perrors.WithMessage("failed to debug UI"),
)
}
}

Expand Down Expand Up @@ -325,18 +329,11 @@ func (m *Model) View() tea.View {
return tea.NewView(baseView)
}

func (m *Model) saveHistory() error {
return m.input.SaveHistory()
}

func (m *Model) Close() error {
if m.dump != nil {
_ = m.dump.Close()
}
if err := m.saveHistory(); err != nil {
return fmt.Errorf("saving history: %w", err)
}
return nil
return m.input.SaveHistory()
}

// PrintCmd returns a command that prints formatted text.
Expand Down
27 changes: 20 additions & 7 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/balajz/pgxcli/internal/config"
"github.com/balajz/pgxcli/internal/database"
"github.com/balajz/pgxcli/internal/logger"
"github.com/balajz/pgxcli/internal/perrors"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -81,8 +82,8 @@ func NewRootCmd(ctx context.Context, cliCtx *CliContext) *cobra.Command {
},

PersistentPostRunE: func(_ *cobra.Command, _ []string) error {
if cliCtx.Logger != nil {
if err := cliCtx.Logger.Close(); err != nil {
if cliCtx.App != nil {
if err := cliCtx.App.Close(); err != nil {
return err
}
}
Expand All @@ -91,8 +92,8 @@ func NewRootCmd(ctx context.Context, cliCtx *CliContext) *cobra.Command {
return err
}
}
if cliCtx.App != nil {
if err := cliCtx.App.Close(); err != nil {
if cliCtx.Logger != nil {
if err := cliCtx.Logger.Close(); err != nil {
return err
}
}
Expand Down Expand Up @@ -408,23 +409,35 @@ func promptPassword(s string) (string, error) {
if !term.IsTerminal(fd) {
pwd, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
return "", perrors.Wrap(
err,
perrors.WithMessage("failed to read password"),
perrors.WithDetails("terminal", false),
)
}
return strings.TrimRight(pwd, "\r\n"), nil
}

pwd, err := term.ReadPassword(fd)
fmt.Println()
if err != nil {
return "", err
return "", perrors.Wrap(
err,
perrors.WithMessage("failed to read password"),
perrors.WithDetails("terminal", true),
)
}
return string(pwd), nil
}

func mustParsePort(port string) (uint16, error) {
portNum, err := strconv.Atoi(port)
if err != nil {
return 0, err
return 0, perrors.Wrap(
err,
perrors.WithMessage("failed to parse port"),
perrors.WithDetails("port", port),
)
}
return uint16(portNum), nil
}
Expand Down
Loading
Loading