Skip to content

Commit

Permalink
Add delay flag to prevent hitting rate limits with remote state
Browse files Browse the repository at this point in the history
Set a default delay of 2s, which is working for remote state with the
google terraform provider
  • Loading branch information
xanonid authored and mbode committed Oct 4, 2020
1 parent 4752829 commit 2b68a2e
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 [-- <terraform args>]",
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 {
Expand All @@ -53,10 +66,23 @@ 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
}
}
return nil
}

func readConfig(ctx *cli.Context) config {
return config{
delay: ctx.Duration("delay"),
}
}

func wait(cfg config) {
time.Sleep(cfg.delay)
}

0 comments on commit 2b68a2e

Please sign in to comment.