forked from Azure/aztfexport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (129 loc) · 3.71 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
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"flag"
"github.com/Azure/aztfy/internal/config"
"github.com/Azure/aztfy/internal/meta"
"github.com/Azure/aztfy/internal/ui"
"github.com/meowgorithm/babyenv"
)
var (
flagVersion *bool
flagOutputDir *string
flagMappingFile *string
flagContinue *bool
flagBatchMode *bool
flagPattern *string
flagOverwrite *bool
)
func init() {
flagVersion = flag.Bool("v", false, "Print version")
flagOutputDir = flag.String("o", "", "Specify output dir. Default is a dir under the user cache dir, which is named after the resource group name")
flagMappingFile = flag.String("m", "", "Specify the resource mapping file")
flagContinue = flag.Bool("k", false, "Whether continue on import error (batch mode only)")
flagBatchMode = flag.Bool("b", false, "Batch mode (i.e. Non-interactive mode)")
flagPattern = flag.String("p", "res-", `The pattern of the resource name. The resource name is generated by taking the pattern and adding an auto-incremental integer to the end. If pattern includes a "*", the auto-incremental integer replaces the last "*".`)
flagOverwrite = flag.Bool("f", false, "Whether to overwrite the out dir if it is not empty")
}
const usage = `aztfy [option] <resource group name>
`
func fatal(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "%s\n", usage)
flag.PrintDefaults()
}
flag.Parse()
if *flagVersion {
fmt.Println(version)
os.Exit(0)
}
// Flag sanity check
if len(flag.Args()) != 1 {
flag.Usage()
os.Exit(1)
}
if *flagBatchMode && *flagMappingFile == "" {
fatal(errors.New("`-q` must be used together with `-m`"))
}
if *flagContinue && !*flagBatchMode {
fatal(errors.New("`-k` must be used together with `-q`"))
}
rg := flag.Args()[0]
// Initialize the config
var cfg config.Config
if err := babyenv.Parse(&cfg); err != nil {
fatal(err)
}
cfg.ResourceGroupName = rg
cfg.ResourceNamePattern = *flagPattern
cfg.ResourceMappingFile = *flagMappingFile
cfg.OutputDir = *flagOutputDir
cfg.Overwrite = *flagOverwrite
cfg.BatchMode = *flagBatchMode
if cfg.BatchMode {
if err := batchImport(cfg, *flagContinue); err != nil {
fatal(err)
}
return
}
prog, err := ui.NewProgram(cfg)
if err != nil {
fatal(err)
}
if err := prog.Start(); err != nil {
fatal(err)
}
}
func batchImport(cfg config.Config, continueOnError bool) error {
// Discard logs from hashicorp/azure-go-helper
log.SetOutput(io.Discard)
// Define another dedicated logger for the ui
logger := log.New(os.Stderr, "", log.LstdFlags)
if cfg.Logfile != "" {
f, err := os.OpenFile(cfg.Logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return err
}
logger = log.New(f, "aztfy", log.LstdFlags)
}
logger.Println("New meta")
c, err := meta.NewMeta(cfg)
if err != nil {
return err
}
logger.Println("Initialize")
if err := c.Init(); err != nil {
return err
}
logger.Println("List resources")
list := c.ListResource()
logger.Println("Import resources")
for i := range list {
if list[i].Skip() {
logger.Printf("[WARN] No mapping information for resource: %s, skip it\n", list[i].ResourceID)
continue
}
logger.Printf("Importing %s as %s\n", list[i].ResourceID, list[i].TFAddr())
c.Import(&list[i])
if err := list[i].ImportError; err != nil {
msg := fmt.Sprintf("Failed to import %s as %s: %v", list[i].ResourceID, list[i].TFAddr(), err)
if !continueOnError {
return fmt.Errorf(msg)
}
logger.Println("[ERROR] " + msg)
}
}
logger.Println("Generate Terraform configurations")
if err := c.GenerateCfg(list); err != nil {
return fmt.Errorf("generating Terraform configuration: %v", err)
}
return nil
}