123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package feed
- import (
- "context"
- "github.com/docker/docker/api/types/container"
- "github.com/docker/docker/client"
- "strings"
- )
- const (
- dockerAPIVersion = "1.24"
- dockerGlanceEnable = "glance.enable"
- dockerGlanceTitle = "glance.title"
- dockerGlanceUrl = "glance.url"
- dockerGlanceIconUrl = "glance.iconUrl"
- )
- type DockerContainer struct {
- Id string
- Image string
- Title string
- URL string
- IconURL string
- Status string
- State string
- }
- func FetchDockerContainers(ctx context.Context) ([]DockerContainer, error) {
- apiClient, err := client.NewClientWithOpts(client.WithVersion(dockerAPIVersion), client.FromEnv)
- if err != nil {
- return nil, err
- }
- defer apiClient.Close()
- containers, err := apiClient.ContainerList(ctx, container.ListOptions{})
- if err != nil {
- return nil, err
- }
- var results []DockerContainer
- for _, c := range containers {
- isGlanceEnabled := getLabelValue(c.Labels, dockerGlanceEnable, "true")
- if isGlanceEnabled != "true" {
- continue
- }
- results = append(results, DockerContainer{
- Id: c.ID,
- Image: c.Image,
- Title: getLabelValue(c.Labels, dockerGlanceTitle, strings.Join(c.Names, "")),
- URL: getLabelValue(c.Labels, dockerGlanceUrl, ""),
- IconURL: getLabelValue(c.Labels, dockerGlanceIconUrl, "si:docker"),
- Status: c.Status,
- State: c.State,
- })
- }
- return results, nil
- }
- // 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
- }
|