2022-07-11 17:57:33 +00:00
package main
import (
"fmt"
2022-07-12 08:17:38 +00:00
"strings"
2022-07-11 17:57:33 +00:00
"unicode"
"srs.epita.fr/fic-server/libfic"
)
func EPITACheckKeyFlag ( flag * fic . FlagKey , raw string ) ( 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 ] ) )
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 ] ) )
}
2022-07-12 08:17:38 +00:00
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" ) )
}
2022-07-12 10:03:54 +00:00
if flag . Placeholder == "" && ( flag . Type == "text" || ( flag . Type == "ucq" && flag . ChoicesCost > 0 ) ) {
errs = append ( errs , fmt . Errorf ( "no placeholder defined" ) )
}
2022-07-12 08:17:38 +00:00
return
}
func EPITACheckKeyFlagWithChoices ( flag * fic . FlagKey , raw string , choices [ ] * fic . FlagChoice ) ( errs [ ] error ) {
if len ( choices ) < 10 && flag . ChoicesCost == 0 {
errs = 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" ) )
}
2022-07-11 17:57:33 +00:00
return
}