add version, help printing to main()

This commit is contained in:
Bradley Cicenas 2017-03-07 01:04:45 +00:00
parent d40b765dbb
commit aaf98a77dc

74
main.go
View File

@ -12,24 +12,26 @@ import (
)
var (
build = ""
build = "none"
version = "dev-build"
log *logging.CTopLogger
cursor *GridCursor
cGrid = compact.NewCompactGrid()
header = widgets.NewCTopHeader()
cGrid *compact.CompactGrid
header *widgets.CTopHeader
)
func main() {
defer func() {
if r := recover(); r != nil {
ui.Clear()
fmt.Printf("panic: %s\n", r)
os.Exit(1)
}
}()
readArgs()
defer panicExit()
// init ui
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()
// init global config
config.Init()
// init logger
@ -38,12 +40,10 @@ func main() {
logging.StartServer()
}
// init ui, grid
if err := ui.Init(); err != nil {
panic(err)
}
defer ui.Close()
// init grid, cursor, header
cursor = NewGridCursor()
cGrid = compact.NewCompactGrid()
header = widgets.NewCTopHeader()
for {
exit := Display()
@ -54,3 +54,47 @@ func main() {
}
}
}
func readArgs() {
if len(os.Args) < 2 {
return
}
for _, arg := range os.Args[1:] {
switch arg {
case "-v", "version":
printVersion()
os.Exit(0)
case "-h", "help":
printHelp()
os.Exit(0)
default:
fmt.Printf("invalid option or argument: \"%s\"\n", arg)
os.Exit(1)
}
}
}
func panicExit() {
if r := recover(); r != nil {
ui.Clear()
fmt.Printf("panic: %s\n", r)
os.Exit(1)
}
}
var helpMsg = `cTop - container metric viewer
usage: ctop [options]
options:
-h display this help dialog
-v output version information and exit
`
func printHelp() {
fmt.Println(helpMsg)
}
func printVersion() {
fmt.Printf("cTop version %v, build %v\n", version, build)
}