From 83d450b1f28c79967e8409b931cfda3f658d5366 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Thu, 5 Oct 2017 15:12:50 +0200 Subject: [PATCH] config: support days (d) and weeks (w) in durations fixes #18 --- cmd/config_job_local.go | 2 +- cmd/config_job_pull.go | 2 +- cmd/config_job_source.go | 2 +- cmd/config_parse.go | 41 ++++++++++++++++++++++++++++++ cmd/config_prune_grid.go | 38 +-------------------------- docs/content/configuration/misc.md | 7 +++++ 6 files changed, 52 insertions(+), 40 deletions(-) diff --git a/cmd/config_job_local.go b/cmd/config_job_local.go index 1a70d81..c95e924 100644 --- a/cmd/config_job_local.go +++ b/cmd/config_job_local.go @@ -49,7 +49,7 @@ func parseLocalJob(c JobParsingContext, name string, i map[string]interface{}) ( return } - if j.Interval, err = time.ParseDuration(asMap.Interval); err != nil { + if j.Interval, err = parsePostitiveDuration(asMap.Interval); err != nil { err = errors.Wrap(err, "cannot parse interval") return } diff --git a/cmd/config_job_pull.go b/cmd/config_job_pull.go index 2afe7ae..e34f13c 100644 --- a/cmd/config_job_pull.go +++ b/cmd/config_job_pull.go @@ -48,7 +48,7 @@ func parsePullJob(c JobParsingContext, name string, i map[string]interface{}) (j return nil, err } - if j.Interval, err = time.ParseDuration(asMap.Interval); err != nil { + if j.Interval, err = parsePostitiveDuration(asMap.Interval); err != nil { err = errors.Wrap(err, "cannot parse 'interval'") return nil, err } diff --git a/cmd/config_job_source.go b/cmd/config_job_source.go index 6628235..e3e2cc1 100644 --- a/cmd/config_job_source.go +++ b/cmd/config_job_source.go @@ -50,7 +50,7 @@ func parseSourceJob(c JobParsingContext, name string, i map[string]interface{}) return } - if j.Interval, err = time.ParseDuration(asMap.Interval); err != nil { + if j.Interval, err = parsePostitiveDuration(asMap.Interval); err != nil { err = errors.Wrap(err, "cannot parse 'interval'") return } diff --git a/cmd/config_parse.go b/cmd/config_parse.go index 75996e8..57842ba 100644 --- a/cmd/config_parse.go +++ b/cmd/config_parse.go @@ -8,6 +8,9 @@ import ( "github.com/mitchellh/mapstructure" "github.com/pkg/errors" "os" + "regexp" + "strconv" + "time" ) var ConfigFileDefaultLocations []string = []string{ @@ -252,3 +255,41 @@ func parseAuthenticatedChannelListenerFactory(c JobParsingContext, v map[string] } } + +var durationStringRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*(s|m|h|d|w)\s*$`) + +func parsePostitiveDuration(e string) (d time.Duration, err error) { + comps := durationStringRegex.FindStringSubmatch(e) + if len(comps) != 3 { + err = fmt.Errorf("does not match regex: %s %#v", e, comps) + return + } + + durationFactor, err := strconv.ParseInt(comps[1], 10, 64) + if err != nil { + return 0, err + } + if durationFactor <= 0 { + return 0, errors.New("duration must be positive integer") + } + + var durationUnit time.Duration + switch comps[2] { + case "s": + durationUnit = time.Second + case "m": + durationUnit = time.Minute + case "h": + durationUnit = time.Hour + case "d": + durationUnit = 24 * time.Hour + case "w": + durationUnit = 24 * 7 * time.Hour + default: + err = fmt.Errorf("contains unknown time unit '%s'", comps[2]) + return + } + + d = time.Duration(durationFactor) * durationUnit + return +} diff --git a/cmd/config_prune_grid.go b/cmd/config_prune_grid.go index 2859cb3..0556e55 100644 --- a/cmd/config_prune_grid.go +++ b/cmd/config_prune_grid.go @@ -93,42 +93,6 @@ func parseGridPrunePolicy(e map[string]interface{}) (p *GridPrunePolicy, err err return } -var durationStringRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*(s|m|h|d|w)\s*$`) - -func parseDuration(e string) (d time.Duration, err error) { - comps := durationStringRegex.FindStringSubmatch(e) - if len(comps) != 3 { - err = fmt.Errorf("does not match regex: %s %#v", e, comps) - return - } - - durationFactor, err := strconv.ParseInt(comps[1], 10, 64) - if err != nil { - return - } - - var durationUnit time.Duration - switch comps[2] { - case "s": - durationUnit = time.Second - case "m": - durationUnit = time.Minute - case "h": - durationUnit = time.Hour - case "d": - durationUnit = 24 * time.Hour - case "w": - durationUnit = 24 * 7 * time.Hour - default: - err = fmt.Errorf("contains unknown time unit '%s'", comps[2]) - return - } - - d = time.Duration(durationFactor) * durationUnit - return - -} - var retentionStringIntervalRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*x\s*([^\(]+)\s*(\((.*)\))?\s*$`) func parseRetentionGridIntervalString(e string) (intervals []util.RetentionInterval, err error) { @@ -146,7 +110,7 @@ func parseRetentionGridIntervalString(e string) (intervals []util.RetentionInter return nil, fmt.Errorf("contains factor <= 0") } - duration, err := parseDuration(comps[2]) + duration, err := parsePostitiveDuration(comps[2]) if err != nil { return nil, err } diff --git a/docs/content/configuration/misc.md b/docs/content/configuration/misc.md index 3c30b54..63322bf 100644 --- a/docs/content/configuration/misc.md +++ b/docs/content/configuration/misc.md @@ -26,6 +26,13 @@ global: sockdir: /var/run/zrepl/stdinserver ``` +## Durations & Intervals + +Interval & duration fields in job definitions, pruning configurations, etc. must match the following regex: +```go +var durationStringRegex *regexp.Regexp = regexp.MustCompile(`^\s*(\d+)\s*(s|m|h|d|w)\s*$`) +// s = second, m = minute, h = hour, d = day, w = week (7 days) +``` ## Super-Verbose Job Debugging