jx/internal/fan/container.go
Matthew Rich c34a76981e
Some checks are pending
Lint / golangci-lint (push) Waiting to run
Declarative Tests / test (push) Waiting to run
Declarative Tests / build-fedora (push) Waiting to run
Declarative Tests / build-ubuntu-focal (push) Waiting to run
move source/target converters to fan pkg
2024-09-19 08:03:23 +00:00

78 lines
2.0 KiB
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package fan
import (
"context"
_ "encoding/json"
"fmt"
_ "gopkg.in/yaml.v3"
"net/url"
_ "path/filepath"
"decl/internal/resource"
"decl/internal/folio"
"decl/internal/data"
_ "os"
_ "io"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"log/slog"
)
type Container struct {
apiClient resource.ContainerClient
}
func NewContainer(containerClientApi resource.ContainerClient) *Container {
var apiClient resource.ContainerClient = containerClientApi
if apiClient == nil {
var err error
apiClient, err = client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
}
return &Container{
apiClient: apiClient,
}
}
func init() {
folio.DocumentRegistry.ConverterTypes.Register([]string{"container"}, func(u *url.URL) data.Converter {
c := NewContainer(nil)
return c
})
}
func (c *Container) Type() data.TypeName { return "container" }
func (c *Container) Extract(sourceResource data.Resource, filter data.ElementSelector) (document data.Document, err error) {
var extractErr error
ctx := context.Background()
slog.Info("container source Extract()", "container", c)
containers, err := c.apiClient.ContainerList(ctx, container.ListOptions{All: true})
if err != nil {
return nil, err
}
document = folio.DocumentRegistry.NewDocument(folio.URI(sourceResource.URI()))
for _, container := range containers {
runningContainer := resource.NewContainer(nil)
if inspectErr := runningContainer.Inspect(ctx, container.ID); inspectErr != nil {
extractErr = fmt.Errorf("%w: %w", extractErr, inspectErr)
}
document.(*folio.Document).AddResourceDeclaration("container", runningContainer)
}
return document, extractErr
}
func (c *Container) Emit(document data.Document, filter data.ElementSelector) (resource data.Resource, err error) {
return nil, nil
}
func (c *Container) Close() error {
return nil
}