github.com/clusterize-io/tusk@v0.6.3-0.20211001020217-cfe8a8cd0d4a/appcli/task.go (about)

     1  package appcli
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/urfave/cli"
     8  
     9  	"github.com/clusterize-io/tusk/runner"
    10  )
    11  
    12  // addTasks adds a series of tasks to a cli.App using a command creator.
    13  func addTasks(app *cli.App, meta *runner.Metadata, cfg *runner.Config, create commandCreator) error {
    14  	for _, t := range cfg.Tasks {
    15  		if err := addTask(app, meta, cfg, t, create); err != nil {
    16  			return fmt.Errorf("could not add task %q: %w", t.Name, err)
    17  		}
    18  	}
    19  
    20  	sort.Sort(cli.CommandsByName(app.Commands))
    21  	return nil
    22  }
    23  
    24  func addTask(
    25  	app *cli.App,
    26  	meta *runner.Metadata,
    27  	cfg *runner.Config,
    28  	t *runner.Task,
    29  	create commandCreator,
    30  ) error {
    31  	if t.Private {
    32  		return nil
    33  	}
    34  
    35  	command, err := create(app, meta, t)
    36  	if err != nil {
    37  		return fmt.Errorf(`could not create command %q: %w`, t.Name, err)
    38  	}
    39  
    40  	if err := addAllFlagsUsed(cfg, command, t); err != nil {
    41  		return fmt.Errorf("could not add flags: %w", err)
    42  	}
    43  
    44  	app.Commands = append(app.Commands, *command)
    45  
    46  	return nil
    47  }