Add datastore to snapshots, force Datastore display when jail exist on multi datastores

This commit is contained in:
yo
2022-06-18 18:24:09 +02:00
parent 1c04f62ed8
commit 9218ffafe1
6 changed files with 145 additions and 92 deletions

View File

@ -391,7 +391,7 @@ func getJailFromArray(name string, jarray []Jail) (*Jail, error) {
return &Jail{}, errors.New("Jail not found")
}
/*****************************************************************************
/******************************************************************************
*
* Generic utilities
*
@ -405,6 +405,28 @@ func isStringInArray(strarr []string, searched string) bool {
return false
}
func getDatastoreFromArray(name string, dsa []Datastore) (*Datastore, error) {
for _, d := range dsa {
if name == d.Name {
return &d, nil
}
}
return &Datastore{}, errors.New("Datastore not found")
}
/******************************************************************************
* Return the quantity of jails with the name passed as parameter
*****************************************************************************/
func countOfJailsWithThisName(name string) int {
count := 0
for _, j := range gJails {
if strings.EqualFold(j.Name, name) {
count++
}
}
return count
}
/********************************************************************************
* Recurse into structure, returning reflect.Kind of named field.
* Nested fields are named with a dot (ex "MyStruct.MyField")
@ -836,7 +858,7 @@ func displayJailsFields(jails []Jail, valsToDisplay []string) {
/********************************************************************************
* Pretty display of snapshots field
* Fields to show are given in a string array parameter
* Ex. : displaySnapshotsFields(snapshots, ["Name", "Dsname", "Used"])
* Ex. : displaySnapshotsFields(snapshots, ["Name", "Datastore", "Used"])
*******************************************************************************/
func displaySnapshotsFields(snaps []Snapshot, valsToDisplay []string) {
/* A line is defined by :
@ -878,7 +900,14 @@ func displaySnapshotsFields(snaps []Snapshot, valsToDisplay []string) {
itnr := len(strings.Split(string(a.FieldByName(f).Interface().(string)), ","))
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface())) / itnr
} else {
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface()))
// Special case of disk size : We will print human readable values
if field.Name == "Used" || field.Name == "Referenced" || field.Name == "Available" {
var v datasize.ByteSize
v.UnmarshalText([]byte(fmt.Sprintf("%v", a.FieldByName(f).Interface())))
field.MaxLen = len(fmt.Sprintf("%v", v.HumanReadable()))
} else {
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface()))
}
}
field.Value = fmt.Sprintf("%v", a.FieldByName(f).Interface())
} else {
@ -983,25 +1012,24 @@ func displaySnapshotsFields(snaps []Snapshot, valsToDisplay []string) {
fmt.Printf("|")
}
// Special cases of value displaying
/* if f.Name == "JID" && f.Value == "0" {
fmt.Printf(" ")
} else if f.Name == "Ip4_addr" {
ia := strings.Split(f.Value, ",")
// If we have more than 1 value we need to finish this line, and store value for writing at the end of line loop
for i, inter := range ia {
if i > 0 {
supplines[f.Name] = inter
} else {
fmt.Printf(" %s", inter)
}
}
//fmt.Printf(" %s", strings.Split(strings.Split(f.Value, "|")[1], "/")[0])
} else {*/
fmt.Printf(" %s", f.Value)
/*}*/
// Complete with spaces to the max length
for i := len(f.Value) + 1; i < f.MaxLen+1; i++ {
fmt.Printf(" ")
// Pretty print of sizes
if f.Name == "Used" || f.Name == "Referenced" || f.Name == "Available" {
var v datasize.ByteSize
err := v.UnmarshalText([]byte(f.Value))
if err != nil {
return
}
fmt.Printf(" %s", v.HumanReadable())
// Complete with spaces to the max length
for i := len(v.HumanReadable()) + 1; i < f.MaxLen+1; i++ {
fmt.Printf(" ")
}
} else {
fmt.Printf(" %s", f.Value)
// Complete with spaces to the max length
for i := len(f.Value) + 1; i < f.MaxLen+1; i++ {
fmt.Printf(" ")
}
}
fmt.Printf(" |")
}
@ -2125,6 +2153,12 @@ func initJailSortStruct() JailSort {
ConfigPathDec: func(j1, j2 *Jail) bool {
return j1.ConfigPath > j2.ConfigPath
},
DatastoreInc: func(j1, j2 *Jail) bool {
return j1.Datastore < j2.Datastore
},
DatastoreDec: func(j1, j2 *Jail) bool {
return j1.Datastore > j2.Datastore
},
InternalNameInc: func(j1, j2 *Jail) bool {
return j1.InternalName < j2.InternalName
},
@ -2241,11 +2275,11 @@ func initSnapshotSortStruct() SnapshotSort {
NameDec: func(s1, s2 *Snapshot) bool {
return s1.Name > s2.Name
},
DsnameInc: func(s1, s2 *Snapshot) bool {
return s1.Dsname < s2.Dsname
DatastoreInc: func(s1, s2 *Snapshot) bool {
return s1.Datastore < s2.Datastore
},
DsnameDec: func(s1, s2 *Snapshot) bool {
return s1.Dsname > s2.Dsname
DatastoreDec: func(s1, s2 *Snapshot) bool {
return s1.Datastore > s2.Datastore
},
JailnameInc: func(s1, s2 *Snapshot) bool {
return s1.Jailname < s2.Jailname