From b85ca680f0e87e23b443cc2ab86e971fb9f6ed38 Mon Sep 17 00:00:00 2001 From: Bradley Cicenas Date: Thu, 8 Jun 2017 14:51:02 +0000 Subject: [PATCH] restructure container,connectors in subpackage --- dockersource.go => connector/dockersource.go | 26 ++++++++++++-------- mocksource.go => connector/mocksource.go | 11 +++++---- container.go => container/container.go | 11 ++++++--- sort.go => container/sort.go | 8 +++--- cursor.go | 14 ++++++----- debug.go | 3 ++- grid.go | 3 ++- main.go | 3 ++- menus.go | 3 ++- 9 files changed, 50 insertions(+), 32 deletions(-) rename dockersource.go => connector/dockersource.go (85%) rename mocksource.go => connector/mocksource.go (89%) rename container.go => container/container.go (88%) rename sort.go => container/sort.go (97%) diff --git a/dockersource.go b/connector/dockersource.go similarity index 85% rename from dockersource.go rename to connector/dockersource.go index eff699c..a0ffc04 100644 --- a/dockersource.go +++ b/connector/dockersource.go @@ -1,4 +1,4 @@ -package main +package connector import ( "fmt" @@ -6,18 +6,24 @@ import ( "strings" "sync" + "github.com/bcicen/ctop/container" + "github.com/bcicen/ctop/logging" "github.com/bcicen/ctop/metrics" "github.com/fsouza/go-dockerclient" ) +var ( + log = logging.Init() +) + type ContainerSource interface { - All() Containers - Get(string) (*Container, bool) + All() container.Containers + Get(string) (*container.Container, bool) } type DockerContainerSource struct { client *docker.Client - containers map[string]*Container + containers map[string]*container.Container needsRefresh chan string // container IDs requiring refresh lock sync.RWMutex } @@ -30,7 +36,7 @@ func NewDockerContainerSource() *DockerContainerSource { } cm := &DockerContainerSource{ client: client, - containers: make(map[string]*Container), + containers: make(map[string]*container.Container), needsRefresh: make(chan string, 60), lock: sync.RWMutex{}, } @@ -79,7 +85,7 @@ func portsFormat(ports map[docker.Port][]docker.PortBinding) string { return strings.Join(append(exposed, published...), "\n") } -func (cm *DockerContainerSource) refresh(c *Container) { +func (cm *DockerContainerSource) refresh(c *container.Container) { insp := cm.inspect(c.Id) // remove container if no longer exists if insp == nil { @@ -127,14 +133,14 @@ func (cm *DockerContainerSource) Loop() { } // Get a single container, creating one anew if not existing -func (cm *DockerContainerSource) MustGet(id string) *Container { +func (cm *DockerContainerSource) MustGet(id string) *container.Container { c, ok := cm.Get(id) // append container struct for new containers if !ok { // create collector collector := metrics.NewDocker(cm.client, id) // create container - c = NewContainer(id, collector) + c = container.New(id, collector) cm.lock.Lock() cm.containers[id] = c cm.lock.Unlock() @@ -143,7 +149,7 @@ func (cm *DockerContainerSource) MustGet(id string) *Container { } // Get a single container, by ID -func (cm *DockerContainerSource) Get(id string) (*Container, bool) { +func (cm *DockerContainerSource) Get(id string) (*container.Container, bool) { cm.lock.Lock() c, ok := cm.containers[id] cm.lock.Unlock() @@ -159,7 +165,7 @@ func (cm *DockerContainerSource) delByID(id string) { } // Return array of all containers, sorted by field -func (cm *DockerContainerSource) All() (containers Containers) { +func (cm *DockerContainerSource) All() (containers container.Containers) { cm.lock.Lock() for _, c := range cm.containers { containers = append(containers, c) diff --git a/mocksource.go b/connector/mocksource.go similarity index 89% rename from mocksource.go rename to connector/mocksource.go index 309cf40..499a40b 100644 --- a/mocksource.go +++ b/connector/mocksource.go @@ -1,6 +1,6 @@ // +build !release -package main +package connector import ( "math/rand" @@ -8,13 +8,14 @@ import ( "strings" "time" + "github.com/bcicen/ctop/container" "github.com/bcicen/ctop/metrics" "github.com/jgautheron/codename-generator" "github.com/nu7hatch/gouuid" ) type MockContainerSource struct { - containers Containers + containers container.Containers } func NewMockContainerSource() *MockContainerSource { @@ -40,7 +41,7 @@ func (cs *MockContainerSource) Init() { func (cs *MockContainerSource) makeContainer(aggression int64) { collector := metrics.NewMock(aggression) - c := NewContainer(makeID(), collector) + c := container.New(makeID(), collector) c.SetMeta("name", makeName()) c.SetState(makeState()) cs.containers = append(cs.containers, c) @@ -60,7 +61,7 @@ func (cs *MockContainerSource) Loop() { } // Get a single container, by ID -func (cs *MockContainerSource) Get(id string) (*Container, bool) { +func (cs *MockContainerSource) Get(id string) (*container.Container, bool) { for _, c := range cs.containers { if c.Id == id { return c, true @@ -70,7 +71,7 @@ func (cs *MockContainerSource) Get(id string) (*Container, bool) { } // Return array of all containers, sorted by field -func (cs *MockContainerSource) All() Containers { +func (cs *MockContainerSource) All() container.Containers { sort.Sort(cs.containers) cs.containers.Filter() return cs.containers diff --git a/container.go b/container/container.go similarity index 88% rename from container.go rename to container/container.go index 68cdbc2..38d0a66 100644 --- a/container.go +++ b/container/container.go @@ -1,23 +1,28 @@ -package main +package container import ( "github.com/bcicen/ctop/cwidgets" "github.com/bcicen/ctop/cwidgets/compact" + "github.com/bcicen/ctop/logging" "github.com/bcicen/ctop/metrics" ) +var ( + log = logging.Init() +) + // Metrics and metadata representing a container type Container struct { metrics.Metrics Id string Meta map[string]string Widgets *compact.Compact + Display bool // display this container in compact view updater cwidgets.WidgetUpdater collector metrics.Collector - display bool // display this container in compact view } -func NewContainer(id string, collector metrics.Collector) *Container { +func New(id string, collector metrics.Collector) *Container { widgets := compact.NewCompact(id) return &Container{ Metrics: metrics.NewMetrics(), diff --git a/sort.go b/container/sort.go similarity index 97% rename from sort.go rename to container/sort.go index 660ae05..6142043 100644 --- a/sort.go +++ b/container/sort.go @@ -1,4 +1,4 @@ -package main +package container import ( "fmt" @@ -104,14 +104,14 @@ func (a Containers) Filter() { re := regexp.MustCompile(fmt.Sprintf(".*%s", filter)) for _, c := range a { - c.display = true + c.Display = true // Apply name filter if re.FindAllString(c.GetMeta("name"), 1) == nil { - c.display = false + c.Display = false } // Apply state filter if !config.GetSwitchVal("allContainers") && c.GetMeta("state") != "running" { - c.display = false + c.Display = false } } } diff --git a/cursor.go b/cursor.go index 31a46a6..d9ed74d 100644 --- a/cursor.go +++ b/cursor.go @@ -3,24 +3,26 @@ package main import ( "math" + "github.com/bcicen/ctop/connector" + "github.com/bcicen/ctop/container" ui "github.com/gizak/termui" ) type GridCursor struct { selectedID string // id of currently selected container - filtered Containers - cSource ContainerSource + filtered container.Containers + cSource connector.ContainerSource } func NewGridCursor() *GridCursor { return &GridCursor{ - cSource: NewDockerContainerSource(), + cSource: connector.NewDockerContainerSource(), } } func (gc *GridCursor) Len() int { return len(gc.filtered) } -func (gc *GridCursor) Selected() *Container { +func (gc *GridCursor) Selected() *container.Container { idx := gc.Idx() if idx < gc.Len() { return gc.filtered[idx] @@ -33,10 +35,10 @@ func (gc *GridCursor) RefreshContainers() (lenChanged bool) { oldLen := gc.Len() // Containers filtered by display bool - gc.filtered = Containers{} + gc.filtered = container.Containers{} var cursorVisible bool for _, c := range gc.cSource.All() { - if c.display { + if c.Display { if c.Id == gc.selectedID { cursorVisible = true } diff --git a/debug.go b/debug.go index e3a6ab3..bbcab06 100644 --- a/debug.go +++ b/debug.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" + "github.com/bcicen/ctop/container" ui "github.com/gizak/termui" ) @@ -19,7 +20,7 @@ func logEvent(e ui.Event) { } // log container, metrics, and widget state -func dumpContainer(c *Container) { +func dumpContainer(c *container.Container) { msg := fmt.Sprintf("logging state for container: %s\n", c.Id) for k, v := range c.Meta { msg += fmt.Sprintf("Meta.%s = %s\n", k, v) diff --git a/grid.go b/grid.go index dab8943..e8d379f 100644 --- a/grid.go +++ b/grid.go @@ -2,6 +2,7 @@ package main import ( "github.com/bcicen/ctop/config" + "github.com/bcicen/ctop/container" "github.com/bcicen/ctop/cwidgets/expanded" ui "github.com/gizak/termui" ) @@ -34,7 +35,7 @@ func RedrawRows(clr bool) { ui.Render(cGrid) } -func ExpandView(c *Container) { +func ExpandView(c *container.Container) { ui.Clear() ui.DefaultEvtStream.ResetHandlers() defer ui.DefaultEvtStream.ResetHandlers() diff --git a/main.go b/main.go index b0a3ba3..3fff87c 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "os" "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" @@ -102,7 +103,7 @@ func Shutdown() { // ensure a given sort field is valid func validSort(s string) { - if _, ok := Sorters[s]; !ok { + if _, ok := container.Sorters[s]; !ok { fmt.Printf("invalid sort field: %s\n", s) os.Exit(1) } diff --git a/menus.go b/menus.go index 5c5ae15..3edfc1b 100644 --- a/menus.go +++ b/menus.go @@ -2,6 +2,7 @@ package main import ( "github.com/bcicen/ctop/config" + "github.com/bcicen/ctop/container" "github.com/bcicen/ctop/widgets" "github.com/bcicen/ctop/widgets/menu" ui "github.com/gizak/termui" @@ -74,7 +75,7 @@ func SortMenu() { m.SortItems = true m.BorderLabel = "Sort Field" - for _, field := range SortFields() { + for _, field := range container.SortFields() { m.AddItems(menu.Item{field, ""}) }