-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (63 loc) · 1.59 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/ryancragun/terraform-plan-editor/internal/edit"
)
var config = &edit.Config{}
func init() {
flag.StringVar(&config.TextEditorCmd, "editor", "", "the editor to use when editing text files")
flag.StringVar(&config.BinEditorCmd, "bin-editor", "", "the editor to use when editing binary files")
}
func getEditorCmd(cmd string) (string, error) {
if cmd != "" {
fmt.Println("text editor: " + cmd)
return cmd, nil
}
if editor := os.Getenv("EDITOR"); editor != "" {
fmt.Println("editor: " + editor)
return editor, nil
}
return "", fmt.Errorf("you must set the editor with the '-editor' flag or set $EDITOR")
}
func getBinEditorCmd(cmd string) (string, error) {
if cmd != "" {
fmt.Println("binary editor: " + cmd)
return cmd, nil
}
if editor := os.Getenv("EDITOR"); editor != "" {
fmt.Println("binary editor: " + editor)
return editor, nil
}
return "", fmt.Errorf("you must set the editor with the '-bin-editor' flag or set $EDITOR")
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) != 2 {
panic("terraform-plan-editor: <flags> <source-plan-path> <dest-plan-path>")
}
var err error
config.PlanPath, err = filepath.Abs(args[0])
if err != nil {
panic(err)
}
config.DstPath, err = filepath.Abs(args[1])
if err != nil {
panic(err)
}
config.TextEditorCmd, err = getEditorCmd(config.TextEditorCmd)
if err != nil {
panic(err)
}
config.BinEditorCmd, err = getBinEditorCmd(config.BinEditorCmd)
if err != nil {
panic(err)
}
err = edit.New(config).Edit()
if err != nil {
panic(err)
}
}