-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
153 lines (130 loc) · 3.44 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/hashicorp/hc-install/fs"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/magodo/tfadd/tfadd"
"github.com/mitchellh/cli"
)
type arrayFlag []string
func (l *arrayFlag) String() string {
return "array flag"
}
func (l *arrayFlag) Set(value string) error {
*l = append(*l, value)
return nil
}
func defaultFlagSet(name string) *flag.FlagSet {
f := flag.NewFlagSet(name, flag.ContinueOnError)
f.SetOutput(ioutil.Discard)
f.Usage = func() {}
return f
}
type initCommand struct{}
func (s *initCommand) Help() string {
helpText := `
Usage: tfadd [global options] init [options] [providers]
Generate Terraform setting that pins the provider versions to standard output.
`
return strings.TrimSpace(helpText)
}
func (s *initCommand) Run(args []string) int {
b, err := tfadd.Init(args)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
if len(b) == 0 {
return 0
}
fmt.Fprintln(os.Stdout, string(b))
return 0
}
func (s *initCommand) Synopsis() string {
return "Setup the Terraform setting"
}
type stateCommand struct{}
func (r *stateCommand) Help() string {
helpText := `
Usage: tfadd [global options] state [options]
Generates resource template from Terraform state to standard output.
Options:
-full Output all non-computed properties in the generated config
-mask-sensitive Mask sensitive properties
-target=addr Only generate for the specified resource (can specify multiple times)
-chdir Change the current working directory
`
return strings.TrimSpace(helpText)
}
func (r *stateCommand) Run(args []string) int {
fset := defaultFlagSet("state")
flagFull := fset.Bool("full", false, "Whether to generate all non-computed properties")
flagChdir := fset.String("chdir", ".", "Change the current working directory")
flagMaskSensitive := fset.Bool("mask-sensitive", false, "Whether to mask sensitive properties")
var flagTargets arrayFlag
fset.Var(&flagTargets, "target", "Only generate for the specified resource")
if err := fset.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
ctx := context.TODO()
av := fs.AnyVersion{
Product: &product.Terraform,
}
execPath, err := av.Find(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
tf, err := tfexec.NewTerraform(*flagChdir, execPath)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
opts := []tfadd.OptionSetter{
tfadd.Full(*flagFull),
tfadd.MaskSenstitive(*flagMaskSensitive),
}
var template []byte
if len(flagTargets) == 0 {
b, err := tfadd.State(ctx, tf, opts...)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
template = b
} else {
bs, err := tfadd.StateForTargets(ctx, tf, flagTargets, opts...)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
return 1
}
template = bytes.Join(bs, nil)
}
fmt.Println(string(template))
return 0
}
func (r *stateCommand) Synopsis() string {
return "Generate Terraform configuration"
}
func main() {
c := cli.NewCLI("tfadd", "dev")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
"state": func() (cli.Command, error) { return &stateCommand{}, nil },
"init": func() (cli.Command, error) { return &initCommand{}, nil },
}
exitStatus, err := c.Run()
if err != nil {
log.Println(err)
}
os.Exit(exitStatus)
}