mirror of
https://github.com/Xevion/glance.git
synced 2025-12-15 06:11:59 -06:00
Added docker widget with documentation ✨
This commit is contained in:
78
internal/widget/docker.go
Normal file
78
internal/widget/docker.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package widget
|
||||
|
||||
import (
|
||||
"context"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"github.com/glanceapp/glance/internal/assets"
|
||||
"github.com/glanceapp/glance/internal/feed"
|
||||
)
|
||||
|
||||
type containerData struct {
|
||||
Id string
|
||||
Image string
|
||||
URL string
|
||||
Title string
|
||||
Icon CustomIcon
|
||||
StatusShort string
|
||||
StatusFull string
|
||||
StatusStyle string
|
||||
}
|
||||
|
||||
type Docker struct {
|
||||
widgetBase `yaml:",inline"`
|
||||
Containers []containerData `yaml:"-"`
|
||||
}
|
||||
|
||||
func (widget *Docker) Initialize() error {
|
||||
widget.withTitle("Docker").withCacheDuration(1 * time.Minute)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (widget *Docker) Update(ctx context.Context) {
|
||||
containers, err := feed.FetchDockerContainers(ctx)
|
||||
|
||||
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
var items []containerData
|
||||
for _, container := range containers {
|
||||
var item containerData
|
||||
item.Id = container.Id
|
||||
item.Image = container.Image
|
||||
item.URL = container.URL
|
||||
item.Title = container.Title
|
||||
|
||||
_ = item.Icon.FromURL(container.IconURL)
|
||||
|
||||
switch container.State {
|
||||
case "paused":
|
||||
case "starting":
|
||||
case "unhealthy":
|
||||
item.StatusStyle = "warning"
|
||||
break
|
||||
case "stopped":
|
||||
case "dead":
|
||||
case "exited":
|
||||
item.StatusStyle = "error"
|
||||
break
|
||||
default:
|
||||
item.StatusStyle = "success"
|
||||
}
|
||||
|
||||
item.StatusFull = container.Status
|
||||
item.StatusShort = cases.Title(language.English, cases.Compact).String(container.State)
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
widget.Containers = items
|
||||
}
|
||||
|
||||
func (widget *Docker) Render() template.HTML {
|
||||
return widget.render(widget, assets.DockerTemplate)
|
||||
}
|
||||
@@ -185,15 +185,10 @@ type CustomIcon struct {
|
||||
// invert the color based on the theme being light or dark
|
||||
}
|
||||
|
||||
func (i *CustomIcon) UnmarshalYAML(node *yaml.Node) error {
|
||||
var value string
|
||||
if err := node.Decode(&value); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
prefix, icon, found := strings.Cut(value, ":")
|
||||
func (i *CustomIcon) FromURL(url string) error {
|
||||
prefix, icon, found := strings.Cut(url, ":")
|
||||
if !found {
|
||||
i.URL = value
|
||||
i.URL = url
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -218,8 +213,16 @@ func (i *CustomIcon) UnmarshalYAML(node *yaml.Node) error {
|
||||
|
||||
i.URL = "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons@master/" + ext + "/" + basename + "." + ext
|
||||
default:
|
||||
i.URL = value
|
||||
i.URL = url
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *CustomIcon) UnmarshalYAML(node *yaml.Node) error {
|
||||
var value string
|
||||
if err := node.Decode(&value); err != nil {
|
||||
return err
|
||||
}
|
||||
return i.FromURL(value)
|
||||
}
|
||||
|
||||
@@ -71,6 +71,8 @@ func New(widgetType string) (Widget, error) {
|
||||
widget = &SplitColumn{}
|
||||
case "custom-api":
|
||||
widget = &CustomApi{}
|
||||
case "docker":
|
||||
widget = &Docker{}
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown widget type: %s", widgetType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user