mirror of
https://github.com/Xevion/glance.git
synced 2025-12-09 16:07:20 -06:00
Rewrote docker client to reduce dependencies ♻️
This commit is contained in:
@@ -5,12 +5,21 @@ import (
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"html/template"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/glanceapp/glance/internal/assets"
|
||||
"github.com/glanceapp/glance/internal/feed"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultDockerHost = "unix:///var/run/docker.sock"
|
||||
dockerGlanceEnable = "glance.enable"
|
||||
dockerGlanceTitle = "glance.title"
|
||||
dockerGlanceUrl = "glance.url"
|
||||
dockerGlanceIconUrl = "glance.iconUrl"
|
||||
)
|
||||
|
||||
type containerData struct {
|
||||
Id string
|
||||
Image string
|
||||
@@ -24,6 +33,7 @@ type containerData struct {
|
||||
|
||||
type Docker struct {
|
||||
widgetBase `yaml:",inline"`
|
||||
HostURL string `yaml:"host-url"`
|
||||
Containers []containerData `yaml:"-"`
|
||||
}
|
||||
|
||||
@@ -32,24 +42,34 @@ func (widget *Docker) Initialize() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (widget *Docker) Update(ctx context.Context) {
|
||||
containers, err := feed.FetchDockerContainers(ctx)
|
||||
func (widget *Docker) Update(_ context.Context) {
|
||||
if widget.HostURL == "" {
|
||||
widget.HostURL = defaultDockerHost
|
||||
}
|
||||
|
||||
containers, err := feed.FetchDockerContainers(widget.HostURL)
|
||||
|
||||
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
var items []containerData
|
||||
for _, container := range containers {
|
||||
for _, c := range containers {
|
||||
isGlanceEnabled := getLabelValue(c.Labels, dockerGlanceEnable, "true")
|
||||
|
||||
if isGlanceEnabled != "true" {
|
||||
continue
|
||||
}
|
||||
|
||||
var item containerData
|
||||
item.Id = container.Id
|
||||
item.Image = container.Image
|
||||
item.URL = container.URL
|
||||
item.Title = container.Title
|
||||
item.Id = c.Id
|
||||
item.Image = c.Image
|
||||
item.Title = getLabelValue(c.Labels, dockerGlanceTitle, strings.Join(c.Names, ""))
|
||||
item.URL = getLabelValue(c.Labels, dockerGlanceUrl, "")
|
||||
|
||||
_ = item.Icon.FromURL(container.IconURL)
|
||||
_ = item.Icon.FromURL(getLabelValue(c.Labels, dockerGlanceIconUrl, "si:docker"))
|
||||
|
||||
switch container.State {
|
||||
switch c.State {
|
||||
case "paused":
|
||||
case "starting":
|
||||
case "unhealthy":
|
||||
@@ -64,8 +84,8 @@ func (widget *Docker) Update(ctx context.Context) {
|
||||
item.StatusStyle = "success"
|
||||
}
|
||||
|
||||
item.StatusFull = container.Status
|
||||
item.StatusShort = cases.Title(language.English, cases.Compact).String(container.State)
|
||||
item.StatusFull = c.Status
|
||||
item.StatusShort = cases.Title(language.English, cases.Compact).String(c.State)
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
@@ -76,3 +96,11 @@ func (widget *Docker) Update(ctx context.Context) {
|
||||
func (widget *Docker) Render() template.HTML {
|
||||
return widget.render(widget, assets.DockerTemplate)
|
||||
}
|
||||
|
||||
// getLabelValue get string value associated to a label.
|
||||
func getLabelValue(labels map[string]string, labelName, defaultValue string) string {
|
||||
if value, ok := labels[labelName]; ok && len(value) > 0 {
|
||||
return value
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user