Skip to content

Commit

Permalink
use tabwriter in favour of tablewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
endophage committed Jun 30, 2016
1 parent 0517d57 commit 3902f82
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 1,051 deletions.
8 changes: 0 additions & 8 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions cmd/notary/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/docker/notary"
notaryclient "github.com/docker/notary/client"
Expand Down Expand Up @@ -198,7 +199,10 @@ func (d *delegationCommander) delegationRemove(cmd *cobra.Command, args []string
removingItems = removingItems + "with all paths, "
}
if d.paths != nil {
removingItems = removingItems + fmt.Sprintf("with paths [%s], ", prettyPrintPaths(d.paths))
removingItems = removingItems + fmt.Sprintf(
"with paths [%s], ",
strings.Join(prettyPaths(d.paths), "\n"),
)
}
cmd.Printf("Removal of delegation role %s %sto repository \"%s\" staged for next publish.\n", role, removingItems, gun)
}
Expand Down Expand Up @@ -289,7 +293,10 @@ func (d *delegationCommander) delegationAdd(cmd *cobra.Command, args []string) e
addingItems = addingItems + fmt.Sprintf("with keys %s, ", pubKeyIDs)
}
if d.paths != nil || d.allPaths {
addingItems = addingItems + fmt.Sprintf("with paths [%s], ", prettyPrintPaths(d.paths))
addingItems = addingItems + fmt.Sprintf(
"with paths [%s], ",
strings.Join(prettyPaths(d.paths), "\n"),
)
}
cmd.Printf(
"Addition of delegation role %s %sto repository \"%s\" staged for next publish.\n",
Expand Down
105 changes: 75 additions & 30 deletions cmd/notary/prettyprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import (
"io"
"sort"
"strings"
"text/tabwriter"

"github.com/docker/notary/client"
"github.com/docker/notary/trustmanager"
"github.com/docker/notary/tuf/data"
"github.com/olekukonko/tablewriter"
)

// returns a tablewriter
func getTable(headers []string, writer io.Writer) *tablewriter.Table {
table := tablewriter.NewWriter(writer)
table.SetBorder(false)
table.SetColumnSeparator(" ")
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("-")
table.SetAutoWrapText(false)
table.SetHeader(headers)
return table
const fourItemRow = "%s\t%s\t%s\t%s\n"

func initTabWriter(columns []string, writer io.Writer) *tabwriter.Writer {
tw := tabwriter.NewWriter(writer, 4, 4, 4, ' ', 0)
fmt.Fprintln(tw, strings.Join(columns, "\t"))
breakLine := make([]string, 0, len(columns))
for _, h := range columns {
breakLine = append(
breakLine,
strings.Repeat("-", len(h)),
)
}
fmt.Fprintln(tw, strings.Join(breakLine, "\t"))
return tw
}

// --- pretty printing certs ---
Expand Down Expand Up @@ -109,17 +113,19 @@ func prettyPrintKeys(keyStores []trustmanager.KeyStore, writer io.Writer) {

sort.Stable(keyInfoSorter(info))

table := getTable([]string{"ROLE", "GUN", "KEY ID", "LOCATION"}, writer)
tw := initTabWriter([]string{"ROLE", "GUN", "KEY ID", "LOCATION"}, writer)

for _, oneKeyInfo := range info {
table.Append([]string{
fmt.Fprintf(
tw,
fourItemRow,
oneKeyInfo.role,
truncateWithEllipsis(oneKeyInfo.gun, maxGUNWidth, true),
oneKeyInfo.keyID,
truncateWithEllipsis(oneKeyInfo.location, maxLocWidth, true),
})
)
}
table.Render()
tw.Flush()
}

// --- pretty printing targets ---
Expand Down Expand Up @@ -151,17 +157,19 @@ func prettyPrintTargets(ts []*client.TargetWithRole, writer io.Writer) {

sort.Stable(targetsSorter(ts))

table := getTable([]string{"Name", "Digest", "Size (bytes)", "Role"}, writer)
tw := initTabWriter([]string{"NAME", "DIGEST", "SIZE (BYTES)", "ROLE"}, writer)

for _, t := range ts {
table.Append([]string{
fmt.Fprintf(
tw,
fourItemRow,
t.Name,
hex.EncodeToString(t.Hashes["sha256"]),
fmt.Sprintf("%d", t.Length),
t.Role,
})
)
}
table.Render()
tw.Flush()
}

// Pretty-prints the list of provided Roles
Expand All @@ -174,30 +182,67 @@ func prettyPrintRoles(rs []*data.Role, writer io.Writer, roleType string) {
// this sorter works for Role types
sort.Stable(roleSorter(rs))

table := getTable([]string{"Role", "Paths", "Key IDs", "Threshold"}, writer)
tw := initTabWriter([]string{"ROLE", "PATHS", "KEY IDS", "THRESHOLD"}, writer)

for _, r := range rs {
table.Append([]string{
var path, kid string
pp := prettyPaths(r.Paths)
if len(pp) > 0 {
path = pp[0]
}
if len(r.KeyIDs) > 0 {
kid = r.KeyIDs[0]
}
fmt.Fprintf(
tw,
fourItemRow,
r.Name,
prettyPrintPaths(r.Paths),
strings.Join(r.KeyIDs, "\n"),
path,
kid,
fmt.Sprintf("%v", r.Threshold),
})
)
printExtraRoleRows(tw, pp, r.KeyIDs)
}
tw.Flush()
}

func printExtraRoleRows(tw *tabwriter.Writer, paths, keyIDs []string) {
lPaths := len(paths)
lKeyIDs := len(keyIDs)
longer := len(keyIDs)
if len(paths) > len(keyIDs) {
longer = len(paths)
}
for i := 1; i < longer; i++ {
var path, kid string
if lPaths > i {
path = paths[i]
}
if lKeyIDs > i {
kid = keyIDs[i]
}
fmt.Fprintf(
tw,
fourItemRow,
"",
path,
kid,
"",
)
}
table.Render()
}

// Pretty-prints a list of delegation paths, and ensures the empty string is printed as "" in the console
func prettyPrintPaths(paths []string) string {
// Pretty-formats a list of delegation paths, and ensures the empty string is printed as "" in the console
func prettyPaths(paths []string) []string {
// sort paths first
sort.Strings(paths)
prettyPaths := []string{}
pp := make([]string, 0, len(paths))
for _, path := range paths {
// manually escape "" and designate that it is all paths with an extra print <all paths>
if path == "" {
path = "\"\" <all paths>"
}
prettyPaths = append(prettyPaths, path)
pp = append(pp, path)
}
return strings.Join(prettyPaths, "\n")
return pp
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3902f82

Please sign in to comment.