github.com/dnephin/dobi@v0.15.0/tasks/job/remove.go (about)

     1  package job
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/dnephin/dobi/config"
     8  	"github.com/dnephin/dobi/logging"
     9  	"github.com/dnephin/dobi/tasks/context"
    10  	"github.com/dnephin/dobi/tasks/task"
    11  	"github.com/dnephin/dobi/tasks/types"
    12  )
    13  
    14  // RemoveTask is a task which removes the container used by the run task and the
    15  // artifact created by the run task.
    16  type RemoveTask struct {
    17  	types.NoStop
    18  	name   task.Name
    19  	config *config.JobConfig
    20  }
    21  
    22  func newRemoveTask(name task.Name, conf config.Resource) types.Task {
    23  	return &RemoveTask{name: name, config: conf.(*config.JobConfig)}
    24  }
    25  
    26  // Name returns the name of the task
    27  func (t *RemoveTask) Name() task.Name {
    28  	return t.name
    29  }
    30  
    31  // Repr formats the task for logging
    32  func (t *RemoveTask) Repr() string {
    33  	return fmt.Sprintf("%s %v", t.name.Format("job"), t.config.Artifact)
    34  }
    35  
    36  // Run creates the host path if it doesn't already exist
    37  func (t *RemoveTask) Run(ctx *context.ExecuteContext, _ bool) (bool, error) {
    38  	logger := logging.ForTask(t)
    39  
    40  	removeContainer(logger, ctx.Client, containerName(ctx, t.name.Resource())) // nolint: errcheck
    41  
    42  	for _, path := range t.config.Artifact.Paths() {
    43  		if err := os.RemoveAll(path); err != nil {
    44  			logger.Warnf("failed to remove artifact %s: %s", t.config.Artifact, err)
    45  		}
    46  	}
    47  
    48  	logger.Info("Removed")
    49  	return true, nil
    50  }