/
oti.go
78 lines (65 loc) · 1.73 KB
/
oti.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
// Copyright 2014, Bryan Matsuo. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// oti.go [created: Thu, 20 Mar 2014]
package main
import (
"github.com/bmatsuo/oti/oticonfig"
"github.com/bmatsuo/oti/otisub"
"flag"
"log"
"os"
)
var DEBUG bool
var OTIVersion = "0.1"
var OTIAgent = "oti"
// default configuration informatio
var ConfigPath = "oti.json"
var Config = &oticonfig.C{
AwsKeyPath: "aws_credentials.json",
Ec2: oticonfig.Ec2{
TagPrefix: "co.bmats.oti.",
},
}
var Log = log.New(os.Stderr, "", 0)
func main() {
fs := flag.NewFlagSet("oti", flag.ExitOnError)
fs.BoolVar(&DEBUG, "debug", false, "debug logging output")
fs.StringVar(&ConfigPath, "c", ConfigPath, "config file location")
fs.Usage = func() {
Log.Println("usage: oti [options] command")
Log.Println()
Log.Println("options:")
fs.PrintDefaults()
Log.Println()
Log.Println("commands:")
for _, cmd := range otisub.GetAll() {
Log.Print("\t", cmd.Name())
}
Log.Println()
Log.Println("for details about a specific command:")
Log.Println("\toti command -h")
}
fs.Parse(os.Args[1:])
cmdname, args := getargs(fs.Args(), "lifecycle")
cmd := otisub.Get(cmdname)
if cmd == nil {
Log.Printf("no command %q; exiting", cmdname)
Log.Fatal("for a list of commands run oti -h")
}
err := oticonfig.Read(ConfigPath, Config)
if err != nil {
if os.IsNotExist(err) {
Log.Println("warning: config file not found. using defaults.")
} else {
Log.Fatal("error reading config: ", err)
}
}
cmd.Main(args)
}
func getargs(args []string, defcmd string, defargs ...string) (subcmd string, subargs []string) {
if len(args) == 0 {
return defcmd, defargs
}
return args[0], args[1:]
}