-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
119 lines (101 loc) · 2.29 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
package main
import (
"fmt"
"io"
"log"
"os"
"strings"
"github.com/hashicorp/logutils"
"github.com/minamijoyo/tfupdate/command"
"github.com/mitchellh/cli"
"github.com/spf13/afero"
)
// Version is a version number.
var version = "0.8.5"
// UI is a user interface which is a global variable for mocking.
var UI cli.Ui
func init() {
UI = &cli.BasicUi{
Writer: os.Stdout,
}
}
func main() {
log.SetOutput(logOutput())
log.Printf("[INFO] CLI args: %#v", os.Args)
commands := initCommands()
args := os.Args[1:]
c := &cli.CLI{
Name: "tfupdate",
Version: version,
Args: args,
Commands: commands,
HelpWriter: os.Stdout,
Autocomplete: true,
AutocompleteInstall: "install-autocomplete",
AutocompleteUninstall: "uninstall-autocomplete",
}
exitStatus, err := c.Run()
if err != nil {
UI.Error(fmt.Sprintf("Failed to execute CLI: %s", err))
}
os.Exit(exitStatus)
}
func logOutput() io.Writer {
levels := []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
minLevel := os.Getenv("TFUPDATE_LOG")
// default log writer is null device.
writer := io.Discard
if minLevel != "" {
writer = os.Stderr
}
filter := &logutils.LevelFilter{
Levels: levels,
MinLevel: logutils.LogLevel(strings.ToUpper(minLevel)),
Writer: writer,
}
return filter
}
func initCommands() map[string]cli.CommandFactory {
meta := command.Meta{
UI: UI,
Fs: afero.NewOsFs(),
}
commands := map[string]cli.CommandFactory{
"terraform": func() (cli.Command, error) {
return &command.TerraformCommand{
Meta: meta,
}, nil
},
"provider": func() (cli.Command, error) {
return &command.ProviderCommand{
Meta: meta,
}, nil
},
"module": func() (cli.Command, error) {
return &command.ModuleCommand{
Meta: meta,
}, nil
},
"lock": func() (cli.Command, error) {
return &command.LockCommand{
Meta: meta,
}, nil
},
"release": func() (cli.Command, error) {
return &command.ReleaseCommand{
Meta: meta,
}, nil
},
"release latest": func() (cli.Command, error) {
return &command.ReleaseLatestCommand{
Meta: meta,
}, nil
},
"release list": func() (cli.Command, error) {
return &command.ReleaseListCommand{
Meta: meta,
}, nil
},
}
return commands
}