-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added delete option and output formatting.
- Loading branch information
Showing
5 changed files
with
232 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package unicreds | ||
|
||
import ( | ||
"encoding/csv" | ||
"io" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
) | ||
|
||
const ( | ||
// TableFormatTerm format the table for a terminal session | ||
TableFormatTerm = iota // 0 | ||
// TableFormatCSV format the table as CSV | ||
TableFormatCSV // 1 | ||
) | ||
|
||
// TableWriter enables writing of tables in a variety of formats | ||
type TableWriter struct { | ||
tableFormat int | ||
headers []string | ||
rows [][]string | ||
wr io.Writer | ||
} | ||
|
||
// NewTable create a new table writer | ||
func NewTable(wr io.Writer) *TableWriter { | ||
return &TableWriter{wr: wr} | ||
} | ||
|
||
// SetHeaders set the column headers | ||
func (tw *TableWriter) SetHeaders(headers []string) { | ||
tw.headers = headers | ||
} | ||
|
||
// SetFormat set the format | ||
func (tw *TableWriter) SetFormat(tableFormat int) { | ||
tw.tableFormat = tableFormat | ||
} | ||
|
||
func (tw *TableWriter) Write(row []string) { | ||
tw.rows = append(tw.rows, row) | ||
} | ||
|
||
// BulkWrite append an array of rows to the buffer | ||
func (tw *TableWriter) BulkWrite(rows [][]string) { | ||
tw.rows = append(tw.rows, rows...) | ||
} | ||
|
||
// Render render the table out to the supplied writer | ||
func (tw *TableWriter) Render() error { | ||
switch tw.tableFormat { | ||
case TableFormatTerm: | ||
table := tablewriter.NewWriter(tw.wr) | ||
table.SetHeader(tw.headers) | ||
table.AppendBulk(tw.rows) | ||
table.Render() | ||
case TableFormatCSV: | ||
w := csv.NewWriter(tw.wr) | ||
|
||
if err := w.Write(tw.headers); err != nil { | ||
return err | ||
} | ||
|
||
for _, r := range tw.rows { | ||
if err := w.Write(r); err != nil { | ||
return err | ||
} | ||
} | ||
w.Flush() | ||
|
||
if err := w.Error(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package unicreds | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRender(t *testing.T) { | ||
|
||
tt := []struct { | ||
tableFormat int | ||
output string | ||
headers []string | ||
rows [][]string | ||
}{ | ||
{ | ||
tableFormat: TableFormatTerm, | ||
output: `+------------+-----------+ | ||
| NAME | VERSION | | ||
+------------+-----------+ | ||
| testlogin1 | testpass1 | | ||
| testlogin2 | testpass2 | | ||
+------------+-----------+ | ||
`, | ||
headers: []string{"Name", "Version"}, | ||
rows: [][]string{{"testlogin1", "testpass1"}, {"testlogin2", "testpass2"}}, | ||
}, | ||
{ | ||
tableFormat: TableFormatCSV, | ||
output: "Name,Version\ntestlogin1,testpass1\ntestlogin2,testpass2\n", | ||
headers: []string{"Name", "Version"}, | ||
rows: [][]string{{"testlogin1", "testpass1"}, {"testlogin2", "testpass2"}}, | ||
}, | ||
} | ||
|
||
for _, tv := range tt { | ||
var b bytes.Buffer | ||
|
||
table := NewTable(&b) | ||
table.SetHeaders(tv.headers) | ||
table.SetFormat(tv.tableFormat) | ||
table.BulkWrite(tv.rows) | ||
table.Render() | ||
|
||
assert.Equal(t, tv.output, b.String()) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters