Skip to content

Commit

Permalink
fix: invalid log head.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Jan 17, 2024
1 parent 68954bf commit 1bcc23b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/hsu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 6 additions & 1 deletion internal/fetcher/hsu.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fetcher

import (
"fmt"
"io"
"sort"
"strconv"
Expand Down Expand Up @@ -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().
Expand Down
27 changes: 7 additions & 20 deletions internal/log/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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{}
}

0 comments on commit 1bcc23b

Please sign in to comment.