ctop/main.go

150 lines
2.9 KiB
Go
Raw Normal View History

package main
import (
2017-03-10 18:39:06 +00:00
"flag"
2017-03-03 07:57:26 +00:00
"fmt"
"os"
2017-06-09 17:35:29 +00:00
"strings"
2017-03-03 07:57:26 +00:00
2017-02-07 03:33:09 +00:00
"github.com/bcicen/ctop/config"
"github.com/bcicen/ctop/container"
"github.com/bcicen/ctop/cwidgets/compact"
"github.com/bcicen/ctop/logging"
"github.com/bcicen/ctop/widgets"
ui "github.com/gizak/termui"
tm "github.com/nsf/termbox-go"
)
var (
2017-03-07 01:04:45 +00:00
build = "none"
2017-03-07 00:13:48 +00:00
version = "dev-build"
log *logging.CTopLogger
cursor *GridCursor
2017-03-07 01:04:45 +00:00
cGrid *compact.CompactGrid
header *widgets.CTopHeader
versionStr = fmt.Sprintf("ctop version %v, build %v", version, build)
)
func main() {
2017-03-07 01:04:45 +00:00
defer panicExit()
2017-03-10 18:39:06 +00:00
// 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")
2017-03-15 00:02:46 +00:00
var invertFlag = flag.Bool("i", false, "invert default colors")
2017-06-09 17:35:29 +00:00
var connectorFlag = flag.String("connector", "docker", "container connector to use")
2017-03-10 18:39:06 +00:00
flag.Parse()
2017-06-09 17:35:29 +00:00
validConnector(*connectorFlag)
2017-03-15 00:06:52 +00:00
if *versionFlag {
fmt.Println(versionStr)
2017-03-10 18:39:06 +00:00
os.Exit(0)
}
2017-03-15 00:06:52 +00:00
if *helpFlag {
2017-03-10 18:39:06 +00:00
printHelp()
os.Exit(0)
}
// init logger
log = logging.Init()
// init global config
config.Init()
2017-03-10 18:39:06 +00:00
// override default config values with command line flags
if *filterFlag != "" {
config.Update("filterStr", *filterFlag)
}
2017-03-15 00:06:52 +00:00
if *activeOnlyFlag {
2017-03-10 18:39:06 +00:00
config.Toggle("allContainers")
}
if *sortFieldFlag != "" {
2017-03-14 22:49:11 +00:00
validSort(*sortFieldFlag)
2017-03-10 18:39:06 +00:00
config.Update("sortField", *sortFieldFlag)
}
2017-03-15 00:06:52 +00:00
if *reverseSortFlag {
2017-03-10 18:39:06 +00:00
config.Toggle("sortReversed")
}
2017-03-14 22:49:11 +00:00
// init ui
2017-03-15 00:06:52 +00:00
if *invertFlag {
2017-03-15 00:02:46 +00:00
InvertColorMap()
}
2017-03-14 22:49:11 +00:00
ui.ColorMap = ColorMap // override default colormap
if err := ui.Init(); err != nil {
panic(err)
}
2017-06-13 20:25:58 +00:00
defer Shutdown()
// init grid, cursor, header
cursor = NewGridCursor(*connectorFlag)
cGrid = compact.NewCompactGrid()
header = widgets.NewCTopHeader()
for {
exit := Display()
if exit {
return
}
}
}
2017-03-07 01:04:45 +00:00
func Shutdown() {
log.Notice("shutting down")
log.Exit()
if tm.IsInit {
ui.Close()
}
}
2017-06-09 17:35:29 +00:00
func validConnector(s string) {
if _, ok := enabledConnectors[s]; !ok {
2017-06-09 21:35:28 +00:00
fmt.Printf("invalid connector type: %s\n", s)
2017-06-09 17:35:29 +00:00
var connectors []string
for k, _ := range enabledConnectors {
connectors = append(connectors, k)
}
2017-06-09 21:11:59 +00:00
fmt.Printf("connector must be one of: %s\n", strings.Join(connectors, ","))
2017-06-09 17:35:29 +00:00
os.Exit(1)
}
}
2017-03-14 22:49:11 +00:00
// ensure a given sort field is valid
func validSort(s string) {
if _, ok := container.Sorters[s]; !ok {
2017-03-14 22:49:11 +00:00
fmt.Printf("invalid sort field: %s\n", s)
os.Exit(1)
}
}
2017-03-07 01:04:45 +00:00
func panicExit() {
if r := recover(); r != nil {
Shutdown()
2017-03-07 01:04:45 +00:00
fmt.Printf("panic: %s\n", r)
os.Exit(1)
}
}
2017-03-08 23:40:35 +00:00
var helpMsg = `ctop - container metric viewer
2017-03-07 01:04:45 +00:00
usage: ctop [options]
options:
`
func printHelp() {
fmt.Println(helpMsg)
2017-03-10 18:39:06 +00:00
flag.PrintDefaults()
2017-03-07 01:04:45 +00:00
}