sync: Replace []error by go.uber.org/multierr

This commit is contained in:
nemunaire 2023-11-22 12:16:53 +01:00
commit b6966d47ce
25 changed files with 380 additions and 348 deletions

View file

@ -5,29 +5,31 @@ import (
"path"
"strings"
"go.uber.org/multierr"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
)
func EPITACheckFile(file *fic.EFile, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
func EPITACheckFile(file *fic.EFile, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs error) {
// Enforce file format
if path.Ext(file.Name) == ".rar" || path.Ext(file.Name) == ".7z" {
errs = append(errs, fmt.Errorf("this file use a forbidden archive type."))
errs = multierr.Append(errs, fmt.Errorf("this file use a forbidden archive type."))
}
// Check for stange file extension
if strings.HasSuffix(file.Name, ".tar.zip") {
errs = append(errs, fmt.Errorf(".tar.zip is not a valid tar format"))
errs = multierr.Append(errs, fmt.Errorf(".tar.zip is not a valid tar format"))
}
// Check .gz files have a dedicated hash
if path.Ext(file.Name) == ".gz" && !strings.HasSuffix(file.Name, ".tar.gz") && len(file.ChecksumShown) == 0 {
errs = append(errs, fmt.Errorf("digest of original, uncompressed, file missing in DIGESTS.txt"))
errs = multierr.Append(errs, fmt.Errorf("digest of original, uncompressed, file missing in DIGESTS.txt"))
}
// Check for huge file to compress
if file.Size > 4000000 && path.Ext(file.Name) == ".tar" {
errs = append(errs, fmt.Errorf("archive to compress with bzip2"))
errs = multierr.Append(errs, fmt.Errorf("archive to compress with bzip2"))
} else if file.Size > 40000000 && (path.Ext(file.Name) == "" ||
path.Ext(file.Name) == ".csv" ||
path.Ext(file.Name) == ".dump" ||
@ -38,7 +40,7 @@ func EPITACheckFile(file *fic.EFile, _ *fic.Exercice, exceptions *sync.CheckExce
path.Ext(file.Name) == ".pcap" ||
path.Ext(file.Name) == ".pcapng" ||
path.Ext(file.Name) == ".txt") {
errs = append(errs, fmt.Errorf("huge file to compress with gzip"))
errs = multierr.Append(errs, fmt.Errorf("huge file to compress with gzip"))
}
return

View file

@ -6,53 +6,55 @@ import (
"strings"
"unicode"
"go.uber.org/multierr"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
)
func EPITACheckKeyFlag(flag *fic.FlagKey, raw string, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
func EPITACheckKeyFlag(flag *fic.FlagKey, raw string, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs error) {
if (flag.Label[0] == 'Q' || flag.Label[0] == 'q') && (flag.Label[1] == 'U' || flag.Label[1] == 'u') ||
(flag.Label[0] == 'W' || flag.Label[0] == 'w') && (flag.Label[1] == 'H' || flag.Label[1] == 'h') {
errs = append(errs, fmt.Errorf("Label should not begin with %s. This seem to be a question. Reword your label as a description of the expected flag, `:` are automatically appended.", flag.Label[0:2]))
errs = multierr.Append(errs, fmt.Errorf("Label should not begin with %s. This seem to be a question. Reword your label as a description of the expected flag, `:` are automatically appended.", flag.Label[0:2]))
flag.Label = flag.Label[1:]
}
if flag.Label[len(flag.Label)-1] != ')' && flag.Label[len(flag.Label)-1] != '©' && !unicode.IsLetter(rune(flag.Label[len(flag.Label)-1])) && !unicode.IsDigit(rune(flag.Label[len(flag.Label)-1])) {
errs = append(errs, fmt.Errorf("Label should not end with punct (%q). Reword your label as a description of the expected flag, `:` are automatically appended.", flag.Label[len(flag.Label)-1]))
errs = multierr.Append(errs, fmt.Errorf("Label should not end with punct (%q). Reword your label as a description of the expected flag, `:` are automatically appended.", flag.Label[len(flag.Label)-1]))
}
if strings.HasPrefix(strings.ToLower(raw), "cve-") && flag.Type != "ucq" {
errs = append(errs, fmt.Errorf("CVE numbers are required to be UCQ with choice_cost"))
errs = multierr.Append(errs, fmt.Errorf("CVE numbers are required to be UCQ with choice_cost"))
}
if _, err := strconv.ParseInt(raw, 10, 64); flag.Type == "key" && err == nil && !exceptions.HasException(":not-number-flag") {
errs = append(errs, fmt.Errorf("shouldn't be this flag a number type? (:not-number-flag)"))
errs = multierr.Append(errs, fmt.Errorf("shouldn't be this flag a number type? (:not-number-flag)"))
}
if flag.Placeholder == "" && (strings.HasPrefix(flag.Type, "number") || flag.Type == "key" || flag.Type == "text" || (flag.Type == "ucq" && flag.ChoicesCost > 0)) {
errs = append(errs, fmt.Errorf("no placeholder defined"))
errs = multierr.Append(errs, fmt.Errorf("no placeholder defined"))
}
if strings.HasPrefix(flag.Type, "number") {
min, max, step, err := fic.AnalyzeNumberFlag(flag.Type)
if err != nil {
errs = append(errs, err)
errs = multierr.Append(errs, err)
} else if min == nil || max == nil || step == nil {
errs = append(errs, fmt.Errorf("please define min and max for your number flag"))
errs = multierr.Append(errs, fmt.Errorf("please define min and max for your number flag"))
} else if (*max-*min) / *step <= 10 {
errs = append(errs, fmt.Errorf("to avoid bruteforce, define more than 10 possibilities"))
errs = multierr.Append(errs, fmt.Errorf("to avoid bruteforce, define more than 10 possibilities"))
}
}
return
}
func EPITACheckKeyFlagWithChoices(flag *fic.FlagKey, raw string, choices []*fic.FlagChoice, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs []error) {
func EPITACheckKeyFlagWithChoices(flag *fic.FlagKey, raw string, choices []*fic.FlagChoice, _ *fic.Exercice, exceptions *sync.CheckExceptions) (errs error) {
if !exceptions.HasException(":bruteforcable-choices") {
if len(choices) < 10 && flag.ChoicesCost == 0 {
errs = append(errs, fmt.Errorf("requires at least 10 choices to avoid brute-force"))
errs = multierr.Append(errs, fmt.Errorf("requires at least 10 choices to avoid brute-force"))
} else if len(choices) < 6 && flag.ChoicesCost > 0 {
errs = append(errs, fmt.Errorf("requires at least 10 choices to avoid brute-force"))
errs = multierr.Append(errs, fmt.Errorf("requires at least 10 choices to avoid brute-force"))
}
}