Correctly display multiple jails snapshots

This commit is contained in:
yo 2022-04-04 20:10:42 +02:00
parent a12c268be2
commit 966a3d57c1
3 changed files with 22 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import (
*******************************************************************************/ *******************************************************************************/
func ListJailsSnapshots(args []string) { func ListJailsSnapshots(args []string) {
var jailNames []string var jailNames []string
var snapshots []Snapshot
if len(args) > 0 { if len(args) > 0 {
for _, a := range args { for _, a := range args {
@ -20,24 +21,25 @@ func ListJailsSnapshots(args []string) {
if len(jailNames) == 0 || len(args) == 0 { if len(jailNames) == 0 || len(args) == 0 {
for _, j := range gJails { for _, j := range gJails {
listJailSnapshots(j) snapshots = append(snapshots, listJailSnapshots(j)...)
} }
} else { } else {
for _, cj := range gJails { for _, cj := range gJails {
for _, jn := range jailNames { for _, jn := range jailNames {
if strings.EqualFold(cj.Name, jn) { 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 * List all snapshots a jail have
*******************************************************************************/ *******************************************************************************/
func listJailSnapshots(jail Jail) { func listJailSnapshots(jail Jail) []Snapshot {
var snapshots []Snapshot var snapshots []Snapshot
// 1. List all datasets // 1. List all datasets
@ -48,7 +50,7 @@ func listJailSnapshots(jail Jail) {
out, err := executeCommand(cmd) out, err := executeCommand(cmd)
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err.Error()) fmt.Printf("Error: %s\n", err.Error())
return return snapshots
} }
dateLayout := "Mon Jan _2 15:04 2006" dateLayout := "Mon Jan _2 15:04 2006"
@ -61,7 +63,7 @@ func listJailSnapshots(jail Jail) {
creationts, err := time.ParseInLocation(dateLayout, ls[4], loc) creationts, err := time.ParseInLocation(dateLayout, ls[4], loc)
if err != nil { if err != nil {
fmt.Println("Error while parsing date %s:", ls[4], err) fmt.Println("Error while parsing date %s:", ls[4], err)
return return snapshots
} }
// Get subdir to append to snapshot name // Get subdir to append to snapshot name
subdir := strings.Replace(strings.Split(ls[0], "@")[0], subdir := strings.Replace(strings.Split(ls[0], "@")[0],
@ -69,6 +71,7 @@ func listJailSnapshots(jail Jail) {
snapshots = append(snapshots, Snapshot{Dsname: ls[0], snapshots = append(snapshots, Snapshot{Dsname: ls[0],
Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir), Name: fmt.Sprintf("%s%s", strings.Split(ls[0], "@")[1], subdir),
Jailname: jail.Name,
Mountpoint: ls[1], Mountpoint: ls[1],
Used: ls[2], Used: ls[2],
Referenced: ls[3], Referenced: ls[3],
@ -80,9 +83,5 @@ func listJailSnapshots(jail Jail) {
ss := initSnapshotSortStruct() ss := initSnapshotSortStruct()
SnapshotsOrderedBy(ss.CreationInc).Sort(snapshots) SnapshotsOrderedBy(ss.CreationInc).Sort(snapshots)
// TODO : Pretty display. Make an interface of displayStructFields? return snapshots
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)
}*/
} }

View File

@ -181,6 +181,7 @@ type Snapshot struct {
// snapshot name is stored after '@' in dataset name // snapshot name is stored after '@' in dataset name
Name string Name string
Dsname string Dsname string
Jailname string
Mountpoint string Mountpoint string
Used string Used string
Referenced string Referenced string
@ -486,8 +487,10 @@ type JailConfigSort struct {
type SnapshotSort struct { type SnapshotSort struct {
NameInc snapshotLessFunc NameInc snapshotLessFunc
NameDec snapshotLessFunc NameDec snapshotLessFunc
DsNameInc snapshotLessFunc DsnameInc snapshotLessFunc
DsNameDec snapshotLessFunc DsnameDec snapshotLessFunc
JailnameInc snapshotLessFunc
JailnameDec snapshotLessFunc
MountpointInc snapshotLessFunc MountpointInc snapshotLessFunc
MountpointDec snapshotLessFunc MountpointDec snapshotLessFunc
UsedInc snapshotLessFunc UsedInc snapshotLessFunc

View File

@ -1659,12 +1659,18 @@ func initSnapshotSortStruct() SnapshotSort {
NameDec: func(s1, s2 *Snapshot) bool { NameDec: func(s1, s2 *Snapshot) bool {
return s1.Name > s2.Name return s1.Name > s2.Name
}, },
DsNameInc: func(s1, s2 *Snapshot) bool { DsnameInc: func(s1, s2 *Snapshot) bool {
return s1.Dsname < s2.Dsname return s1.Dsname < s2.Dsname
}, },
DsNameDec: func(s1, s2 *Snapshot) bool { DsnameDec: func(s1, s2 *Snapshot) bool {
return s1.Dsname > s2.Dsname 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 { MountpointInc: func(s1, s2 *Snapshot) bool {
return s1.Mountpoint < s2.Mountpoint return s1.Mountpoint < s2.Mountpoint
}, },