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] 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) }