fix searching for the container id
All checks were successful
Declarative Tests / test (push) Successful in 50s

This commit is contained in:
Matthew Rich 2024-03-22 23:21:11 -07:00
parent 6af91dec6c
commit dc85c45226

View File

@ -12,7 +12,7 @@ _ "os"
_ "gopkg.in/yaml.v3"
_ "os/exec"
_ "strings"
"log"
"log/slog"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/container"
@ -199,13 +199,14 @@ func (c *Container) Read(ctx context.Context) ([]byte, error) {
All: true,
Filters: filterArgs,
})
if err != nil {
panic(fmt.Errorf("%w: %s %s", err, c.Type(), c.Name))
}
for _, container := range containers {
for _, containerName := range container.Names {
if containerName == c.Name {
if containerName == "/" + c.Name {
containerID = container.ID
}
}
@ -217,7 +218,9 @@ func (c *Container) Read(ctx context.Context) ([]byte, error) {
} else {
c.State = "present"
c.Id = containerJSON.ID
if c.Name == "" {
c.Name = containerJSON.Name
}
c.Path = containerJSON.Path
c.Image = containerJSON.Image
if containerJSON.State != nil {
@ -231,6 +234,7 @@ func (c *Container) Read(ctx context.Context) ([]byte, error) {
c.RestartCount = containerJSON.RestartCount
c.Driver = containerJSON.Driver
}
slog.Info("Read() ", "type", c.Type(), "name", c.Name, "Id", c.Id)
return yaml.Marshal(c)
}
@ -241,7 +245,7 @@ func (c *Container) Delete(ctx context.Context) error {
Force: false,
})
if err != nil {
log.Printf("Failed to remove: %s\n", c.Id)
slog.Error("Failed to remove: ", "Id", c.Id)
panic(err)
}
return err