2014-08-04 20:55:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-11-25 21:38:14 +00:00
|
|
|
"io"
|
2014-11-24 20:12:32 +00:00
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2014-08-04 20:55:54 +00:00
|
|
|
|
|
|
|
"github.com/fd0/khepri"
|
2014-09-23 20:39:12 +00:00
|
|
|
"github.com/fd0/khepri/backend"
|
2014-08-04 20:55:54 +00:00
|
|
|
)
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
const (
|
|
|
|
minute = 60
|
|
|
|
hour = 60 * minute
|
|
|
|
day = 24 * hour
|
|
|
|
week = 7 * day
|
|
|
|
)
|
|
|
|
|
2014-11-25 21:38:14 +00:00
|
|
|
type Table struct {
|
|
|
|
Header string
|
|
|
|
Rows [][]interface{}
|
|
|
|
|
|
|
|
RowFormat string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTable() Table {
|
|
|
|
return Table{
|
|
|
|
Rows: [][]interface{}{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Table) Print(w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintln(w, t.Header)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = fmt.Fprintln(w, strings.Repeat("-", 70))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, row := range t.Rows {
|
|
|
|
_, err = fmt.Fprintf(w, t.RowFormat+"\n", row...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
const TimeFormat = "2006-01-02 15:04:05"
|
|
|
|
|
|
|
|
func reltime(t time.Time) string {
|
|
|
|
sec := uint64(time.Since(t).Seconds())
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case sec > week:
|
|
|
|
return t.Format(TimeFormat)
|
|
|
|
case sec > day:
|
|
|
|
return fmt.Sprintf("%d days ago", sec/day)
|
|
|
|
case sec > hour:
|
|
|
|
return fmt.Sprintf("%d hours ago", sec/hour)
|
|
|
|
case sec > minute:
|
|
|
|
return fmt.Sprintf("%d minutes ago", sec/minute)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%d seconds ago", sec)
|
|
|
|
}
|
|
|
|
}
|
2014-08-04 20:55:54 +00:00
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
func commandSnapshots(be backend.Server, key *khepri.Key, args []string) error {
|
2014-08-04 20:55:54 +00:00
|
|
|
if len(args) != 0 {
|
|
|
|
return errors.New("usage: snapshots")
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
ch, err := khepri.NewContentHandler(be, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-25 21:38:14 +00:00
|
|
|
tab := NewTable()
|
|
|
|
tab.Header = fmt.Sprintf("%-8s %-19s %-10s %s\n", "ID", "Date", "Source", "Directory")
|
|
|
|
tab.RowFormat = "%-8s %-19s %-10s %s"
|
2014-11-24 20:12:32 +00:00
|
|
|
|
|
|
|
list := []*khepri.Snapshot{}
|
2014-09-23 20:39:12 +00:00
|
|
|
backend.EachID(be, backend.Snapshot, func(id backend.ID) {
|
2014-11-24 20:12:32 +00:00
|
|
|
sn, err := ch.LoadSnapshot(id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error loading snapshot %s: %v\n", id, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pos := sort.Search(len(list), func(i int) bool {
|
|
|
|
return list[i].Time.After(sn.Time)
|
|
|
|
})
|
|
|
|
|
|
|
|
if pos < len(list) {
|
|
|
|
list = append(list, nil)
|
|
|
|
copy(list[pos+1:], list[pos:])
|
|
|
|
list[pos] = sn
|
|
|
|
} else {
|
|
|
|
list = append(list, sn)
|
|
|
|
}
|
2014-09-23 20:39:12 +00:00
|
|
|
})
|
2014-08-04 20:55:54 +00:00
|
|
|
|
2014-11-25 21:38:14 +00:00
|
|
|
plen, err := backend.PrefixLength(be, backend.Snapshot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
for _, sn := range list {
|
2014-11-25 21:38:14 +00:00
|
|
|
tab.Rows = append(tab.Rows, []interface{}{sn.ID()[:plen], sn.Time.Format(TimeFormat), sn.Hostname, sn.Dir})
|
2014-11-24 20:12:32 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 21:38:14 +00:00
|
|
|
tab.Print(os.Stdout)
|
|
|
|
|
2014-08-04 20:55:54 +00:00
|
|
|
return nil
|
|
|
|
}
|