From 2f274dacf0184f930fd946c9e83c8346b37eddba Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Fri, 25 Sep 2020 03:50:25 +0200 Subject: [PATCH] Code cleanup & decoupling --- main.go | 38 +++++++++++++++++++++++--------------- mounts.go | 1 + table.go | 15 ++++++++++++--- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index 9747eeac..b3865321 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,8 @@ var ( jsonOutput = flag.Bool("json", false, "output all devices in JSON format") ) -func renderTables(m []Mount, sortCol int) error { +// renderTables renders all tables. +func renderTables(m []Mount, columns []int, sortCol int) error { var local, network, fuse, special []Mount // sort/filter devices @@ -72,19 +73,6 @@ func renderTables(m []Mount, sortCol int) error { local = append(local, v) } - columns, err := parseColumns(*output) - if err != nil { - return err - } - if len(columns) == 0 { - // no columns supplied, use defaults - if *inodes { - columns = []int{1, 6, 7, 8, 9, 10, 11} - } else { - columns = []int{1, 2, 3, 4, 5, 10, 11} - } - } - // print tables if !*hideLocal || *all { printTable("local", local, sortCol, columns) @@ -101,6 +89,7 @@ func renderTables(m []Mount, sortCol int) error { return nil } +// renderJSON encodes the JSON output and prints it. func renderJSON(m []Mount) error { output, err := json.MarshalIndent(m, "", " ") if err != nil { @@ -111,6 +100,7 @@ func renderJSON(m []Mount) error { return nil } +// parseColumns parses the supplied output flag into a slice of column indices. func parseColumns(cols string) ([]int, error) { var i []int @@ -135,6 +125,21 @@ func parseColumns(cols string) ([]int, error) { func main() { flag.Parse() + // validate flags + columns, err := parseColumns(*output) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + if len(columns) == 0 { + // no columns supplied, use defaults + if *inodes { + columns = []int{1, 6, 7, 8, 9, 10, 11} + } else { + columns = []int{1, 2, 3, 4, 5, 10, 11} + } + } + sortCol, err := stringToSortIndex(*sortBy) if err != nil { fmt.Fprintln(os.Stderr, err) @@ -160,10 +165,12 @@ func main() { os.Exit(1) } + // print out warnings for _, warning := range warnings { fmt.Fprintln(os.Stderr, warning) } + // print JSON if *jsonOutput { err := renderJSON(m) if err != nil { @@ -173,7 +180,8 @@ func main() { return } - err = renderTables(m, sortCol) + // print tables + err = renderTables(m, columns, sortCol) if err != nil { fmt.Fprintln(os.Stderr, err) } diff --git a/mounts.go b/mounts.go index 90b6754d..ccbca36f 100644 --- a/mounts.go +++ b/mounts.go @@ -8,6 +8,7 @@ import ( "golang.org/x/sys/unix" ) +// Mount contains all metadata for a single filesystem mount. type Mount struct { Device string `json:"device"` DeviceType string `json:"device_type"` diff --git a/table.go b/table.go index eb512c52..862e7928 100644 --- a/table.go +++ b/table.go @@ -43,6 +43,7 @@ var ( colorCyan = term.Color("#66C2CD") ) +// printTable prints an individual table of mounts. func printTable(title string, m []Mount, sortBy int, cols []int) { tab := table.NewWriter() tab.SetAllowedRowLength(int(*width)) @@ -138,10 +139,12 @@ func printTable(title string, m []Mount, sortBy int, cols []int) { tab.Render() } +// sizeTransformer makes a size human-readable. func sizeTransformer(val interface{}) string { return sizeToString(val.(uint64)) } +// spaceTransformer makes a size human-readable and applies a color coding. func spaceTransformer(val interface{}) string { free := val.(uint64) @@ -158,6 +161,7 @@ func spaceTransformer(val interface{}) string { return s.String() } +// barTransformer transforms a percentage into a progress-bar. func barTransformer(val interface{}) string { usage := val.(float64) s := termenv.String() @@ -187,9 +191,11 @@ func barTransformer(val interface{}) string { return s.String() } -func inColumns(i []int, j int) bool { - for _, v := range i { - if v == j { +// inColumns return true if the column with index i is in the slice of visible +// columns cols. +func inColumns(cols []int, i int) bool { + for _, v := range cols { + if v == i { return true } } @@ -197,6 +203,7 @@ func inColumns(i []int, j int) bool { return false } +// barWidth returns the width of progress-bars for the given render width. func barWidth() int { switch { case *width < 100: @@ -208,6 +215,7 @@ func barWidth() int { } } +// tableWidth returns the required minimum table width for the given columns. func tableWidth(cols []int, separators bool) int { var sw int if separators { @@ -274,6 +282,7 @@ func stringToSortIndex(s string) (int, error) { return 0, fmt.Errorf("unknown column: %s (valid: %s)", s, strings.Join(columnIDs(), ", ")) } +// columnsIDs returns a slice of all column IDs. func columnIDs() []string { s := make([]string, len(columns)) for i, v := range columns {