mirror of
https://github.com/sbrow/envr.git
synced 2025-12-29 23:47:39 -05:00
feat(sync): Results are now displayed in a table.
Release-As: 0.1.1
This commit is contained in:
41
cmd/sync.go
41
cmd/sync.go
@@ -1,8 +1,11 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/mattn/go-isatty"
|
||||||
|
"github.com/olekukonko/tablewriter"
|
||||||
"github.com/sbrow/envr/app"
|
"github.com/sbrow/envr/app"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -22,33 +25,53 @@ var syncCmd = &cobra.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
for _, file := range files {
|
type syncResult struct {
|
||||||
fmt.Printf("%s\n", file.Path)
|
Path string `json:"path"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
}
|
||||||
|
var results []syncResult
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
// Syncronize the filesystem with the database.
|
// Syncronize the filesystem with the database.
|
||||||
changed, err := file.Sync()
|
changed, err := file.Sync()
|
||||||
|
|
||||||
|
var status string
|
||||||
switch changed {
|
switch changed {
|
||||||
case app.Updated:
|
case app.Updated:
|
||||||
fmt.Printf("File updated - changes saved\n")
|
status = "Backed Up"
|
||||||
if err := db.Insert(file); err != nil {
|
if err := db.Insert(file); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case app.Restored:
|
case app.Restored:
|
||||||
fmt.Printf("File missing - restored backup\n")
|
status = "Restored"
|
||||||
case app.Error:
|
case app.Error:
|
||||||
if err == nil {
|
if err == nil {
|
||||||
panic("err cannot be nil when Sync returns Error")
|
panic("err cannot be nil when Sync returns Error")
|
||||||
} else {
|
|
||||||
fmt.Printf("%s\n", err)
|
|
||||||
}
|
}
|
||||||
|
status = err.Error()
|
||||||
case app.Noop:
|
case app.Noop:
|
||||||
fmt.Println("Nothing to do")
|
status = "OK"
|
||||||
default:
|
default:
|
||||||
panic("Unknown result")
|
panic("Unknown result")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("")
|
results = append(results, syncResult{
|
||||||
|
Path: file.Path,
|
||||||
|
Status: status,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if isatty.IsTerminal(os.Stdout.Fd()) {
|
||||||
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
|
table.Header([]string{"File", "Status"})
|
||||||
|
|
||||||
|
for _, result := range results {
|
||||||
|
table.Append([]string{result.Path, result.Status})
|
||||||
|
}
|
||||||
|
table.Render()
|
||||||
|
} else {
|
||||||
|
encoder := json.NewEncoder(os.Stdout)
|
||||||
|
return encoder.Encode(results)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in New Issue
Block a user