Skip to content

Commit f1937b3

Browse files
authored
feat(cmd): add command to display ledger errors (#28)
1 parent 4ea7558 commit f1937b3

4 files changed

Lines changed: 135 additions & 0 deletions

File tree

cmd/error/error.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmderror
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"text/tabwriter"
7+
8+
"github.com/spf13/cobra"
9+
10+
"beancount.io/beancount-cli/generated"
11+
"beancount.io/beancount-cli/internal/utils"
12+
)
13+
14+
func NewCmdError() *cobra.Command {
15+
return &cobra.Command{
16+
Use: "error <ledger>",
17+
Short: "Show errors for a ledger",
18+
Args: cobra.ExactArgs(1),
19+
RunE: runError,
20+
}
21+
}
22+
23+
func runError(cmd *cobra.Command, args []string) error {
24+
client, err := utils.NewAuthedClient()
25+
if err != nil {
26+
return err
27+
}
28+
29+
ledgerID := utils.LedgerID(args[0])
30+
resp, err := generated.GetLedgerErrors(context.Background(), client, ledgerID)
31+
if err != nil {
32+
return fmt.Errorf("failed to get ledger errors: %w", err)
33+
}
34+
35+
errs := resp.GetLedgerErrors
36+
if len(errs) == 0 {
37+
fmt.Fprintln(cmd.OutOrStdout(), "No errors found.")
38+
return nil
39+
}
40+
41+
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
42+
fmt.Fprintln(w, "FILE\tLINE\tMESSAGE")
43+
for _, e := range errs {
44+
filename := ""
45+
if e.Filename != nil {
46+
filename = *e.Filename
47+
}
48+
lineno := ""
49+
if e.Lineno != nil {
50+
lineno = fmt.Sprintf("%.0f", *e.Lineno)
51+
}
52+
fmt.Fprintf(w, "%s\t%s\t%s\n", filename, lineno, e.Message)
53+
}
54+
return w.Flush()
55+
}

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
cmdcollaborator "beancount.io/beancount-cli/cmd/collaborator"
1616
cmdcommodity "beancount.io/beancount-cli/cmd/commodity"
1717
cmddocument "beancount.io/beancount-cli/cmd/document"
18+
cmderror "beancount.io/beancount-cli/cmd/error"
1819
cmdevent "beancount.io/beancount-cli/cmd/event"
1920
cmdledger "beancount.io/beancount-cli/cmd/ledger"
2021
cmdnote "beancount.io/beancount-cli/cmd/note"
@@ -79,6 +80,7 @@ func init() {
7980
rootCmd.AddCommand(cmdcommodity.NewCmdCommodity())
8081
rootCmd.AddCommand(cmddocument.NewCmdDocument())
8182
rootCmd.AddCommand(cmdevent.NewCmdEvent())
83+
rootCmd.AddCommand(cmderror.NewCmdError())
8284
rootCmd.AddCommand(cmdledger.NewCmdLedger())
8385
rootCmd.AddCommand(cmdnote.NewCmdNote())
8486
rootCmd.AddCommand(cmdprice.NewCmdPrice())

generated/genqlient.go

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphql/operations.graphql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,11 @@ mutation DeleteLedgerCollaborator($ledgerId: String!, $collaborator: String!) {
180180
message
181181
}
182182
}
183+
184+
query GetLedgerErrors($ledgerId: String!) {
185+
getLedgerErrors(ledgerId: $ledgerId) {
186+
filename
187+
lineno
188+
message
189+
}
190+
}

0 commit comments

Comments
 (0)