From 25b9074ee38b6c0b306d92ab07b42360aaefcf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Fri, 17 Nov 2023 15:44:04 -0300 Subject: [PATCH] Display cron for humans --- cmd/plugin/rpaasv2/cmd/autoscale.go | 11 ++++++-- cmd/plugin/rpaasv2/cmd/autoscale_test.go | 36 ++++++++++++------------ go.mod | 1 + go.sum | 2 ++ 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/cmd/plugin/rpaasv2/cmd/autoscale.go b/cmd/plugin/rpaasv2/cmd/autoscale.go index 62944e322..4d2673c7b 100644 --- a/cmd/plugin/rpaasv2/cmd/autoscale.go +++ b/cmd/plugin/rpaasv2/cmd/autoscale.go @@ -10,6 +10,7 @@ import ( "io" "strings" + "github.com/lnquy/cron" "github.com/olekukonko/tablewriter" "github.com/urfave/cli/v2" @@ -250,7 +251,7 @@ func writeAutoscale(w io.Writer, autoscale *autogenerated.Autoscale) { } var schedules strings.Builder - + exprDesc, _ := cron.NewDescriptor() for i, s := range autoscale.Schedules { if i > 0 { fmt.Fprintln(&schedules) @@ -259,8 +260,12 @@ func writeAutoscale(w io.Writer, autoscale *autogenerated.Autoscale) { fmt.Fprintf(&schedules, "Window %d:\n", i+1) fmt.Fprintln(&schedules, " Min replicas:", s.MinReplicas) - fmt.Fprintf(&schedules, " Start: %q\n", s.Start) - fmt.Fprintf(&schedules, " End: %q", s.End) + + human, _ := exprDesc.ToDescription(s.Start, cron.Locale_en) + fmt.Fprintf(&schedules, " Start: %s (%s)\n", human, s.Start) + + human, _ = exprDesc.ToDescription(s.Start, cron.Locale_en) + fmt.Fprintf(&schedules, " End: %s (%s)", human, s.End) } if text := schedules.String(); text != "" { diff --git a/cmd/plugin/rpaasv2/cmd/autoscale_test.go b/cmd/plugin/rpaasv2/cmd/autoscale_test.go index ed9665e09..e1e5ed2e4 100644 --- a/cmd/plugin/rpaasv2/cmd/autoscale_test.go +++ b/cmd/plugin/rpaasv2/cmd/autoscale_test.go @@ -76,24 +76,24 @@ max replicas: 5 }), expected: `min replicas: 0 max replicas: 100 -+-------------+--------------------------+ -| Triggers | trigger details | -+-------------+--------------------------+ -| Schedule(s) | Window 1: | -| | Min replicas: 1 | -| | Start: "00 08 * * 1-5" | -| | End: "00 20 * * 1-5" | -| | | -| | Window 2: | -| | Min replicas: 5 | -| | Start: "00 20 * * 2" | -| | End: "00 01 * * 3" | -| | | -| | Window 3: | -| | Min replicas: 5 | -| | Start: "00 22 * * 0" | -| | End: "00 02 * * 1" | -+-------------+--------------------------+ ++-------------+-------------------------------------------------------------+ +| Triggers | trigger details | ++-------------+-------------------------------------------------------------+ +| Schedule(s) | Window 1: | +| | Min replicas: 1 | +| | Start: At 08:00 AM, Monday through Friday (00 08 * * 1-5) | +| | End: At 08:00 AM, Monday through Friday (00 20 * * 1-5) | +| | | +| | Window 2: | +| | Min replicas: 5 | +| | Start: At 08:00 PM, only on Tuesday (00 20 * * 2) | +| | End: At 08:00 PM, only on Tuesday (00 01 * * 3) | +| | | +| | Window 3: | +| | Min replicas: 5 | +| | Start: At 10:00 PM, only on Sunday (00 22 * * 0) | +| | End: At 10:00 PM, only on Sunday (00 02 * * 1) | ++-------------+-------------------------------------------------------------+ `, }, diff --git a/go.mod b/go.mod index 179d14cfe..97607d45d 100644 --- a/go.mod +++ b/go.mod @@ -93,6 +93,7 @@ require ( github.com/json-iterator/go v1.1.12 // indirect github.com/labstack/gommon v0.4.0 // indirect github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect + github.com/lnquy/cron v1.1.1 // indirect github.com/magiconair/properties v1.8.5 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect diff --git a/go.sum b/go.sum index 05cc2a666..7f2fba4e5 100644 --- a/go.sum +++ b/go.sum @@ -505,6 +505,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0= github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/lithammer/dedent v1.1.0/go.mod h1:jrXYCQtgg0nJiN+StA2KgR7w6CiQNv9Fd/Z9BP0jIOc= +github.com/lnquy/cron v1.1.1 h1:iaDX1ublgQ9LBhA8l9BVU+FrTE1PPSPAuvAdhgdnXgA= +github.com/lnquy/cron v1.1.1/go.mod h1:hu2Y7H68/8oKk6T4+K4qdbopbnaP4rGltK3ylWiiDss= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=