1// statelocker use used for testing command with a locked state. 2// This will lock the state file at a given path, then wait for a sigal. On 3// SIGINT and SIGTERM the state will be Unlocked before exit. 4package main 5 6import ( 7 "io" 8 "log" 9 "os" 10 "os/signal" 11 "syscall" 12 "time" 13 14 "github.com/hashicorp/terraform/internal/command/clistate" 15 "github.com/hashicorp/terraform/internal/states/statemgr" 16) 17 18func main() { 19 if len(os.Args) != 2 { 20 log.Fatal(os.Args[0], "statefile") 21 } 22 23 s := &clistate.LocalState{ 24 Path: os.Args[1], 25 } 26 27 info := statemgr.NewLockInfo() 28 info.Operation = "test" 29 info.Info = "state locker" 30 31 lockID, err := s.Lock(info) 32 if err != nil { 33 io.WriteString(os.Stderr, err.Error()) 34 return 35 } 36 37 // signal to the caller that we're locked 38 io.WriteString(os.Stdout, "LOCKID "+lockID) 39 40 defer func() { 41 if err := s.Unlock(lockID); err != nil { 42 io.WriteString(os.Stderr, err.Error()) 43 } 44 }() 45 46 c := make(chan os.Signal, 1) 47 signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) 48 49 // timeout after 10 second in case we don't get cleaned up by the test 50 select { 51 case <-time.After(10 * time.Second): 52 case <-c: 53 } 54} 55