-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
122 lines (100 loc) · 2.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"time"
"github.com/hashicorp/logutils"
backendInit "github.com/hashicorp/terraform/backend/init"
"github.com/hashicorp/terraform/command"
"github.com/hashicorp/terraform/command/arguments"
"github.com/hashicorp/terraform/command/clistate"
"github.com/hashicorp/terraform/command/views"
"github.com/mitchellh/cli"
)
// Version is a version number.
var version = "0.0.3"
// LockCommand is a Command implementation that lock a Terraform state.
type LockCommand struct {
command.StateMeta
}
// Run runs the procedure of this command.
func (c *LockCommand) Run(args []string) int {
// Read the from state
stateFromMgr, err := c.State()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error loading the state: %s", err))
return 1
}
view := views.NewStateLocker(arguments.ViewHuman, c.View)
stateLocker := clistate.NewLocker((0 * time.Second), view)
if err := stateLocker.Lock(stateFromMgr, "tflock"); err != nil {
c.Ui.Error(fmt.Sprintf("Error locking source state: %s", err))
return 1
}
return 0
}
// Help returns long-form help text.
func (*LockCommand) Help() string {
helpText := `
Usage: tflock
`
return strings.TrimSpace(helpText)
}
// Synopsis returns one-line help text.
func (c *LockCommand) Synopsis() string {
return "Lock your Terraform state"
}
func logOutput() io.Writer {
levels := []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
minLevel := os.Getenv("TFLOCK_LOG")
// default log writer is null device.
writer := ioutil.Discard
if minLevel != "" {
writer = os.Stderr
}
filter := &logutils.LevelFilter{
Levels: levels,
MinLevel: logutils.LogLevel(minLevel),
Writer: writer,
}
return filter
}
func main() {
log.SetOutput(logOutput())
// Initialize the backends.
// This is needed for registering backend types such as s3.
backendInit.Init(nil)
UI := &cli.BasicUi{
Writer: os.Stdout,
Reader: os.Stdin,
}
meta := command.Meta{
Ui: UI,
}
commands := map[string]cli.CommandFactory{
"": func() (cli.Command, error) {
return &LockCommand{
StateMeta: command.StateMeta{
Meta: meta,
},
}, nil
},
}
args := os.Args[1:]
c := &cli.CLI{
Name: "tflock",
Version: version,
Args: args,
Commands: commands,
HelpWriter: os.Stdout,
}
exitStatus, err := c.Run()
if err != nil {
UI.Error(fmt.Sprintf("Failed to execute CLI: %s", err))
}
os.Exit(exitStatus)
}