github.com/dnephin/dobi@v0.15.0/tasks/alias/actions.go (about)

     1  package alias
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dnephin/dobi/config"
     7  	"github.com/dnephin/dobi/tasks/task"
     8  	"github.com/dnephin/dobi/tasks/types"
     9  )
    10  
    11  // GetTaskConfig returns a new TaskConfig for the action
    12  func GetTaskConfig(name, act string, conf *config.AliasConfig) (types.TaskConfig, error) {
    13  	switch act {
    14  	case "", "run":
    15  		return types.NewTaskConfig(
    16  			task.NewDefaultName(name, "run"), conf, RunDeps(conf), NewTask), nil
    17  	case "remove", "rm":
    18  		return types.NewTaskConfig(
    19  			task.NewName(name, "rm"), conf, RemoveDeps(conf), NewTask), nil
    20  	default:
    21  		return nil, fmt.Errorf("invalid alias action %q for task %q", act, name)
    22  	}
    23  }
    24  
    25  // NewTask creates a new Task object
    26  func NewTask(name task.Name, conf config.Resource) types.Task {
    27  	// TODO: cleaner way to avoid this cast?
    28  	return &Task{name: name, config: conf.(*config.AliasConfig)}
    29  }
    30  
    31  // RunDeps returns the dependencies for the run action
    32  func RunDeps(conf config.Resource) func() []string {
    33  	return func() []string {
    34  		return conf.Dependencies()
    35  	}
    36  }
    37  
    38  // RemoveDeps returns the dependencies for the remove action
    39  func RemoveDeps(conf config.Resource) func() []string {
    40  	return func() []string {
    41  		confDeps := conf.Dependencies()
    42  		deps := []string{}
    43  		for i := len(confDeps); i > 0; i-- {
    44  			taskname := task.ParseName(confDeps[i-1])
    45  			deps = append(deps, taskname.Resource()+":"+"rm")
    46  		}
    47  		return deps
    48  	}
    49  }