2016-12-22 22:04:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-10 18:39:06 +00:00
|
|
|
"flag"
|
2017-03-03 07:57:26 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-06-19 12:23:39 +00:00
|
|
|
"runtime"
|
2018-01-30 10:54:13 +00:00
|
|
|
"strings"
|
2017-03-03 07:57:26 +00:00
|
|
|
|
2017-02-07 03:33:09 +00:00
|
|
|
"github.com/bcicen/ctop/config"
|
2017-06-14 13:11:40 +00:00
|
|
|
"github.com/bcicen/ctop/connector"
|
2017-06-08 14:51:02 +00:00
|
|
|
"github.com/bcicen/ctop/container"
|
2017-03-06 03:03:05 +00:00
|
|
|
"github.com/bcicen/ctop/cwidgets/compact"
|
2017-02-04 02:01:52 +00:00
|
|
|
"github.com/bcicen/ctop/logging"
|
2017-03-06 03:03:05 +00:00
|
|
|
"github.com/bcicen/ctop/widgets"
|
2017-01-01 22:48:35 +00:00
|
|
|
ui "github.com/gizak/termui"
|
2017-06-08 20:19:34 +00:00
|
|
|
tm "github.com/nsf/termbox-go"
|
2016-12-22 22:04:21 +00:00
|
|
|
)
|
|
|
|
|
2017-03-06 03:03:05 +00:00
|
|
|
var (
|
2017-06-19 12:23:39 +00:00
|
|
|
build = "none"
|
|
|
|
version = "dev-build"
|
|
|
|
goVersion = runtime.Version()
|
2017-03-07 00:13:48 +00:00
|
|
|
|
2019-05-22 16:58:55 +00:00
|
|
|
log *logging.CTopLogger
|
|
|
|
cursor *GridCursor
|
|
|
|
cGrid *compact.CompactGrid
|
|
|
|
header *widgets.CTopHeader
|
|
|
|
status *widgets.StatusLine
|
|
|
|
errView *widgets.ErrorView
|
2017-03-23 06:48:25 +00:00
|
|
|
|
2017-06-19 12:23:39 +00:00
|
|
|
versionStr = fmt.Sprintf("ctop version %v, build %v %v", version, build, goVersion)
|
2017-03-06 03:03:05 +00:00
|
|
|
)
|
2017-02-04 02:01:52 +00:00
|
|
|
|
2016-12-25 23:05:22 +00:00
|
|
|
func main() {
|
2017-03-07 01:04:45 +00:00
|
|
|
defer panicExit()
|
|
|
|
|
2017-03-10 18:39:06 +00:00
|
|
|
// parse command line arguments
|
2017-11-29 03:03:46 +00:00
|
|
|
var (
|
|
|
|
versionFlag = flag.Bool("v", false, "output version information and exit")
|
|
|
|
helpFlag = flag.Bool("h", false, "display this help dialog")
|
|
|
|
filterFlag = flag.String("f", "", "filter containers")
|
|
|
|
activeOnlyFlag = flag.Bool("a", false, "show active containers only")
|
|
|
|
sortFieldFlag = flag.String("s", "", "select container sort field")
|
|
|
|
reverseSortFlag = flag.Bool("r", false, "reverse container sort order")
|
|
|
|
invertFlag = flag.Bool("i", false, "invert default colors")
|
|
|
|
connectorFlag = flag.String("connector", "docker", "container connector to use")
|
|
|
|
)
|
2017-03-10 18:39:06 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2017-03-15 00:06:52 +00:00
|
|
|
if *versionFlag {
|
2017-03-23 06:48:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-03-19 04:59:01 +00:00
|
|
|
// init logger
|
|
|
|
log = logging.Init()
|
|
|
|
|
2018-01-11 18:19:01 +00:00
|
|
|
// init global config and read config file if exists
|
2017-03-19 04:59:01 +00:00
|
|
|
config.Init()
|
2020-10-26 13:04:00 +00:00
|
|
|
if err := config.Read(); err != nil {
|
|
|
|
log.Warningf("reading config: %s", err)
|
|
|
|
}
|
2017-03-19 04:59:01 +00:00
|
|
|
|
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)
|
|
|
|
}
|
2018-05-10 09:44:57 +00:00
|
|
|
tm.SetInputMode(tm.InputAlt)
|
2017-03-14 22:49:11 +00:00
|
|
|
|
2017-06-13 20:25:58 +00:00
|
|
|
defer Shutdown()
|
|
|
|
// init grid, cursor, header
|
2019-05-22 16:58:55 +00:00
|
|
|
cSuper, err := connector.ByName(*connectorFlag)
|
2017-06-14 13:11:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-05-22 16:58:55 +00:00
|
|
|
cursor = &GridCursor{cSuper: cSuper}
|
2017-06-13 20:25:58 +00:00
|
|
|
cGrid = compact.NewCompactGrid()
|
|
|
|
header = widgets.NewCTopHeader()
|
2018-01-11 18:19:01 +00:00
|
|
|
status = widgets.NewStatusLine()
|
2019-05-22 16:58:55 +00:00
|
|
|
errView = widgets.NewErrorView()
|
2017-06-13 20:25:58 +00:00
|
|
|
|
2017-01-01 22:42:13 +00:00
|
|
|
for {
|
2017-03-06 03:03:05 +00:00
|
|
|
exit := Display()
|
2017-01-01 22:42:13 +00:00
|
|
|
if exit {
|
2017-01-01 22:48:35 +00:00
|
|
|
return
|
2017-01-01 22:42:13 +00:00
|
|
|
}
|
2016-12-22 22:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-07 01:04:45 +00:00
|
|
|
|
2017-03-23 06:48:25 +00:00
|
|
|
func Shutdown() {
|
|
|
|
log.Notice("shutting down")
|
|
|
|
log.Exit()
|
2017-06-08 20:19:34 +00:00
|
|
|
if tm.IsInit {
|
|
|
|
ui.Close()
|
|
|
|
}
|
2017-03-23 06:48:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 22:49:11 +00:00
|
|
|
// ensure a given sort field is valid
|
|
|
|
func validSort(s string) {
|
2017-06-08 14:51:02 +00:00
|
|
|
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 {
|
2017-03-23 06:48:25 +00:00
|
|
|
Shutdown()
|
2019-05-22 16:58:55 +00:00
|
|
|
panic(r)
|
2017-06-14 13:11:40 +00:00
|
|
|
fmt.Printf("error: %s\n", r)
|
2017-03-07 01:04:45 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 10:54:13 +00:00
|
|
|
var helpMsg = `ctop - interactive container 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()
|
2018-01-30 10:54:13 +00:00
|
|
|
fmt.Printf("\navailable connectors: ")
|
|
|
|
fmt.Println(strings.Join(connector.Enabled(), ", "))
|
2017-03-07 01:04:45 +00:00
|
|
|
}
|