diff --git a/cmd/snapshots.go b/cmd/snapshots.go index 662d340..31d0549 100644 --- a/cmd/snapshots.go +++ b/cmd/snapshots.go @@ -11,6 +11,7 @@ import ( *******************************************************************************/ func ListJailsSnapshots(args []string) { var jailNames []string + var snapshots []Snapshot if len(args) > 0 { for _, a := range args { @@ -20,24 +21,25 @@ func ListJailsSnapshots(args []string) { if len(jailNames) == 0 || len(args) == 0 { for _, j := range gJails { - listJailSnapshots(j) + snapshots = append(snapshots, listJailSnapshots(j)...) } } else { for _, cj := range gJails { for _, jn := range jailNames { if strings.EqualFold(cj.Name, jn) { - listJailSnapshots(cj) + snapshots = append(snapshots, listJailSnapshots(cj)...) } } } } + displaySnapshotsFields(snapshots, []string{"Jailname","Name","Creation","Referenced","Used"}) } /******************************************************************************** * List all snapshots a jail have *******************************************************************************/ -func listJailSnapshots(jail Jail) { +func listJailSnapshots(jail Jail) []Snapshot { var snapshots []Snapshot // 1. List all datasets @@ -48,7 +50,7 @@ func listJailSnapshots(jail Jail) { out, err := executeCommand(cmd) if err != nil { fmt.Printf("Error: %s\n", err.Error()) - return + return snapshots } dateLayout := "Mon Jan _2 15:04 2006" @@ -61,7 +63,7 @@ func listJailSnapshots(jail Jail) { creationts, err := time.ParseInLocation(dateLayout, ls[4], loc) if err != nil { fmt.Println("Error while parsing date %s:", ls[4], err) - return + return snapshots } // Get subdir to append to snapshot name subdir := strings.Replace(strings.Split(ls[0], "@")[0], @@ -69,6 +71,7 @@ func listJailSnapshots(jail Jail) { snapshots = append(snapshots, Snapshot{Dsname: ls[0], Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir), + Jailname: jail.Name, Mountpoint: ls[1], Used: ls[2], Referenced: ls[3], @@ -80,9 +83,5 @@ func listJailSnapshots(jail Jail) { ss := initSnapshotSortStruct() SnapshotsOrderedBy(ss.CreationInc).Sort(snapshots) - // TODO : Pretty display. Make an interface of displayStructFields? - displaySnapshotsFields(snapshots, []string{"Name","Creation","Referenced","Used"}) - /*for _, s := range snapshots { - fmt.Printf("| %s | %s | %s | %s |\n", s.Name, s.Creation.String(), s.Referenced, s.Used) - }*/ + return snapshots } diff --git a/cmd/struct.go b/cmd/struct.go index 3209072..e7057c3 100644 --- a/cmd/struct.go +++ b/cmd/struct.go @@ -181,6 +181,7 @@ type Snapshot struct { // snapshot name is stored after '@' in dataset name Name string Dsname string + Jailname string Mountpoint string Used string Referenced string @@ -486,8 +487,10 @@ type JailConfigSort struct { type SnapshotSort struct { NameInc snapshotLessFunc NameDec snapshotLessFunc - DsNameInc snapshotLessFunc - DsNameDec snapshotLessFunc + DsnameInc snapshotLessFunc + DsnameDec snapshotLessFunc + JailnameInc snapshotLessFunc + JailnameDec snapshotLessFunc MountpointInc snapshotLessFunc MountpointDec snapshotLessFunc UsedInc snapshotLessFunc diff --git a/cmd/utils.go b/cmd/utils.go index 25db380..a491ba1 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -1659,12 +1659,18 @@ func initSnapshotSortStruct() SnapshotSort { NameDec: func(s1, s2 *Snapshot) bool { return s1.Name > s2.Name }, - DsNameInc: func(s1, s2 *Snapshot) bool { + DsnameInc: func(s1, s2 *Snapshot) bool { return s1.Dsname < s2.Dsname }, - DsNameDec: func(s1, s2 *Snapshot) bool { + DsnameDec: func(s1, s2 *Snapshot) bool { return s1.Dsname > s2.Dsname }, + JailnameInc: func(s1, s2 *Snapshot) bool { + return s1.Jailname < s2.Jailname + }, + JailnameDec: func(s1, s2 *Snapshot) bool { + return s1.Jailname > s2.Jailname + }, MountpointInc: func(s1, s2 *Snapshot) bool { return s1.Mountpoint < s2.Mountpoint },