-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathcodetainer.go
144 lines (106 loc) · 3.86 KB
/
codetainer.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
//go:generate go-bindata -pkg=codetainer web/...
package codetainer
import (
"fmt"
"os"
"github.com/codetainerapp/codetainer/mlog"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
const (
// Name application name
Name = "Codetainer"
// Description
Description = ""
// Version application version number
Version = "0.1.0"
)
var (
// Build SHA
Build string
// TimeFormat global time format string
TimeFormat = "15:04:05"
app = kingpin.New(Name, Description)
debug = app.Flag("debug", "Enable debug logging.").Short('v').Bool()
dev = app.Flag("dev", "Enable dev mode.").Bool()
quiet = app.Flag("quiet", "Remove all output logging.").Short('q').Bool()
appSSL = app.Flag("ssl", "Enable SSL (useful outside nginx/apache).").Short('s').Bool()
appConfigPath = app.Flag("config", "Config path (default is ~/.codetainer/config.toml or /etc/codetainer/config.toml)").Short('c').String()
server = app.Command("server", "Start the Codetainer API server.")
profileCommand = app.Command("profile", "Profile commands")
profileListCommand = profileCommand.Command("list", "List profiles")
profileRegisterCommand = profileCommand.Command("register", "Register a profile")
profileRegisterPath = profileRegisterCommand.Arg("path", "Path to load of JSON profile").Required().String()
profileRegisterName = profileRegisterCommand.Arg("name", "name of profile").Required().String()
imageCommand = app.Command("image", "Image commands")
registerCommand = imageCommand.Command("register", "Register an image for use with codetainer")
registerImageId = registerCommand.Arg("image-id", "Docker image id").Required().String()
registerCommandArg = registerCommand.Arg("command", "Default command to use to start container, e.g. /bin/bash").String()
listImagesCommand = imageCommand.Command("list", "List Images")
codetainerCreate = app.Command("create", "Launch a new codetainer")
codetainerCreateImageId = codetainerCreate.Arg("image-id", "Docker image id").Required().String()
codetainerCreateName = codetainerCreate.Arg("name", "Name of container").String()
codetainerRemove = app.Command("remove", "Remove a codetainer")
codetainerRemoveId = codetainerRemove.Arg("id", "id to remove").Required().String()
codetainerList = app.Command("list", "List all codetainers")
// Log Global logger
Log *mlog.Logger
// DevMode Development mode switch. If true
// debug logging and serving assets from disk
// is enabled.
DevMode bool
// TestMode
TestMode bool
)
func initLogger() {
Log = mlog.New()
Log.Prefix = Name
if *debug {
Log.SetLevel(mlog.DebugLevel)
} else {
Log.SetLevel(mlog.InfoLevel)
}
if *dev {
DevMode = true
Log.SetLevel(mlog.DebugLevel)
Log.Info("DEBUG MODE ENABLED.")
} else {
DevMode = false
}
if *quiet {
Log.SetLevel(mlog.FatalLevel)
}
}
func Start() {
app.Version(fmt.Sprintf("Version: %s Build: %s", Version, Build))
args, perr := app.Parse(os.Args[1:])
initLogger()
config, err := NewConfig(*appConfigPath)
if err != nil {
Log.Fatal(err)
} else {
GlobalConfig = *config
}
if !config.TestConfig() {
Log.Fatal("Invalid configuration detected.")
}
switch kingpin.MustParse(args, perr) {
case server.FullCommand():
StartServer()
case registerCommand.FullCommand():
RegisterCodetainerImage(*registerImageId, *registerCommandArg)
case codetainerCreate.FullCommand():
CreateCodetainer(*codetainerCreateImageId, *codetainerCreateName)
case codetainerList.FullCommand():
CodetainerList()
case codetainerRemove.FullCommand():
CodetainerRemove(*codetainerRemoveId)
case listImagesCommand.FullCommand():
ListCodetainerImages()
case profileListCommand.FullCommand():
ListCodetainerProfiles()
case profileRegisterCommand.FullCommand():
RegisterCodetainerProfile(*profileRegisterPath, *profileRegisterName)
default:
app.Usage([]string{})
}
}