-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiguration.go
72 lines (61 loc) · 2.09 KB
/
configuration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"cloud.google.com/go/civil"
"flag"
"fmt"
"os"
"regexp"
"time"
)
type Configuration struct {
Profile string
Command string
Interval time.Duration
HasPreferredRunTime bool
PreferredRunTime civil.Time
RunNow bool
}
func ParseConfigFromFlags() Configuration {
configuration := Configuration{}
flag.StringVar(&configuration.Profile, "profile", "default", "Profile to use.")
flag.StringVar(&configuration.Command, "command",
"echo 'daily-runner has run echo printing this text'",
"The command that runner will execute")
flag.DurationVar(&configuration.Interval, "interval",
4*time.Minute,
"The interval that daily-runner will use to check if it needs to run. "+
"Can accept values acceptable to golang time.ParseDuration function")
flag.BoolVar(&configuration.RunNow, "now",
false,
"Run the command immediately. Previous runs are not taken into account. "+
"This option also disables process locking, so multiple processes can run at the same time.")
preferedRunTimePtr := flag.String("preferredTime", "",
"Set a preferred time for the runner to run command. This time overrides the daily logic and the command will always "+
"run if the system is up at that time.")
flag.Parse()
isValid, message := validateProfile(configuration.Profile)
if !isValid {
fmt.Print(message)
os.Exit(2)
}
if *preferedRunTimePtr != "" {
parsedCivilTime, err2 := civil.ParseTime(*preferedRunTimePtr)
if err2 != nil {
fmt.Printf("Parsing PreferredRunTime flag failed with error: %v", err2)
os.Exit(2)
}
configuration.HasPreferredRunTime = true
configuration.PreferredRunTime = parsedCivilTime
} else {
configuration.HasPreferredRunTime = false
}
return configuration
}
func validateProfile(profile string) (bool, string) {
matched, _ := regexp.MatchString(`^[0-9a-zA-Z.\-_]*$`, profile)
if !matched {
return false, fmt.Sprintf("Wrong profile name argument provided: '%v'. Please provide a name containing only latin "+
"letters, numbers and the characters '.-_'. Exiting", profile)
}
return true, ""
}