libfic/flag: add validatorRegexp field

This commit is contained in:
nemunaire 2018-11-16 20:46:19 +01:00 committed by Pierre-Olivier Mercier
parent c2558fe0ec
commit ff56ec9fe3
8 changed files with 111 additions and 35 deletions

View file

@ -17,6 +17,7 @@ Tous les textes doivent utiliser l'encodage UTF8.
- `[[flag]]` : drapeau classique à valider pour résoudre le challenge :
* `label = "Intitulé"` : (facultatif, par défaut : `Flag`) intitulé du drapeau ;
* `raw = 'MieH2athxuPhai6u'` : drapeau exact à trouver ;
* `validator_regexp = "^(?:sudo +)?(.*)$"` : (facultatif) expression rationnelle dont les groupes capturés serviront comme chaîne à valider (notez que `?:` au début d'un groupe ne le capturera pas) ;
* `ignorecase = true` : (facultatif, par défaut : `false`) ignore la case de ce drapeau ;
* `help = "Indication"` : (facultatif) chaîne de caractères placée sous le champ du formulaire, idéale pour donner une indication de format ;
* `[[flag.unlock_file]]` : bloque l'accès à un fichier tant que le flag n'est pas obtenu :
@ -29,6 +30,7 @@ Tous les textes doivent utiliser l'encodage UTF8.
- `[[flag_ucq]]` : drapeau sous forme de question à choix unique :
* `label = "Intitulé du groupe"` : (facultatif) intitulé du groupe de choix ;
* `raw = 'MieH2athxuPhai6u'` : drapeau attendu parmi les propositions ;
* `validator_regexp = "^(?:sudo +)?(.*)$"` : (facultatif) expression rationnelle dont les groupes capturés serviront comme chaîne à valider (notez que `?:` au début d'un groupe ne le capturera pas) ;
* `help = "Indication"` : (facultatif, uniquement si `displayAs = select`) chaîne de caractères placée sous le champ du formulaire ;
* `displayAs = "select|radio"` : (facultatif, par défaut `radio`) manière dont est affichée le choix : `select` pour une liste de choix, `radio` pour des boutons radios ;
* `choices_cost = 20` : (facultatif, par défaut `0`) coût pour afficher les choix, avant l'affichage, se comporte comme un `flag` classique (à 0, les choix sont affichés directement) ;

View file

@ -27,11 +27,12 @@ type ExerciceUnlockFile struct {
// ExerciceFlag holds informations about a "classic" flag.
type ExerciceFlag struct {
Label string `toml:",omitempty"`
Raw string
IgnoreCase bool `toml:",omitempty"`
Help string `toml:",omitempty"`
LockedFile []ExerciceUnlockFile `toml:"unlock_file,omitempty"`
Label string `toml:",omitempty"`
Raw string
IgnoreCase bool `toml:",omitempty"`
ValidatorRe string `toml:"validator_regexp,omitempty"`
Help string `toml:",omitempty"`
LockedFile []ExerciceUnlockFile `toml:"unlock_file,omitempty"`
}
// ExerciceFlagMCQChoice holds a choice for an MCQ flag.
@ -57,6 +58,7 @@ type ExerciceFlagUCQ struct {
Label string `toml:",omitempty"`
Raw string
IgnoreCase bool `toml:",omitempty"`
ValidatorRe string `toml:"validator_regexp,omitempty"`
Help string `toml:",omitempty"`
DisplayAs string `toml:",omitempty"`
Choices_Cost int64 `toml:",omitempty"`

View file

@ -19,6 +19,15 @@ func isFullGraphic(s string) bool {
return true
}
func validatorRegexp(vre string) (validator_regexp *string) {
if len(vre) > 0 {
validator_regexp = &vre
} else {
validator_regexp = nil
}
return
}
// SyncExerciceFlags reads the content of challenge.txt and import "classic" flags as Key for the given challenge.
func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) {
if _, err := exercice.WipeFlags(); err != nil {
@ -40,7 +49,7 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) {
errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
}
if k, err := exercice.AddRawFlag(flag.Label, flag.Help, flag.IgnoreCase, flag.Raw); err != nil {
if k, err := exercice.AddRawFlag(flag.Label, flag.Help, flag.IgnoreCase, validatorRegexp(flag.ValidatorRe), []byte(flag.Raw)); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag #%d: %s", path.Base(exercice.Path), nline + 1, err))
continue
} else {
@ -67,7 +76,7 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) {
errs = append(errs, fmt.Sprintf("%q: WARNING flag UCQ #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
}
if k, err := exercice.AddRawFlag(flag.Label, flag.Help, flag.IgnoreCase, flag.Raw); err != nil {
if k, err := exercice.AddRawFlag(flag.Label, flag.Help, flag.IgnoreCase, validatorRegexp(flag.ValidatorRe), []byte(flag.Raw)); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag UCQ #%d: %s", path.Base(exercice.Path), nline + 1, err))
continue
} else {