Merge pull request #31 from scriptnull/master

adds more commandline arguments
This commit is contained in:
bradley 2017-03-11 15:01:33 +11:00 committed by GitHub
commit d5ef818c8d

60
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
@ -22,9 +23,27 @@ var (
) )
func main() { func main() {
readArgs()
defer panicExit() defer panicExit()
// parse command line arguments
var versionFlag = flag.Bool("v", false, "output version information and exit")
var helpFlag = flag.Bool("h", false, "display this help dialog")
var filterFlag = flag.String("f", "", "filter containers")
var activeOnlyFlag = flag.Bool("a", false, "show active containers only")
var sortFieldFlag = flag.String("s", "", "select container sort field")
var reverseSortFlag = flag.Bool("r", false, "reverse container sort order")
flag.Parse()
if *versionFlag == true {
printVersion()
os.Exit(0)
}
if *helpFlag == true {
printHelp()
os.Exit(0)
}
// init ui // init ui
ui.ColorMap = ColorMap // override default colormap ui.ColorMap = ColorMap // override default colormap
if err := ui.Init(); err != nil { if err := ui.Init(); err != nil {
@ -35,6 +54,23 @@ func main() {
// init global config // init global config
config.Init() config.Init()
// override default config values with command line flags
if *filterFlag != "" {
config.Update("filterStr", *filterFlag)
}
if *activeOnlyFlag == true {
config.Toggle("allContainers")
}
if *sortFieldFlag != "" {
config.Update("sortField", *sortFieldFlag)
}
if *reverseSortFlag == true {
config.Toggle("sortReversed")
}
// init logger // init logger
log = logging.Init() log = logging.Init()
if config.GetSwitchVal("loggingEnabled") { if config.GetSwitchVal("loggingEnabled") {
@ -56,25 +92,6 @@ 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() { func panicExit() {
if r := recover(); r != nil { if r := recover(); r != nil {
ui.Clear() ui.Clear()
@ -88,12 +105,11 @@ var helpMsg = `ctop - container metric viewer
usage: ctop [options] usage: ctop [options]
options: options:
-h display this help dialog
-v output version information and exit
` `
func printHelp() { func printHelp() {
fmt.Println(helpMsg) fmt.Println(helpMsg)
flag.PrintDefaults()
} }
func printVersion() { func printVersion() {