From b747de9ca10e170059bea322e2157940f35398cc Mon Sep 17 00:00:00 2001 From: Zhoulin <443619637@qq.com> Date: Wed, 16 Aug 2023 10:03:22 +0800 Subject: [PATCH 1/2] The unit of the time interval parameter can identify the number of days --- .gitignore | 1 + cmd/flags.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/.gitignore b/.gitignore index 88aa9df399b1..cff77a1e4b5a 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ cmd/cmd .hypothesis __pycache__ /node_modules +/vendor diff --git a/cmd/flags.go b/cmd/flags.go index 019b2a3ea800..480b0c575828 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -19,8 +19,10 @@ package cmd import ( "os" "path" + "regexp" "runtime" "strconv" + "strings" "time" "github.com/urfave/cli/v2" @@ -323,7 +325,51 @@ func expandFlags(compoundFlags ...[]cli.Flag) []cli.Flag { return flags } +func parseDuration(input string) (time.Duration, error) { + re := regexp.MustCompile(`(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?`) + matches := re.FindStringSubmatch(input) + if len(matches) == 5 { + var days, hours, minutes, seconds int + var err error + if matches[1] != "" { + days, err = strconv.Atoi(matches[1]) + if err != nil { + return 0, err + } + } + if matches[2] != "" { + hours, err = strconv.Atoi(matches[2]) + if err != nil { + return 0, err + } + } + if matches[3] != "" { + minutes, err = strconv.Atoi(matches[3]) + if err != nil { + return 0, err + } + } + if matches[4] != "" { + seconds, err = strconv.Atoi(matches[4]) + if err != nil { + return 0, err + } + } + durationDays := time.Duration(days*24*int(time.Hour) + hours*int(time.Hour) + minutes*int(time.Minute) + seconds*int(time.Second)) + return durationDays, nil + } else { + logger.Warnf("Invalid input format of cache expire ") + return 0, nil + } +} + func duration(s string) time.Duration { + //param is *d + if strings.Contains(s, "d") { + if newDuration, err := parseDuration(s); err == nil { + return newDuration + } + } if v, err := strconv.ParseInt(s, 10, 64); err == nil { return time.Second * time.Duration(v) } From d34cea64f6cf586222896cc8e8fad9da1772b051 Mon Sep 17 00:00:00 2001 From: Sandy Xu Date: Mon, 11 Dec 2023 15:46:31 +0800 Subject: [PATCH 2/2] handle number of days --- .gitignore | 1 - cmd/flags.go | 64 +++++++++++++--------------------------------------- 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index cff77a1e4b5a..88aa9df399b1 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,3 @@ cmd/cmd .hypothesis __pycache__ /node_modules -/vendor diff --git a/cmd/flags.go b/cmd/flags.go index a5c46ff8c30a..959443e34135 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -19,7 +19,6 @@ package cmd import ( "os" "path" - "regexp" "runtime" "strconv" "strings" @@ -334,56 +333,25 @@ func expandFlags(compoundFlags ...[]cli.Flag) []cli.Flag { return flags } -func parseDuration(input string) (time.Duration, error) { - re := regexp.MustCompile(`(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?`) - matches := re.FindStringSubmatch(input) - if len(matches) == 5 { - var days, hours, minutes, seconds int - var err error - if matches[1] != "" { - days, err = strconv.Atoi(matches[1]) - if err != nil { - return 0, err - } - } - if matches[2] != "" { - hours, err = strconv.Atoi(matches[2]) - if err != nil { - return 0, err - } - } - if matches[3] != "" { - minutes, err = strconv.Atoi(matches[3]) - if err != nil { - return 0, err - } - } - if matches[4] != "" { - seconds, err = strconv.Atoi(matches[4]) - if err != nil { - return 0, err - } - } - durationDays := time.Duration(days*24*int(time.Hour) + hours*int(time.Hour) + minutes*int(time.Minute) + seconds*int(time.Second)) - return durationDays, nil - } else { - logger.Warnf("Invalid input format of cache expire ") - return 0, nil +func duration(s string) time.Duration { + v, err := strconv.Atoi(s) + if err == nil { + return time.Second * time.Duration(v) } -} -func duration(s string) time.Duration { - //param is *d - if strings.Contains(s, "d") { - if newDuration, err := parseDuration(s); err == nil { - return newDuration - } + err = nil + var d time.Duration + p := strings.Index(s, "d") + if p >= 0 { + v, err = strconv.Atoi(s[:p]) } - if v, err := strconv.ParseInt(s, 10, 64); err == nil { - return time.Second * time.Duration(v) + if err == nil { + d, err = time.ParseDuration(s[p+1:]) } - if v, err := time.ParseDuration(s); err == nil { - return v + + if err != nil { + logger.Warnf("Invalid duration value: %s, setting it to 0", s) + return 0 } - return 0 + return d + time.Hour*time.Duration(v*24) }