Add sort support, can use every field

This commit is contained in:
yo
2021-12-20 22:10:38 +01:00
parent 9af50111f3
commit c36cf9511a
4 changed files with 1299 additions and 3 deletions

View File

@ -357,6 +357,50 @@ func ListJails(args []string, display bool) {
jails = js
}
/************************************************************************************
/ Sort jails
/ We support 3 sort criteria max
/***********************************************************************************/
if len(gSortFields) > 0 {
js := initSortStruct()
// The way we manage criteria quantity is not very elegant...
var fct1, fct2, fct3 reflect.Value
for i, c := range strings.Split(gSortFields, ",") {
var fctName string
if strings.HasPrefix(c, "-") {
fctName = fmt.Sprintf("%sDec", strings.Replace(c, "-", "", 1))
} else { // Par defaut (pas de prefix +/-) on considere un tri incremental
fctName = fmt.Sprintf("%sInc", strings.Replace(c, "+", "", 1))
}
// Get function by its name
fct, _, err := getStructFieldValue(js, fctName)
if err != nil {
fieldName := strings.Replace(strings.Replace(c, "-", "", 1), "+", "", 1)
fmt.Printf("ERROR getting JailSort struct field %s. Please check the field name: %s\n", fctName, fieldName)
return
}
switch i+1 {
case 1: fct1 = fct
case 2: fct2 = fct
case 3: fct3 = fct
}
}
switch len(strings.Split(gSortFields, ",")) {
case 1:
OrderedBy(fct1.Interface().(lessFunc)).Sort(jails)
case 2:
OrderedBy(fct1.Interface().(lessFunc), fct2.Interface().(lessFunc)).Sort(jails)
case 3:
OrderedBy(fct1.Interface().(lessFunc), fct2.Interface().(lessFunc), fct3.Interface().(lessFunc)).Sort(jails)
}
}
/************************************************************************************
/ Finally, display jails
/***********************************************************************************/
if display {
displayStructFields(jails, fields)
}