github.com/dnephin/dobi@v0.15.0/cmd/autoclean.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dnephin/dobi/config"
     7  	"github.com/dnephin/dobi/tasks"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  func newCleanCommand(opts *dobiOptions) *cobra.Command {
    12  	cmd := &cobra.Command{
    13  		Use:   "autoclean",
    14  		Short: "Run the remove action for all resources",
    15  		RunE: func(cmd *cobra.Command, args []string) error {
    16  			return runClean(opts)
    17  		},
    18  	}
    19  	return cmd
    20  }
    21  
    22  func runClean(opts *dobiOptions) error {
    23  	conf, err := config.Load(opts.filename)
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	client, err := buildClient()
    29  	if err != nil {
    30  		return fmt.Errorf("failed to create client: %s", err)
    31  	}
    32  
    33  	return tasks.Run(tasks.RunOptions{
    34  		Client: client,
    35  		Config: conf,
    36  		Tasks:  removeTasks(conf),
    37  		Quiet:  opts.quiet,
    38  	})
    39  }
    40  
    41  func removeTasks(conf *config.Config) []string {
    42  	resources := conf.Sorted()
    43  	tasks := []string{}
    44  	for i := len(resources) - 1; i >= 0; i-- {
    45  		tasks = append(tasks, resources[i]+":rm")
    46  	}
    47  	return tasks
    48  }