11package cmd
22
33import (
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