Skip to content

Commit

Permalink
Merge branch 'master' into feat/968/maintenance-listing
Browse files Browse the repository at this point in the history
  • Loading branch information
SCedricThomas authored Jul 13, 2023
2 parents 3dd4cb2 + f20a49a commit 53985c7
Show file tree
Hide file tree
Showing 65 changed files with 16,258 additions and 3,380 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* chore(term): remove `github.com/andrew-d/go-termutil`, use standard library instead
* feat(cmd): addon can be retrieve from addon type, not only UUID
* feat: add database maintenance listing with the new `database-maintenance-list` command
* feat(addons): add maintenance windows manipulation with the new `addon-config` command

### 1.29.1

Expand Down
86 changes: 86 additions & 0 deletions addons/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package addons

import (
"context"
"fmt"
"strings"
"time"

"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/utils"
"github.com/Scalingo/go-scalingo/v6"
"github.com/Scalingo/go-utils/errors/v2"
)

type UpdateAddonConfigOpts struct {
MaintenanceWindowDay *string
MaintenanceWindowHour *int
}

func UpdateConfig(ctx context.Context, app, addon string, options UpdateAddonConfigOpts) error {
weekdayNameToWeekdayOrderMap := map[string]time.Weekday{
strings.ToLower(time.Sunday.String()): time.Sunday,
strings.ToLower(time.Monday.String()): time.Monday,
strings.ToLower(time.Tuesday.String()): time.Tuesday,
strings.ToLower(time.Wednesday.String()): time.Wednesday,
strings.ToLower(time.Thursday.String()): time.Thursday,
strings.ToLower(time.Friday.String()): time.Friday,
strings.ToLower(time.Saturday.String()): time.Saturday,
}

c, err := config.ScalingoClient(ctx)
if err != nil {
return errors.Notef(ctx, err, "get Scalingo client to update addon config")
}

// If addon does not contain a UUID, we consider it contains an addon type (e.g. MongoDB)
if !strings.HasPrefix(addon, "ad-") {
addon, err = utils.GetAddonUUIDFromType(ctx, app, addon)
if err != nil {
return errors.Notef(ctx, err, "fail to get the addon UUID based on its type")
}
}

// fetching the current database maintenance window allows
// to only overload the specified options
db, err := c.DatabaseShow(ctx, app, addon)
if err != nil {
return errors.Notef(ctx, err, "get database information")
}

weekdayLocal, startingHourLocal := utils.ConvertDayAndHourToTimezone(
time.Weekday(db.MaintenanceWindow.WeekdayUTC),
db.MaintenanceWindow.StartingHourUTC,
time.UTC,
time.Local,
)

if options.MaintenanceWindowDay != nil {
day, ok := weekdayNameToWeekdayOrderMap[strings.ToLower(*options.MaintenanceWindowDay)]
if !ok {
return errors.Notef(ctx, err, "invalid weekday '%s'", *options.MaintenanceWindowDay)
}
weekdayLocal = day
}

if options.MaintenanceWindowHour != nil {
if *options.MaintenanceWindowHour < 0 || *options.MaintenanceWindowHour > 23 {
return errors.Notef(ctx, err, "invalid starting hour '%d': it must be between 0 and 23", *options.MaintenanceWindowHour)
}
startingHourLocal = *options.MaintenanceWindowHour
}

weekdayUTC, startingHourUTC := utils.ConvertDayAndHourToTimezone(weekdayLocal, startingHourLocal, time.Local, time.UTC)

_, err = c.DatabaseUpdateMaintenanceWindow(ctx, app, addon, scalingo.MaintenanceWindowParams{
WeekdayUTC: utils.IntPtr(int(weekdayUTC)),
StartingHourUTC: utils.IntPtr(startingHourUTC),
})
if err != nil {
return errors.Notef(ctx, err, "update the database maintenance window")
}

fmt.Println("Addon config updated.")

return nil
}
3 changes: 3 additions & 0 deletions addons/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/olekukonko/tablewriter"
"gopkg.in/errgo.v1"

"github.com/Scalingo/cli/config"
"github.com/Scalingo/cli/utils"
)

// Info is the command handler displaying static information about one given addon
Expand Down Expand Up @@ -48,6 +50,7 @@ func Info(ctx context.Context, app, addon string) error {
t.Append([]string{"Plan", fmt.Sprintf("%v", addonInfo.Plan.Name)})
t.Append([]string{"Force TLS", forceSsl})
t.Append([]string{"Internet Accessibility", internetAccess})
t.Append([]string{"Maintenance window", utils.FormatMaintenanceWindowWithTimezone(dbInfo.MaintenanceWindow, time.Local)})

t.Render()

Expand Down
51 changes: 51 additions & 0 deletions cmd/addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Scalingo/cli/cmd/autocomplete"
"github.com/Scalingo/cli/detect"
"github.com/Scalingo/cli/utils"
"github.com/Scalingo/go-utils/errors/v2"
)

var (
Expand Down Expand Up @@ -175,4 +176,54 @@ var (
autocomplete.CmdFlagsAutoComplete(c, "addons-info")
},
}
addonsConfigCommand = cli.Command{
Name: "addons-config",
Category: "Addons",
Usage: "Configure an add-on attached to your app",
Flags: []cli.Flag{&appFlag,
&cli.StringFlag{Name: "maintenance-window-hour", Usage: "Configure add-on maintenance window starting hour (in your local timezone)"},
&cli.StringFlag{Name: "maintenance-window-day", Usage: "Configure add-on maintenance window day"},
},
ArgsUsage: "addon-id",
Description: CommandDescription{
Description: "Configure an add-on attached to your app",
Examples: []string{"scalingo --app my-app addons-config --maintenance-window-day tuesday --maintenance-window-hour 2 addon_uuid"},
SeeAlso: []string{"addons", "addons-info"},
}.Render(),
Action: func(c *cli.Context) error {
if c.Args().Len() != 1 {
return cli.ShowCommandHelp(c, "addons-config")
}

ctx := c.Context
currentApp := detect.CurrentApp(c)
currentAddon := c.Args().First()
config := addons.UpdateAddonConfigOpts{}

if c.IsSet("maintenance-window-day") {
config.MaintenanceWindowDay = utils.StringPtr(c.String("maintenance-window-day"))
}

if c.IsSet("maintenance-window-hour") {
config.MaintenanceWindowHour = utils.IntPtr(c.Int("maintenance-window-hour"))
}

if config.MaintenanceWindowHour == nil && config.MaintenanceWindowDay == nil {
errorQuitWithHelpMessage(errors.New(ctx, "cannot update your addon without a specified option"), c, "addons-config")
}

err := addons.UpdateConfig(ctx, currentApp, currentAddon, config)
if err != nil {
errorQuit(err)
}

return nil
},
BashComplete: func(c *cli.Context) {
err := autocomplete.CmdFlagsAutoComplete(c, "addons-config")
if err != nil {
errorQuit(err)
}
},
}
)
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ var (
&addonsRemoveCommand,
&addonsUpgradeCommand,
&addonsInfoCommand,
&addonsConfigCommand,

// Integration Link
&integrationLinkShowCommand,
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/Scalingo/go-scalingo/v6 v6.6.0
github.com/Scalingo/go-scalingo/v6 v6.7.1
github.com/Scalingo/go-utils/errors/v2 v2.3.0
github.com/Scalingo/go-utils/logger v1.2.0
github.com/Scalingo/go-utils/retry v1.1.1
Expand All @@ -21,12 +21,12 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.4
github.com/stvp/rollbar v0.5.1
github.com/urfave/cli/v2 v2.24.2
golang.org/x/crypto v0.10.0
golang.org/x/term v0.9.0
golang.org/x/text v0.10.0
golang.org/x/crypto v0.11.0
golang.org/x/term v0.10.0
golang.org/x/text v0.11.0
gopkg.in/errgo.v1 v1.0.1
)

Expand Down Expand Up @@ -63,8 +63,8 @@ require (
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/tools v0.9.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
32 changes: 14 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63n
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903 h1:ZK3C5DtzV2nVAQTx5S5jQvMeDqWtD1By5mOoyY/xJek=
github.com/ProtonMail/go-crypto v0.0.0-20230518184743-7afd39499903/go.mod h1:8TI4H3IbrackdNgv+92dI+rhpCaLqM0IfpgCgenFvRE=
github.com/Scalingo/go-scalingo/v6 v6.6.0 h1:w1spshwyJDaudgC7mhC7xp5wtBt1hYFoBTXMR/iempQ=
github.com/Scalingo/go-scalingo/v6 v6.6.0/go.mod h1:u3ROWLWw78ug4EQaunzOBzSPKy4ukNQTDrCFaKMG938=
github.com/Scalingo/go-scalingo/v6 v6.7.1 h1:Jn5uQVXrrig+tDCfCX3BhcA+zjqHjFvHgprQdo3aeBQ=
github.com/Scalingo/go-scalingo/v6 v6.7.1/go.mod h1:h58MZqmLQauBsyFWL8UkQAOdmX4jRTz4jc9vsDr8bTA=
github.com/Scalingo/go-utils/errors/v2 v2.3.0 h1:9Ju4EmxXWoUsm0LOEWyEYUfT503l78YeQx0r6i1MdBI=
github.com/Scalingo/go-utils/errors/v2 v2.3.0/go.mod h1:ovQmOjKaifysELQaMU21SAy8nqhZQ3xW/xrj0Ed9lWo=
github.com/Scalingo/go-utils/logger v1.2.0 h1:E3jtaoRxpIsFcZu/jsvWew8ttUAwKUYQufdPqGYp7EU=
Expand Down Expand Up @@ -131,16 +131,12 @@ github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVs
github.com/skeema/knownhosts v1.1.1 h1:MTk78x9FPgDFVFkDLTrsnnfCJl7g1C/nnKvePgrIngE=
github.com/skeema/knownhosts v1.1.1/go.mod h1:g4fPeYpque7P0xefxtGzV81ihjC8sX2IqpAoNkjxbMo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stvp/rollbar v0.5.1 h1:qvyWbd0RNL5V27MBumqCXlcU7ohmHeEtKX+Czc8oeuw=
github.com/stvp/rollbar v0.5.1/go.mod h1:/fyFC854GgkbHRz/rSsiYc6h84o0G5hxBezoQqRK7Ho=
github.com/urfave/cli/v2 v2.24.2 h1:q1VA+ofZ8SWfEKB9xXHUD4QZaeI9e+ItEqSbfH2JBXk=
Expand All @@ -156,8 +152,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
Expand All @@ -171,8 +167,8 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -198,23 +194,23 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
Expand Down
17 changes: 17 additions & 0 deletions utils/pointers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

func Int64Ptr(r int64) *int64 {
return &r
}

func IntPtr(r int) *int {
return &r
}

func Float64Ptr(r float64) *float64 {
return &r
}

func StringPtr(r string) *string {
return &r
}
52 changes: 52 additions & 0 deletions utils/time.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,57 @@
package utils

import (
"fmt"
"time"

"github.com/Scalingo/go-scalingo/v6"
)

const (
TimeFormat = "2006/01/02 15:04:05"
)

func FormatMaintenanceWindowWithTimezone(maintenanceWindow scalingo.MaintenanceWindow, location *time.Location) string {
weekdayUTC := time.Weekday(maintenanceWindow.WeekdayUTC)
weekdayLocal, startingHourLocal := ConvertDayAndHourToTimezone(
weekdayUTC,
maintenanceWindow.StartingHourUTC,
time.UTC,
location,
)

return fmt.Sprintf("%ss at %02d:00 (%02d hours)",
weekdayLocal.String(),
startingHourLocal,
maintenanceWindow.DurationInHour,
)
}

func ConvertDayAndHourToTimezone(weekday time.Weekday, hour int, inputLocation *time.Location, outputLocation *time.Location) (time.Weekday, int) {
newTimezoneDate := beginningOfWeek(time.Now().In(inputLocation))

newTimezoneDate = newTimezoneDate.AddDate(0, 0, int(weekday)-1)
newTimezoneDate = newTimezoneDate.Add(time.Duration(hour) * time.Hour)

newTimezoneDate = newTimezoneDate.In(outputLocation)

return newTimezoneDate.Weekday(), newTimezoneDate.Hour()
}

func beginningOfWeek(t time.Time) time.Time {
t = beginningOfDay(t)
weekday := int(t.Weekday())

weekStartDayInt := int(time.Monday)

if weekday < weekStartDayInt {
weekday = weekday + 7 - weekStartDayInt
} else {
weekday = weekday - weekStartDayInt
}
return t.AddDate(0, 0, -weekday)
}

func beginningOfDay(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
Loading

0 comments on commit 53985c7

Please sign in to comment.