Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty print command output #244

Merged
merged 4 commits into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions cmd/wayback/wayback.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"fmt"
"net/url"
"os"
"strings"
"time"

"github.com/jedib0t/go-pretty/v6/list"
"github.com/spf13/cobra"
"github.com/wabarc/wayback"
"github.com/wabarc/wayback/errors"
Expand Down Expand Up @@ -42,16 +44,9 @@ func archive(cmd *cobra.Command, args []string) {
return err
}

for _, col := range cols {
cmd.Println(col.Src, "=>", col.Dst)
if bundle, ok := rdx.Load(reduxer.Src(col.Src)); ok {
for _, asset := range assets(bundle.Artifact()) {
if asset.Local == "" {
continue
}
cmd.Println(col.Src, "=>", asset.Local)
}
}
content := pretty(cols, rdx)
for _, line := range strings.Split(content, "\n") {
cmd.Println(line)
}

if err := g.Wait(); err != nil {
Expand All @@ -74,6 +69,54 @@ func archive(cmd *cobra.Command, args []string) {
}
}

func pretty(cols []wayback.Collect, rdx reduxer.Reduxer) string {
writer := list.NewWriter()
defer writer.Reset()

type uri string
type collects []wayback.Collect
grouped := make(map[uri]collects, len(cols)/4)
for _, col := range cols {
src := uri(col.Src)
grouped[src] = append(grouped[src], col)
}

for src := range grouped {
writer.AppendItem(src)
writer.Indent()
items := make([]interface{}, 0)
for _, col := range grouped[src] {
item := fmt.Sprintf("%s: %s", strings.ToUpper(col.Arc), col.Dst)
items = append(items, item)
}

hasArtifact := false
artifacts := make([]interface{}, 0)
if bundle, ok := rdx.Load(reduxer.Src(src)); ok {
for _, asset := range assets(bundle.Artifact()) {
hasArtifact = true
if asset.Local == "" {
continue
}
artifacts = append(artifacts, asset.Local)
}
}
if hasArtifact {
items = append(items, "Artifacts")
}
writer.AppendItems(items)
if hasArtifact {
writer.Indent()
writer.AppendItems(artifacts)
writer.UnIndent()
}
writer.UnIndent()
}
writer.SetStyle(list.StyleConnectedRounded)

return writer.Render()
}

func unmarshalArgs(args []string) (urls []*url.URL, err error) {
for _, s := range args {
uri, er := url.Parse(s)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-version v1.6.0
github.com/iawia002/lux v0.14.0
github.com/jedib0t/go-pretty/v6 v6.4.0
github.com/mattn/go-mastodon v0.0.5-0.20210515144304-86627ec7d635
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921i
github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A=
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
github.com/jedib0t/go-pretty/v6 v6.4.0 h1:YlI/2zYDrweA4MThiYMKtGRfT+2qZOO65ulej8GTcVI=
github.com/jedib0t/go-pretty/v6 v6.4.0/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down Expand Up @@ -582,6 +584,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -691,6 +694,7 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down