From 1bcc23bbb5ed95628a244c8946d93799358b01f0 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Wed, 17 Jan 2024 20:47:33 +0800 Subject: [PATCH] fix: invalid log head. --- cmd/hsu.go | 2 +- internal/fetcher/hsu.go | 7 ++++++- internal/log/printer.go | 27 +++++++-------------------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/cmd/hsu.go b/cmd/hsu.go index 7c0dc16..283ec2d 100644 --- a/cmd/hsu.go +++ b/cmd/hsu.go @@ -16,7 +16,7 @@ var hsuCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { log.NewPrinter(). Title("hsu.life Download Information"). - Head(log.DefaultHead). + Head(log.DefaultHead...). Row("Username", flags.Username). Row("Password", flags.HideSensitive(flags.Password)). Row("Config Path", flags.ConfigRoot). diff --git a/internal/fetcher/hsu.go b/internal/fetcher/hsu.go index c56d82f..9e883d4 100644 --- a/internal/fetcher/hsu.go +++ b/internal/fetcher/hsu.go @@ -1,6 +1,7 @@ package fetcher import ( + "fmt" "io" "sort" "strconv" @@ -150,7 +151,11 @@ func newHsuService(config *Config) (service, error) { return nil, err } - c.SetAuthToken(resp.Result().(*HsuLoginResp).Token) + token := resp.Result().(*HsuLoginResp).Token + if token == "" { + return nil, fmt.Errorf("invalid login credential") + } + c.SetAuthToken(token) // Download books. resp, err = c.R(). diff --git a/internal/log/printer.go b/internal/log/printer.go index fc3997b..7aa6614 100644 --- a/internal/log/printer.go +++ b/internal/log/printer.go @@ -7,21 +7,19 @@ import ( "github.com/jedib0t/go-pretty/v6/table" ) -var DefaultHead = []any{"Config Key", "Config Value"} +var DefaultHead = table.Row{"Config Key", "Config Value"} type Printer interface { - Title(title string) Printer // Title adds the table title. - Head(heads ...any) Printer // Head adds the table head. - MaxWidth(width uint8) Printer // MaxColWidth the large column will be trimmed. - Row(fields ...any) Printer // Row add a row to the table. - AllowZeroValue() Printer // AllowZeroValue The row will be printed if it contains zero value. - Print() // Print would print a table-like message from the given config. + Title(title string) Printer // Title adds the table title. + Head(heads ...any) Printer // Head adds the table head. + Row(fields ...any) Printer // Row add a row to the table. + AllowZeroValue() Printer // AllowZeroValue The row will be printed if it contains zero value. + Print() // Print would print a table-like message from the given config. } type tablePrinter struct { title string heads []any - width uint8 rows [][]any allowZero bool } @@ -36,19 +34,8 @@ func (t *tablePrinter) Head(heads ...any) Printer { return t } -func (t *tablePrinter) MaxWidth(width uint8) Printer { - t.width = width - return t -} - func (t *tablePrinter) Row(fields ...any) Printer { if len(fields) > 0 { - // Trim the fields into a small length. - for i, field := range fields { - if f, ok := field.(string); ok && len(f) > int(t.width) { - fields[i] = f[:t.width] + "..." - } - } t.rows = append(t.rows, fields) } return t @@ -94,5 +81,5 @@ func appendRow(writer table.Writer, row []any, allowZero bool) { // NewPrinter will return a printer for table-like logs. func NewPrinter() Printer { - return &tablePrinter{width: 30} + return &tablePrinter{} }