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

     1  package resource
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/gobuffalo/attrs"
     8  	"github.com/gobuffalo/meta"
     9  )
    10  
    11  // Options for generating a new resource
    12  type Options struct {
    13  	App           meta.App    `json:"app"`
    14  	Name          string      `json:"name"`
    15  	Model         string      `json:"model"`
    16  	SkipMigration bool        `json:"skip_migration"`
    17  	SkipModel     bool        `json:"skip_model"`
    18  	SkipTemplates bool        `json:"skip_templates"`
    19  	Attrs         attrs.Attrs `json:"props"`
    20  }
    21  
    22  // Validate that options are usuable
    23  func (opts *Options) Validate() error {
    24  	if opts.App.IsZero() {
    25  		opts.App = meta.New(".")
    26  	}
    27  
    28  	if len(opts.Name) == 0 {
    29  		return fmt.Errorf("you must provide a name")
    30  	}
    31  
    32  	if len(opts.Model) == 0 {
    33  		opts.Model = opts.Name
    34  	}
    35  
    36  	if strings.Contains(opts.Model, "/") {
    37  		parts := strings.Split(opts.Model, "/")
    38  		opts.Model = parts[len(parts)-1]
    39  	}
    40  
    41  	if opts.App.AsAPI {
    42  		opts.SkipTemplates = true
    43  	}
    44  
    45  	return nil
    46  }