github.com/dnephin/dobi@v0.15.0/tasks/types/task.go (about)

     1  package types
     2  
     3  import (
     4  	"github.com/dnephin/dobi/config"
     5  	"github.com/dnephin/dobi/logging"
     6  	"github.com/dnephin/dobi/tasks/context"
     7  	"github.com/dnephin/dobi/tasks/task"
     8  )
     9  
    10  // Task interface performs some operation with a resource config
    11  type Task interface {
    12  	logging.LogRepresenter
    13  	Name() task.Name
    14  	Run(*context.ExecuteContext, bool) (bool, error)
    15  	Stop(*context.ExecuteContext) error
    16  }
    17  
    18  // RunFunc is a function which performs the task. It received a context and a
    19  // bool indicating if any dependencies were modified. It should return true if
    20  // the resource was modified, otherwise false.
    21  type RunFunc func(*context.ExecuteContext, bool) (bool, error)
    22  
    23  // TaskConfig is a data object which stores the full configuration of a Task
    24  type TaskConfig interface {
    25  	Name() task.Name
    26  	Resource() config.Resource
    27  	Dependencies() []string
    28  	Task(config.Resource) Task
    29  }
    30  
    31  type taskConfig struct {
    32  	name      task.Name
    33  	resource  config.Resource
    34  	deps      func() []string
    35  	buildTask func(task.Name, config.Resource) Task
    36  }
    37  
    38  func (t taskConfig) Name() task.Name {
    39  	return t.name
    40  }
    41  
    42  func (t taskConfig) Resource() config.Resource {
    43  	return t.resource
    44  }
    45  
    46  func (t taskConfig) Dependencies() []string {
    47  	return t.deps()
    48  }
    49  
    50  func (t taskConfig) Task(res config.Resource) Task {
    51  	return t.buildTask(t.name, res)
    52  }
    53  
    54  // TaskBuilder is a function which creates a new Task from a name and config
    55  type TaskBuilder func(task.Name, config.Resource) Task
    56  
    57  // NewTaskConfig returns a TaskConfig from components
    58  func NewTaskConfig(
    59  	name task.Name,
    60  	resource config.Resource,
    61  	deps func() []string,
    62  	buildTask TaskBuilder,
    63  ) TaskConfig {
    64  	return &taskConfig{
    65  		name:      name,
    66  		resource:  resource,
    67  		deps:      deps,
    68  		buildTask: buildTask,
    69  	}
    70  }
    71  
    72  // NoStop implements the Stop() method from the types.Task interface. It can be
    73  // used by tasks that don't do anything during the `stop` phase of execution.
    74  type NoStop struct{}
    75  
    76  // Stop does nothing
    77  func (t *NoStop) Stop(_ *context.ExecuteContext) error {
    78  	return nil
    79  }