grim/convey

Add the podman/remove task and unit tests for it
redux
2021-10-11, Gary Kramlich
1ca9512295a0
Parents cab94a2aa737
Children 7d5217252d93
Add the podman/remove task and unit tests for it
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/podman/remove.go Mon Oct 11 01:06:54 2021 -0500
@@ -0,0 +1,59 @@
+package podman
+
+import (
+ "fmt"
+ "strings"
+
+ log "github.com/sirupsen/logrus"
+
+ "keep.imfreedom.org/grim/convey/environment"
+ "keep.imfreedom.org/grim/convey/exec"
+ "keep.imfreedom.org/grim/convey/runtime"
+ "keep.imfreedom.org/grim/convey/tasks"
+ "keep.imfreedom.org/grim/convey/yaml"
+)
+
+type Remove struct {
+ Tags yaml.StringOrSlice `yaml:"tags"`
+ Quiet bool `yaml:"quiet"`
+}
+
+func (r *Remove) Execute(name string, logger *log.Entry, stageEnv environment.Environment, rt *runtime.Runtime) error {
+ env := stageEnv.Copy().Merge(rt.Environment)
+
+ tags := env.Expandv(r.Tags)
+ for _, tag := range tags {
+ generator := exec.NewGenerator(
+ "podman",
+ "rmi",
+ tag,
+ )
+
+ _, stderr, err := exec.RunOutput(name, generator.Command(), rt.Timeout)
+ if err != nil {
+ if r.Quiet && strings.Contains(stderr, "image not known") {
+ continue
+ }
+
+ return fmt.Errorf("%s", strings.TrimSpace(stderr))
+ }
+ }
+
+ return nil
+}
+
+func (r *Remove) New() tasks.Task {
+ return &Remove{}
+}
+
+func (r *Remove) Valid() error {
+ if len(r.Tags) == 0 {
+ return ErrNoTags
+ }
+
+ return nil
+}
+
+func (r *Remove) Deprecated() error {
+ return nil
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/podman/remove_test.go Mon Oct 11 01:06:54 2021 -0500
@@ -0,0 +1,25 @@
+package podman
+
+import (
+ "testing"
+
+ . "github.com/onsi/gomega"
+)
+
+func TestRemove(t *testing.T) {
+ g := NewGomegaWithT(t)
+
+ r := &Remove{
+ Tags: []string{"foo"},
+ }
+
+ g.Expect(r.Valid()).To(BeNil())
+}
+
+func TestRemoveTagsRequired(t *testing.T) {
+ g := NewGomegaWithT(t)
+
+ r := &Remove{}
+
+ g.Expect(r.Valid()).To(MatchError(ErrNoTags))
+}
--- a/podman/tasks.go Mon Oct 11 00:50:04 2021 -0500
+++ b/podman/tasks.go Mon Oct 11 01:06:54 2021 -0500
@@ -10,6 +10,7 @@
"podman/logout": &Logout{},
"podman/pull": &Pull{},
"podman/push": &Push{},
+ "podman/remove": &Remove{},
"podman/run": &Run{},
// make it go!