Skip to content

Commit d07fb41

Browse files
committed
Fix duplicate code issue in event.go
Fixes #117 Signed-off-by: Matt Stratton <matt.stratton@gmail.com>
1 parent 7cd6997 commit d07fb41

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

event/event.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,9 @@ var qsCreateEvent = []*survey.Question{
117117
Validate: func(val interface{}) error {
118118
str, _ := val.(string)
119119
if str != "" {
120-
if _, err := os.Stat(str); err != nil {
121-
return errors.New("File not found.")
122-
}
123-
ret, _ := regexp.MatchString(`[0-9a-z]+\.(png|PNG)`, str)
124-
if ret != true {
125-
return errors.New("Logo image must be a PNG file")
120+
err := checkPNG(str)
121+
if err != nil {
122+
return err
126123
}
127124
}
128125

@@ -138,12 +135,9 @@ var qsCreateEvent = []*survey.Question{
138135
Validate: func(val interface{}) error {
139136
str, _ := val.(string)
140137
if str != "" {
141-
if _, err := os.Stat(str); err != nil {
142-
return errors.New("File not found.")
143-
}
144-
ret, _ := regexp.MatchString(`[0-9a-z]+\.(png|PNG)`, str)
145-
if ret != true {
146-
return errors.New("Logo image must be a PNG file")
138+
err := checkPNG(str)
139+
if err != nil {
140+
return err
147141
}
148142
}
149143

@@ -152,6 +146,19 @@ var qsCreateEvent = []*survey.Question{
152146
},
153147
}
154148

149+
// checkPNG ensures that an image file exists and is a PNG
150+
func checkPNG(imagePath string) error {
151+
if _, err := os.Stat(imagePath); err != nil {
152+
return errors.New("File not found.")
153+
}
154+
ret, _ := regexp.MatchString(`[0-9a-z]+\.(png|PNG)`, imagePath)
155+
if ret != true {
156+
return errors.New("Logo image must be a PNG file")
157+
}
158+
159+
return nil
160+
}
161+
155162
// CreateEvent takes input from the user to create a new event
156163
func CreateEvent(city, year string) (err error) {
157164

0 commit comments

Comments
 (0)