Skip to content

Commit

Permalink
fix: using lipgloss tables instead of tablewriter (#618)
Browse files Browse the repository at this point in the history
* fix: using lipgloss tables instead of tablewriter

* test: fix

Signed-off-by: Carlos Alexandro Becker <[email protected]>

---------

Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 authored Jan 6, 2025
1 parent 14bbcc3 commit 9cd64aa
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 67 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ require (
github.com/aymanbagabas/git-module v1.8.4-0.20231101154130-8d27204ac6d2
github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7
github.com/caarlos0/env/v11 v11.3.1
github.com/caarlos0/tablewriter v0.1.0
github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240708204110-bacbfdb68d92
github.com/charmbracelet/keygen v0.5.1
github.com/charmbracelet/log v0.4.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7 h1:kJP/C2eL9DCKr
github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7/go.mod h1:mSkwb/eZEwOJJJ4tqAKiuhLIPe0e9+FKhlU0oMCpbf8=
github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
github.com/caarlos0/tablewriter v0.1.0 h1:HWwl/Zh3GKgVejSeG8lKHc28YBbI7bLRW2tgvxFF2DA=
github.com/caarlos0/tablewriter v0.1.0/go.mod h1:oZ3/mQeP+SC5c1Dr6zv/6jCf0dfsUWq+PuwNw8l3ir0=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
Expand Down
39 changes: 18 additions & 21 deletions pkg/ssh/cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/caarlos0/duration"
"github.com/caarlos0/tablewriter"
"github.com/charmbracelet/lipgloss/table"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/proto"
"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -92,28 +92,25 @@ func TokenCommand() *cobra.Command {
}

now := time.Now()
return tablewriter.Render(
cmd.OutOrStdout(),
tokens,
[]string{"ID", "Name", "Created At", "Expires In"},
func(t proto.AccessToken) ([]string, error) {
expiresAt := "-"
if !t.ExpiresAt.IsZero() {
if now.After(t.ExpiresAt) {
expiresAt = "expired"
} else {
expiresAt = humanize.Time(t.ExpiresAt)
}
table := table.New().Headers("ID", "Name", "Created At", "Expires In")
for _, token := range tokens {
expiresAt := "-"
if !token.ExpiresAt.IsZero() {
if now.After(token.ExpiresAt) {
expiresAt = "expired"
} else {
expiresAt = humanize.Time(token.ExpiresAt)
}
}

return []string{
strconv.FormatInt(t.ID, 10),
t.Name,
humanize.Time(t.CreatedAt),
expiresAt,
}, nil
},
)
table = table.Row(strconv.FormatInt(token.ID, 10),
token.Name,
humanize.Time(token.CreatedAt),
expiresAt,
)
}
cmd.Println(table)
return nil
},
}

Expand Down
73 changes: 33 additions & 40 deletions pkg/ssh/cmd/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"strings"

"github.com/caarlos0/tablewriter"
"github.com/charmbracelet/lipgloss/table"
"github.com/charmbracelet/soft-serve/pkg/backend"
"github.com/charmbracelet/soft-serve/pkg/webhook"
"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -60,28 +60,24 @@ func webhookListCommand() *cobra.Command {
return err
}

return tablewriter.Render(
cmd.OutOrStdout(),
webhooks,
[]string{"ID", "URL", "Events", "Active", "Created At", "Updated At"},
func(h webhook.Hook) ([]string, error) {
events := make([]string, len(h.Events))
for i, e := range h.Events {
events[i] = e.String()
}

row := []string{
strconv.FormatInt(h.ID, 10),
h.URL,
strings.Join(events, ","),
strconv.FormatBool(h.Active),
humanize.Time(h.CreatedAt),
humanize.Time(h.UpdatedAt),
}
table := table.New().Headers("ID", "URL", "Events", "Active", "Created At", "Updated At")
for _, h := range webhooks {
events := make([]string, len(h.Events))
for i, e := range h.Events {
events[i] = e.String()
}

return row, nil
},
)
table = table.Row(
strconv.FormatInt(h.ID, 10),
h.URL,
strings.Join(events, ","),
strconv.FormatBool(h.Active),
humanize.Time(h.CreatedAt),
humanize.Time(h.UpdatedAt),
)
}
cmd.Println(table)
return nil
},
}

Expand Down Expand Up @@ -290,24 +286,21 @@ func webhookDeliveriesListCommand() *cobra.Command {
return err
}

return tablewriter.Render(
cmd.OutOrStdout(),
dels,
[]string{"Status", "ID", "Event", "Created At"},
func(d webhook.Delivery) ([]string, error) {
status := "❌"
if d.ResponseStatus >= 200 && d.ResponseStatus < 300 {
status = "✅"
}

return []string{
status,
d.ID.String(),
d.Event.String(),
humanize.Time(d.CreatedAt),
}, nil
},
)
table := table.New().Headers("Status", "ID", "Event", "Created At")
for _, d := range dels {
status := "❌"
if d.ResponseStatus >= 200 && d.ResponseStatus < 300 {
status = "✅"
}
table = table.Row(
status,
d.ID.String(),
d.Event.String(),
humanize.Time(d.CreatedAt),
)
}
cmd.Println(table)
return nil
},
}

Expand Down
6 changes: 3 additions & 3 deletions testscript/testdata/token.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ stderr 'Access token created'
# list tokens
usoft token list
cp stdout tokens.txt
grep '1\s+test1.*-' tokens.txt
grep '2\s+test2.*1 year from now' tokens.txt
grep '3\s+test3.*expired' tokens.txt
grep '1.*test1.*-' tokens.txt
grep '2.*test2.*1 year from now' tokens.txt
grep '3.*est3.*expired' tokens.txt

# delete token
usoft token delete 1
Expand Down

0 comments on commit 9cd64aa

Please sign in to comment.