-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cfg: improve validation of app and task names
Fail if an app or task name contains leading or trailing whitespaces or non-printable characters. Names containing these characters are error-prone and unnecessary. Because app and task names are also used in multiple other commands as arguments, these characters make the CLI harder to use.
- Loading branch information
Showing
3 changed files
with
29 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package validation | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"unicode" | ||
) | ||
|
||
// StrID ensures that id does not contain leading or trailing white spaces | ||
// ([unicode.IsSpace] and only printable characters ([unicode.IsPrint]. | ||
func StrID(id string) error { | ||
for pos, r := range id { | ||
if (pos == 0 || pos == len(id)-1) && unicode.IsSpace(r) { | ||
return errors.New("contains leading or trailing white spaces") | ||
} | ||
|
||
if !unicode.IsPrint(r) { | ||
return fmt.Errorf("contains non-printable character: %x", r) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters