fix: using lipgloss tables instead of tablewriter (#618)

Carlos Alexandro Becker created

* fix: using lipgloss tables instead of tablewriter

* test: fix

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Change summary

go.mod                          |  1 
go.sum                          |  2 
pkg/ssh/cmd/token.go            | 39 ++++++++----------
pkg/ssh/cmd/webhooks.go         | 73 +++++++++++++++-------------------
testscript/testdata/token.txtar |  6 +-
5 files changed, 54 insertions(+), 67 deletions(-)

Detailed changes

go.mod 🔗

@@ -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

go.sum 🔗

@@ -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=

pkg/ssh/cmd/token.go 🔗

@@ -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"
@@ -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
 		},
 	}
 

pkg/ssh/cmd/webhooks.go 🔗

@@ -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"
@@ -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
 		},
 	}
 
@@ -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
 		},
 	}
 

testscript/testdata/token.txtar 🔗

@@ -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