2024-01-08 13:26:03 -06:00
|
|
|
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
|
2023-01-10 16:08:57 -06:00
|
|
|
|
2020-02-23 17:02:01 -06:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-06-18 10:21:55 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2023-07-10 23:55:53 -05:00
|
|
|
"github.com/docker/docker/api/types/volume"
|
2020-02-23 17:02:01 -06:00
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
)
|
|
|
|
|
2023-07-10 23:55:53 -05:00
|
|
|
func NewDockerVolumeRemoveExecutor(volumeName string, force bool) common.Executor {
|
2020-06-18 10:21:55 -05:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
cli, err := GetDockerClient(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-24 11:50:43 -05:00
|
|
|
defer cli.Close()
|
2020-06-18 10:21:55 -05:00
|
|
|
|
2023-07-10 23:55:53 -05:00
|
|
|
list, err := cli.VolumeList(ctx, volume.ListOptions{Filters: filters.NewArgs()})
|
2020-06-18 10:21:55 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vol := range list.Volumes {
|
2023-07-10 23:55:53 -05:00
|
|
|
if vol.Name == volumeName {
|
|
|
|
return removeExecutor(volumeName, force)(ctx)
|
2020-06-18 10:21:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Volume not found - do nothing
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeExecutor(volume string, force bool) common.Executor {
|
2020-02-23 17:02:01 -06:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
logger.Debugf("%sdocker volume rm %s", logPrefix, volume)
|
|
|
|
|
|
|
|
if common.Dryrun(ctx) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 23:15:42 -05:00
|
|
|
cli, err := GetDockerClient(ctx)
|
2020-02-23 17:02:01 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-24 11:50:43 -05:00
|
|
|
defer cli.Close()
|
2020-02-23 17:02:01 -06:00
|
|
|
|
|
|
|
return cli.VolumeRemove(ctx, volume, force)
|
|
|
|
}
|
|
|
|
}
|