Skip to content

Commit 42796ec

Browse files
author
Spencer Brower
committed
feat(sync): Results are now displayed in a table.
Release-As: 0.1.1
1 parent 8a349dd commit 42796ec

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

cmd/sync.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cmd
22

33
import (
4-
"fmt"
4+
"encoding/json"
5+
"os"
56

7+
"github.com/mattn/go-isatty"
8+
"github.com/olekukonko/tablewriter"
69
"github.com/sbrow/envr/app"
710
"github.com/spf13/cobra"
811
)
@@ -22,33 +25,53 @@ var syncCmd = &cobra.Command{
2225
if err != nil {
2326
return err
2427
} else {
25-
for _, file := range files {
26-
fmt.Printf("%s\n", file.Path)
28+
type syncResult struct {
29+
Path string `json:"path"`
30+
Status string `json:"status"`
31+
}
32+
var results []syncResult
2733

34+
for _, file := range files {
2835
// Syncronize the filesystem with the database.
2936
changed, err := file.Sync()
3037

38+
var status string
3139
switch changed {
3240
case app.Updated:
33-
fmt.Printf("File updated - changes saved\n")
41+
status = "Backed Up"
3442
if err := db.Insert(file); err != nil {
3543
return err
3644
}
3745
case app.Restored:
38-
fmt.Printf("File missing - restored backup\n")
46+
status = "Restored"
3947
case app.Error:
4048
if err == nil {
4149
panic("err cannot be nil when Sync returns Error")
42-
} else {
43-
fmt.Printf("%s\n", err)
4450
}
51+
status = err.Error()
4552
case app.Noop:
46-
fmt.Println("Nothing to do")
53+
status = "OK"
4754
default:
4855
panic("Unknown result")
4956
}
5057

51-
fmt.Println("")
58+
results = append(results, syncResult{
59+
Path: file.Path,
60+
Status: status,
61+
})
62+
}
63+
64+
if isatty.IsTerminal(os.Stdout.Fd()) {
65+
table := tablewriter.NewWriter(os.Stdout)
66+
table.Header([]string{"File", "Status"})
67+
68+
for _, result := range results {
69+
table.Append([]string{result.Path, result.Status})
70+
}
71+
table.Render()
72+
} else {
73+
encoder := json.NewEncoder(os.Stdout)
74+
return encoder.Encode(results)
5275
}
5376

5477
return nil

0 commit comments

Comments
 (0)