-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
93 lines (76 loc) · 2.36 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
package main
import (
"flag"
"github.com/hashicorp/logutils"
"github.com/webdevwilson/tfwatch/context"
"log"
"os"
"path"
"strings"
)
func main() {
cfg := ParseArgs(os.Args[1:])
ctx := context.NewContext(cfg)
go ctx.Server.Start()
// loop
for {
}
}
func ParseArgs(args []string) *context.Configuration {
var checkoutDir, logDir, logLevel, siteDir, stateDir string
var port uint
var clearState, help, noPlanRuns, verbose bool
flags := flag.NewFlagSet("tfwatch", flag.ExitOnError)
flags.BoolVar(&clearState, "clear-state", false, "Remove all state before starting")
flags.BoolVar(&help, "h", false, "")
flags.BoolVar(&help, "help", false, "Display usage information")
flags.StringVar(&logDir, "log-dir", "", "Directory the logs will be placed in")
flags.StringVar(&logLevel, "log-level", envOr("LOG_LEVEL", "INFO"), "Log level. One of DEBUG, INFO, WARN, ERROR")
flags.BoolVar(&noPlanRuns, "no-plans", false, "Prevents tfwatch from updating the plans")
flags.UintVar(&port, "port", 3000, "Defines port HTTP server will bind to")
flags.StringVar(&siteDir, "site-dir", envOr("SITE_DIR", "site"), "Directory site is served from")
flags.StringVar(&stateDir, "state-dir", envOr("STATE_DIR", ""), "Directory where state is stored")
flags.BoolVar(&verbose, "v", false, "")
flags.BoolVar(&verbose, "verbose", false, "Configure max logging")
//flag.Usage = usage
flags.Parse(os.Args[1:])
// print helpful usage information
if help {
flags.Usage()
os.Exit(0)
}
if verbose {
logLevel = "DEBUG"
}
// ensure we have a checkout directory, this is the only required option
if checkoutDir = flags.Arg(0); checkoutDir == "" {
log.Printf("[ERROR] No directory specified!")
flags.Usage()
os.Exit(1)
}
// set defaults that use checkout directory
if stateDir == "" {
stateDir = path.Join(checkoutDir, ".tfwatch")
}
if logDir == "" {
logDir = path.Join(stateDir, "logs")
}
logLevel = strings.ToUpper(logLevel)
return &context.Configuration{
CheckoutDir: checkoutDir,
ClearState: clearState,
LogDir: logDir,
LogLevel: logutils.LogLevel(logLevel),
Port: uint16(port),
RunPlan: !noPlanRuns,
SiteDir: siteDir,
StateDir: stateDir,
}
}
// envOr returns the environment variable or the default values
func envOr(name string, defaultVal string) (v string) {
if v = os.Getenv(name); len(v) == 0 {
v = defaultVal
}
return
}