Skip to content

Commit

Permalink
feat: add markdown table rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
BacchusJackson committed May 15, 2024
1 parent 4bdc53d commit 9953904
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 299 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.1] - 2024-05-15

### Changed

- Use a table writing package instead of the builtin package

### Added

- `gatecheck list --markdown` support for rendering markdown tables

## [0.6.0] - 2024-04-26

### Changed
Expand Down
12 changes: 6 additions & 6 deletions cmd/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"os"
"strings"

"github.com/gatecheckdev/gatecheck/pkg/format"
"github.com/gatecheckdev/gatecheck/pkg/gatecheck"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -106,14 +106,14 @@ func LoadConfigFromFile(config *gatecheck.Config, filename string) error {

// WriteConfigInfo as a human readable display table
func WriteConfigInfo(w io.Writer, v *viper.Viper, config *gatecheck.Config) error {
table := format.NewTable()
table.AppendRow("key", "Value")
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"key", "value"})

for _, key := range viper.AllKeys() {
table.AppendRow(key, fmt.Sprintf("%v", viper.Get(key)))
table.Append([]string{key, fmt.Sprintf("%v", viper.Get(key))})
}

_, infoErr := format.NewTableWriter(table).WriteTo(w)
table.Render()
_, configErr := fmt.Fprintln(w, config.String())
return errors.Join(infoErr, configErr)
return configErr
}
26 changes: 24 additions & 2 deletions cmd/v1/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func newListCommand() *cobra.Command {
cmd.Flags().String("epss-file", "", "use this file for epss scores, will not query API")
_ = viper.BindPFlag("cli.list.epss-file", cmd.Flags().Lookup("epss-file"))

cmd.Flags().Bool("markdown", false, "print the list as markdown")

return cmd
}

Expand All @@ -43,9 +45,16 @@ func runList(cmd *cobra.Command, args []string) error {

inputType, _ := cmd.Flags().GetString("input-type")
listAll, _ := cmd.Flags().GetBool("all")
markdown, _ := cmd.Flags().GetBool("markdown")

epssURL := viper.GetString("api.epss-url")
epssFilename := viper.GetString("cli.list.epss-file")

displayFormat := "ascii"
if markdown {
displayFormat = "markdown"
}

src, err := fileOrStdin(filename, cmd)
if err != nil {
return err
Expand All @@ -68,8 +77,21 @@ func runList(cmd *cobra.Command, args []string) error {

if listAll {
slog.Debug("listing with epss scores")
return gatecheck.ListAll(cmd.OutOrStdout(), src, filename, http.DefaultClient, epssURL, epssFile)
return gatecheck.ListAll(
cmd.OutOrStdout(),
src,
filename,
http.DefaultClient,
epssURL,
epssFile,
gatecheck.WithDisplayFormat(displayFormat),
)
}

return gatecheck.List(cmd.OutOrStdout(), src, filename)
return gatecheck.List(
cmd.OutOrStdout(),
src,
filename,
gatecheck.WithDisplayFormat(displayFormat),
)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.1
require (
github.com/dustin/go-humanize v1.0.1
github.com/lmittmann/tint v1.0.4
github.com/olekukonko/tablewriter v0.0.5
github.com/pelletier/go-toml/v2 v2.2.1
github.com/sagikazarmark/slog-shim v0.1.0
github.com/spf13/cobra v1.8.0
Expand All @@ -18,6 +19,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc=
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg=
github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
15 changes: 10 additions & 5 deletions pkg/archive/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"log/slog"
"os"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -101,7 +102,7 @@ func (b *Bundle) AddFrom(r io.Reader, label string, properties map[string]string
p, err := io.ReadAll(r)
_, _ = bytes.NewReader(p).WriteTo(hasher)
if err != nil {
return err
return err
}
digest := fmt.Sprintf("%x", hasher.Sum(nil))

Expand Down Expand Up @@ -146,16 +147,20 @@ func (b *Bundle) Delete(label string) {
}

func (b *Bundle) Content() string {
table := format.NewTable()
table.AppendRow("Label", "Digest", "Tags", "Size")
matrix := format.NewSortableMatrix(make([][]string, 0), 0, format.AlphabeticLess)

for label, descriptor := range b.Manifest().Files {
fileSize := humanize.Bytes(uint64(b.FileSize(label)))
tags := strings.Join(descriptor.Tags, ", ")
table.AppendRow(label, descriptor.Digest, tags, fileSize)
row := []string{label, descriptor.Digest, tags, fileSize}
matrix.Append(row)
}

return format.NewTableWriter(table).String()
sort.Sort(matrix)
buf := new(bytes.Buffer)
header := []string{"Label", "Digest", "Tags", "Size"}
matrix.Table(buf, header).Render()
return buf.String()
}

func TarGzipBundle(dst io.Writer, bundle *Bundle) (int64, error) {
Expand Down
67 changes: 67 additions & 0 deletions pkg/format/matrix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package format

import (
"io"

"github.com/olekukonko/tablewriter"
)

type SortableMatrix struct {
data [][]string
selectedColumn int
lessFunc func(a, b string) bool
}

func NewSortableMatrix(data [][]string, sortColIdx int, sortFunc func(a, b string) bool) *SortableMatrix {
return &SortableMatrix{
data: data,
selectedColumn: sortColIdx,
lessFunc: sortFunc,
}
}

func (m *SortableMatrix) Append(row []string) {
m.data = append(m.data, row)
}

func (m *SortableMatrix) Matrix() [][]string {
return m.data
}

func (m *SortableMatrix) Table(w io.Writer, header []string) *tablewriter.Table {
table := tablewriter.NewWriter(w)
table.SetHeader(header)
table.AppendBulk(m.data)
return table
}

func (m *SortableMatrix) Len() int {
return len(m.data)
}

func (m *SortableMatrix) Swap(i, j int) {
m.data[i], m.data[j] = m.data[j], m.data[i]
}

func (m *SortableMatrix) Less(i, j int) bool {
return m.lessFunc(m.data[i][m.selectedColumn], m.data[j][m.selectedColumn])
}

func AlphabeticLess(a, b string) bool {
return a < b
}

func NewCatagoricLess(categories []string) func(a, b string) bool {
return func(a, b string) bool {
aIndex, bIndex := 0, 0
for i, category := range categories {
if a == category {
aIndex = i
}
if b == category {
bIndex = i
}
}
return aIndex < bIndex
}
}
Loading

0 comments on commit 9953904

Please sign in to comment.