Skip to content

Commit dcefb7c

Browse files
committed
Only allow alphanumeric project names
1 parent 160a613 commit dcefb7c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

cli/input.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"errors"
5+
"regexp"
56

67
"github.com/AlecAivazis/survey/v2"
78
"github.com/AlecAivazis/survey/v2/terminal"
@@ -22,6 +23,29 @@ func Input(question string) (string, error) {
2223
return result, err
2324
}
2425

26+
var alphanumRegex = regexp.MustCompile(`^[a-zA-Z0-9_\-]*$`)
27+
28+
// Same as Input but only allows 'a'-'z', 'A'-'Z', '0'-'9', '_', '-' characters.
29+
func InputAlphanum(question string) (string, error) {
30+
var result string
31+
err := survey.AskOne(&survey.Input{
32+
Message: question,
33+
}, &result, survey.WithValidator(survey.Required), survey.WithValidator(func(val interface{}) error {
34+
text, ok := val.(string)
35+
if !ok {
36+
return errors.New("Value must be a string")
37+
}
38+
if !alphanumRegex.MatchString(text) {
39+
return errors.New("Value must be alphanumeric")
40+
}
41+
return nil
42+
}))
43+
if err == terminal.InterruptErr {
44+
err = ErrCanceled
45+
}
46+
return result, err
47+
}
48+
2549
func YesNo(question string, defaultValue bool) (yes bool, err error) {
2650
err = survey.AskOne(&survey.Confirm{
2751
Message: question,

commands/new.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func New() error {
2929
}
3030
}
3131

32-
projectName, err := cli.Input("Project name:")
32+
projectName, err := cli.InputAlphanum("Project name:")
3333
if err != nil {
3434
return err
3535
}

0 commit comments

Comments
 (0)