From 2b68a2e384edcf5a41c54e8a13c6ecdafd1fc85a Mon Sep 17 00:00:00 2001 From: xanonid Date: Fri, 7 Aug 2020 10:48:32 +0200 Subject: [PATCH] Add delay flag to prevent hitting rate limits with remote state Set a default delay of 2s, which is working for remote state with the google terraform provider --- main.go | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 4065d2e..521b98b 100644 --- a/main.go +++ b/main.go @@ -2,35 +2,48 @@ package main import ( + "fmt" "github.com/urfave/cli/v2" "log" "os" "reflect" + "time" ) var ( version = "dev" ) +type config struct { + delay time.Duration +} + func main() { app := &cli.App{ - Name: "terraform-state-mover", - Usage: "refactoring Terraform code has never been easier", - Authors: []*cli.Author{{Name: "Maximilian Bode", Email: "maxbode@gmail.com"}}, - Action: action, - UsageText: "terraform-state-mover [-- ]", - Version: version, + Name: "terraform-state-mover", + Usage: "refactoring Terraform code has never been easier", + Authors: []*cli.Author{{Name: "Maximilian Bode", Email: "maxbode@gmail.com"}}, + Action: action, + Flags: []cli.Flag{ + &cli.DurationFlag{ + Name: "delay", Aliases: []string{"d"}, + Usage: "Delay between terraform state mv calls. Helps to avoid rate-limits.", + Value: time.Second * 2, + }, + }, + Version: version, } if err := app.Run(os.Args); err != nil { log.Fatal(err) } } -func action(c *cli.Context) error { +func action(ctx *cli.Context) error { var args []string if len(os.Args) >= 2 && os.Args[1] == "--" { args = os.Args[2:] } + cfg := readConfig(ctx) changes, err := changes(args) if err != nil { @@ -53,6 +66,9 @@ func action(c *cli.Context) error { delete(dests, dest) } + if len(moves) == 0 { + fmt.Println("Nothing to do.") + } for src, dest := range moves { if err := move(src, dest); err != nil { return err @@ -60,3 +76,13 @@ func action(c *cli.Context) error { } return nil } + +func readConfig(ctx *cli.Context) config { + return config{ + delay: ctx.Duration("delay"), + } +} + +func wait(cfg config) { + time.Sleep(cfg.delay) +}