Skip to content

Commit

Permalink
#191 Docker Compose support: first sort by compose project and then b…
Browse files Browse the repository at this point in the history
…y name

In 99% of cases container name anyway contains its compose project name.
For example we have a folder wp which has docker-compose.yaml with two services: db and wordpress. Then after docker-compose up container names will be: wp_wordpress_1 and wp_db_1.

But in docker-compose.yaml for a specific service may be explicitly set `container_name` e.g. for wordpress service it may be just "blog".
Or some other container that is not part of compose project may have a similar name like wp_admin.
Then on sorting by name we'll see:
blog <- part of wp compose project
wp_admin  <- NOT part of wp compose project
wp_db  <- part of wp compose project

To keep container always together on name sorting we'll first sort by compose project and only then by container name inside of the project.
Thus we'll always have:
wp_admin
blog
wp_db

In this case blog and wp_db was sorted separately.
  • Loading branch information
stokito committed Oct 26, 2020
1 parent 145ed61 commit 5c180e9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion container/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"sort"
"strings"

"github.com/bcicen/ctop/config"
)
Expand All @@ -19,7 +20,13 @@ var stateMap = map[string]int{
}

var idSorter = func(c1, c2 *Container) bool { return c1.Id < c2.Id }
var nameSorter = func(c1, c2 *Container) bool { return c1.GetMeta("name") < c2.GetMeta("name") }
var nameSorter = func(c1, c2 *Container) bool {
composeCmp := strings.Compare(c1.GetMeta("compose project"), c2.GetMeta("compose project"))
if composeCmp == 0 {
return c1.GetMeta("name") < c2.GetMeta("name")
}
return composeCmp < 0
}

var Sorters = map[string]sortMethod{
"id": idSorter,
Expand Down

0 comments on commit 5c180e9

Please sign in to comment.