(flag string)
| 297 | ) |
| 298 | |
| 299 | func parseScheduleAtFlag(flag string) (int, *time.Location, error) { |
| 300 | scheduleAtHour, err := strconv.Atoi(flag) |
| 301 | if err == nil { |
| 302 | // In this case, the schedule-at flag equals a single number |
| 303 | return scheduleAtHour, time.Local, nil |
| 304 | } |
| 305 | |
| 306 | validationError := errors.NewValidationErrorsBuilder() |
| 307 | |
| 308 | // From now on the schedule-at flag is a number and a timezone such as |
| 309 | // "3 Europe/Paris" |
| 310 | s := strings.Split(flag, " ") |
| 311 | if len(s) < 2 { |
| 312 | validationError = validationError.Set("schedule-at", "only contains two space-separated fields") |
| 313 | return -1, nil, validationError.Build() |
| 314 | } |
| 315 | scheduleAtTime := s[0] |
| 316 | scheduleAtTimezone := s[1] |
| 317 | |
| 318 | // If client writes "4:00" or "4h00", we only want to keep "4" in scheduleAtTime |
| 319 | re := regexp.MustCompile(`([:hH].*)|[0]+`) |
| 320 | scheduleAtTime = re.ReplaceAllString(scheduleAtTime, "") |
| 321 | |
| 322 | scheduleAtHour, err = strconv.Atoi(scheduleAtTime) |
| 323 | if err != nil { |
| 324 | validationError = validationError.Set("schedule-at", "first field must be a digit representing the hour") |
| 325 | return -1, nil, validationError.Build() |
| 326 | } |
| 327 | |
| 328 | loc, err := time.LoadLocation(scheduleAtTimezone) |
| 329 | if err != nil { |
| 330 | validationError = validationError.Set("schedule-at", "unknown timezone"+s[1]) |
| 331 | return -1, nil, validationError.Build() |
| 332 | } |
| 333 | |
| 334 | return scheduleAtHour, loc, nil |
| 335 | } |
no outgoing calls