Skip to content

Commit 5a7915d

Browse files
authored
refactor: invoice list (#883)
* fix: invoice list refactor * fix: remove unnecessary option
1 parent 9098811 commit 5a7915d

3 files changed

Lines changed: 48 additions & 24 deletions

File tree

internal/cmd/invoice_flag.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

internal/cmd/invoice_list.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"fmt"
55

66
"github.com/spf13/cobra"
7+
"github.com/tursodatabase/turso-cli/internal/flags"
78
"github.com/tursodatabase/turso-cli/internal/turso"
89
)
910

1011
func init() {
1112
invoiceCmd.AddCommand(listInvoicesCmd)
12-
AddInvoiceType(listInvoicesCmd)
13+
flags.AddInvoiceType(listInvoicesCmd)
1314
}
1415

1516
var listInvoicesCmd = &cobra.Command{
@@ -24,12 +25,9 @@ var listInvoicesCmd = &cobra.Command{
2425
return err
2526
}
2627

27-
if invoiceType == "" {
28-
invoiceType = "issued"
29-
}
30-
31-
if invoiceType != "all" && invoiceType != "upcoming" && invoiceType != "issued" {
32-
return fmt.Errorf("invalid invoice type: %s", invoiceType)
28+
invoiceType, err := flags.InvoiceType()
29+
if err != nil {
30+
return err
3331
}
3432

3533
invoices, err := client.Invoices.List(invoiceType)
@@ -42,25 +40,25 @@ var listInvoicesCmd = &cobra.Command{
4240
return nil
4341
}
4442

45-
printInvoiceListTable(invoices)
43+
printInvoiceTable(invoices)
4644
fmt.Println()
4745
fmt.Println()
48-
printInvoiceListLinks(invoices)
46+
printInvoiceLinks(invoices)
4947
return nil
5048
},
5149
}
5250

53-
func printInvoiceListTable(invoices []turso.Invoice) {
54-
headers, data := invoiceListTable(invoices)
51+
func printInvoiceTable(invoices []turso.Invoice) {
52+
headers, data := invoiceTable(invoices)
5553
printTable(headers, data)
5654
}
5755

58-
func printInvoiceListLinks(invoices []turso.Invoice) {
59-
headers, data := invoiceListLinks(invoices)
56+
func printInvoiceLinks(invoices []turso.Invoice) {
57+
headers, data := invoiceLinks(invoices)
6058
printTable(headers, data)
6159
}
6260

63-
func invoiceListTable(invoices []turso.Invoice) ([]string, [][]string) {
61+
func invoiceTable(invoices []turso.Invoice) ([]string, [][]string) {
6462
headers := []string{"ID", "Amount Due", "Status", "Due Date", "Paid At", "Payment Failed At"}
6563
data := make([][]string, len(invoices))
6664
for i, invoice := range invoices {
@@ -69,7 +67,7 @@ func invoiceListTable(invoices []turso.Invoice) ([]string, [][]string) {
6967
return headers, data
7068
}
7169

72-
func invoiceListLinks(invoices []turso.Invoice) ([]string, [][]string) {
70+
func invoiceLinks(invoices []turso.Invoice) ([]string, [][]string) {
7371
headers := []string{"ID", "Link"}
7472
data := make([][]string, len(invoices))
7573
for i, invoice := range invoices {

internal/flags/invoice_type.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package flags
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var invoiceType string
10+
11+
func AddInvoiceType(cmd *cobra.Command) {
12+
cmd.Flags().StringVar(&invoiceType, "type", "issued", "type of the invoice. Possible values: 'all', 'upcoming', 'issued'")
13+
_ = cmd.RegisterFlagCompletionFunc("type", invoiceTypeFlagCompletion)
14+
15+
}
16+
17+
func InvoiceType() (string, error) {
18+
if err := validateInvoiceType(invoiceType); err != nil {
19+
return "", err
20+
}
21+
return invoiceType, nil
22+
}
23+
24+
func validateInvoiceType(invoiceType string) error {
25+
switch invoiceType {
26+
case "issued", "all", "upcoming":
27+
return nil
28+
default:
29+
return fmt.Errorf("type parameter must be either 'all' or 'upcoming' or 'issued'")
30+
}
31+
}
32+
33+
func invoiceTypeFlagCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
34+
return []string{"issued", "upcoming", "all"}, cobra.ShellCompDirectiveDefault
35+
}

0 commit comments

Comments
 (0)