9cd64aa779d343a58ce31220efe9c978b019d872

Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

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

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

Diff

  1diff --git a/go.mod b/go.mod
  2index e573ba959c4a65f0651c9d94c47ded46ee3dd837..0649b5ab934df6947d3efb00202432bf2ec809c9 100644
  3--- a/go.mod
  4+++ b/go.mod
  5@@ -23,7 +23,6 @@ require (
  6 	github.com/aymanbagabas/git-module v1.8.4-0.20231101154130-8d27204ac6d2
  7 	github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7
  8 	github.com/caarlos0/env/v11 v11.3.1
  9-	github.com/caarlos0/tablewriter v0.1.0
 10 	github.com/charmbracelet/git-lfs-transfer v0.1.1-0.20240708204110-bacbfdb68d92
 11 	github.com/charmbracelet/keygen v0.5.1
 12 	github.com/charmbracelet/log v0.4.0
 13diff --git a/go.sum b/go.sum
 14index 424e57269f541dae722600984a4fb8e81b3f4296..51fb8deb0f43f8de38d1bf49ed1eeff60fb874f0 100644
 15--- a/go.sum
 16+++ b/go.sum
 17@@ -24,8 +24,6 @@ github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7 h1:kJP/C2eL9DCKr
 18 github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7/go.mod h1:mSkwb/eZEwOJJJ4tqAKiuhLIPe0e9+FKhlU0oMCpbf8=
 19 github.com/caarlos0/env/v11 v11.3.1 h1:cArPWC15hWmEt+gWk7YBi7lEXTXCvpaSdCiZE2X5mCA=
 20 github.com/caarlos0/env/v11 v11.3.1/go.mod h1:qupehSf/Y0TUTsxKywqRt/vJjN5nz6vauiYEUUr8P4U=
 21-github.com/caarlos0/tablewriter v0.1.0 h1:HWwl/Zh3GKgVejSeG8lKHc28YBbI7bLRW2tgvxFF2DA=
 22-github.com/caarlos0/tablewriter v0.1.0/go.mod h1:oZ3/mQeP+SC5c1Dr6zv/6jCf0dfsUWq+PuwNw8l3ir0=
 23 github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
 24 github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
 25 github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
 26diff --git a/pkg/ssh/cmd/token.go b/pkg/ssh/cmd/token.go
 27index ddb4d19e2dc4523c97f1066a399cdb5aa1b365a1..6fb8295379189c7e42cf26907c26e08862f0da7d 100644
 28--- a/pkg/ssh/cmd/token.go
 29+++ b/pkg/ssh/cmd/token.go
 30@@ -6,7 +6,7 @@ import (
 31 	"time"
 32 
 33 	"github.com/caarlos0/duration"
 34-	"github.com/caarlos0/tablewriter"
 35+	"github.com/charmbracelet/lipgloss/table"
 36 	"github.com/charmbracelet/soft-serve/pkg/backend"
 37 	"github.com/charmbracelet/soft-serve/pkg/proto"
 38 	"github.com/dustin/go-humanize"
 39@@ -92,28 +92,25 @@ func TokenCommand() *cobra.Command {
 40 			}
 41 
 42 			now := time.Now()
 43-			return tablewriter.Render(
 44-				cmd.OutOrStdout(),
 45-				tokens,
 46-				[]string{"ID", "Name", "Created At", "Expires In"},
 47-				func(t proto.AccessToken) ([]string, error) {
 48-					expiresAt := "-"
 49-					if !t.ExpiresAt.IsZero() {
 50-						if now.After(t.ExpiresAt) {
 51-							expiresAt = "expired"
 52-						} else {
 53-							expiresAt = humanize.Time(t.ExpiresAt)
 54-						}
 55+			table := table.New().Headers("ID", "Name", "Created At", "Expires In")
 56+			for _, token := range tokens {
 57+				expiresAt := "-"
 58+				if !token.ExpiresAt.IsZero() {
 59+					if now.After(token.ExpiresAt) {
 60+						expiresAt = "expired"
 61+					} else {
 62+						expiresAt = humanize.Time(token.ExpiresAt)
 63 					}
 64+				}
 65 
 66-					return []string{
 67-						strconv.FormatInt(t.ID, 10),
 68-						t.Name,
 69-						humanize.Time(t.CreatedAt),
 70-						expiresAt,
 71-					}, nil
 72-				},
 73-			)
 74+				table = table.Row(strconv.FormatInt(token.ID, 10),
 75+					token.Name,
 76+					humanize.Time(token.CreatedAt),
 77+					expiresAt,
 78+				)
 79+			}
 80+			cmd.Println(table)
 81+			return nil
 82 		},
 83 	}
 84 
 85diff --git a/pkg/ssh/cmd/webhooks.go b/pkg/ssh/cmd/webhooks.go
 86index e33ca9b0e731936e8e0fd998f17dc59d11f5eac9..0b894a58e63ec4b5fcb74616c03850f96d7f1720 100644
 87--- a/pkg/ssh/cmd/webhooks.go
 88+++ b/pkg/ssh/cmd/webhooks.go
 89@@ -5,7 +5,7 @@ import (
 90 	"strconv"
 91 	"strings"
 92 
 93-	"github.com/caarlos0/tablewriter"
 94+	"github.com/charmbracelet/lipgloss/table"
 95 	"github.com/charmbracelet/soft-serve/pkg/backend"
 96 	"github.com/charmbracelet/soft-serve/pkg/webhook"
 97 	"github.com/dustin/go-humanize"
 98@@ -60,28 +60,24 @@ func webhookListCommand() *cobra.Command {
 99 				return err
100 			}
101 
102-			return tablewriter.Render(
103-				cmd.OutOrStdout(),
104-				webhooks,
105-				[]string{"ID", "URL", "Events", "Active", "Created At", "Updated At"},
106-				func(h webhook.Hook) ([]string, error) {
107-					events := make([]string, len(h.Events))
108-					for i, e := range h.Events {
109-						events[i] = e.String()
110-					}
111-
112-					row := []string{
113-						strconv.FormatInt(h.ID, 10),
114-						h.URL,
115-						strings.Join(events, ","),
116-						strconv.FormatBool(h.Active),
117-						humanize.Time(h.CreatedAt),
118-						humanize.Time(h.UpdatedAt),
119-					}
120+			table := table.New().Headers("ID", "URL", "Events", "Active", "Created At", "Updated At")
121+			for _, h := range webhooks {
122+				events := make([]string, len(h.Events))
123+				for i, e := range h.Events {
124+					events[i] = e.String()
125+				}
126 
127-					return row, nil
128-				},
129-			)
130+				table = table.Row(
131+					strconv.FormatInt(h.ID, 10),
132+					h.URL,
133+					strings.Join(events, ","),
134+					strconv.FormatBool(h.Active),
135+					humanize.Time(h.CreatedAt),
136+					humanize.Time(h.UpdatedAt),
137+				)
138+			}
139+			cmd.Println(table)
140+			return nil
141 		},
142 	}
143 
144@@ -290,24 +286,21 @@ func webhookDeliveriesListCommand() *cobra.Command {
145 				return err
146 			}
147 
148-			return tablewriter.Render(
149-				cmd.OutOrStdout(),
150-				dels,
151-				[]string{"Status", "ID", "Event", "Created At"},
152-				func(d webhook.Delivery) ([]string, error) {
153-					status := "❌"
154-					if d.ResponseStatus >= 200 && d.ResponseStatus < 300 {
155-						status = "✅"
156-					}
157-
158-					return []string{
159-						status,
160-						d.ID.String(),
161-						d.Event.String(),
162-						humanize.Time(d.CreatedAt),
163-					}, nil
164-				},
165-			)
166+			table := table.New().Headers("Status", "ID", "Event", "Created At")
167+			for _, d := range dels {
168+				status := "❌"
169+				if d.ResponseStatus >= 200 && d.ResponseStatus < 300 {
170+					status = "✅"
171+				}
172+				table = table.Row(
173+					status,
174+					d.ID.String(),
175+					d.Event.String(),
176+					humanize.Time(d.CreatedAt),
177+				)
178+			}
179+			cmd.Println(table)
180+			return nil
181 		},
182 	}
183 
184diff --git a/testscript/testdata/token.txtar b/testscript/testdata/token.txtar
185index 7a88701c1d73fe58e45f9788e3ce0f330c2029c8..87aa1fd570acf0a0ee306408c71528f91d494703 100644
186--- a/testscript/testdata/token.txtar
187+++ b/testscript/testdata/token.txtar
188@@ -22,9 +22,9 @@ stderr 'Access token created'
189 # list tokens
190 usoft token list
191 cp stdout tokens.txt
192-grep '1\s+test1.*-' tokens.txt
193-grep '2\s+test2.*1 year from now' tokens.txt
194-grep '3\s+test3.*expired' tokens.txt
195+grep '1.*test1.*-' tokens.txt
196+grep '2.*test2.*1 year from now' tokens.txt
197+grep '3.*est3.*expired' tokens.txt
198 
199 # delete token
200 usoft token delete 1