github.com/segakazzz/buffalo@v0.16.22-0.20210119082501-1f52048d3feb/genny/grift/options.go (about)

     1  package grift
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/gobuffalo/flect/name"
     8  )
     9  
    10  // Options for creating a new grift task
    11  type Options struct {
    12  	Name       name.Ident   `json:"name"`
    13  	Parts      []name.Ident `json:"parts"`
    14  	Args       []string     `json:"args"`
    15  	Namespaced bool         `json:"namespaced"`
    16  }
    17  
    18  // Last checks if the name is the last of the parts
    19  func (opts Options) Last(n name.Ident) bool {
    20  	return opts.Parts[len(opts.Parts)-1].String() == n.String()
    21  }
    22  
    23  // Validate options
    24  func (opts *Options) Validate() error {
    25  	if len(opts.Args) == 0 {
    26  		return fmt.Errorf("you need to provide a name for the grift task")
    27  	}
    28  
    29  	opts.Namespaced = strings.Contains(opts.Args[0], ":")
    30  
    31  	for _, n := range strings.Split(opts.Args[0], ":") {
    32  		opts.Parts = append(opts.Parts, name.New(n))
    33  	}
    34  	opts.Name = opts.Parts[len(opts.Parts)-1]
    35  	return nil
    36  }