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

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  
     8  	"github.com/gobuffalo/buffalo-pop/v2/genny/newapp"
     9  	"github.com/gobuffalo/buffalo/genny/ci"
    10  	"github.com/gobuffalo/buffalo/genny/docker"
    11  	"github.com/gobuffalo/buffalo/genny/refresh"
    12  	"github.com/gobuffalo/buffalo/genny/vcs"
    13  	"github.com/gobuffalo/buffalo/runtime"
    14  	"github.com/gobuffalo/meta"
    15  )
    16  
    17  // Options for a new Buffalo application
    18  type Options struct {
    19  	App            meta.App
    20  	Docker         *docker.Options
    21  	Pop            *newapp.Options
    22  	CI             *ci.Options
    23  	VCS            *vcs.Options
    24  	Refresh        *refresh.Options
    25  	Version        string
    26  	ForbiddenNames []string
    27  }
    28  
    29  // Validate that options are usuable
    30  func (opts *Options) Validate() error {
    31  	if opts.App.IsZero() {
    32  		opts.App = meta.New(".")
    33  	}
    34  
    35  	if len(opts.Version) == 0 {
    36  		opts.Version = runtime.Version
    37  	}
    38  
    39  	if opts.Pop != nil {
    40  		if err := opts.Pop.Validate(); err != nil {
    41  			return err
    42  		}
    43  		opts.Pop.Root = opts.App.Root
    44  	}
    45  
    46  	if opts.CI != nil {
    47  		if opts.CI.App.IsZero() {
    48  			opts.CI.App = opts.App
    49  		}
    50  		if err := opts.CI.Validate(); err != nil {
    51  			return err
    52  		}
    53  	}
    54  
    55  	if opts.Refresh != nil {
    56  		if opts.Refresh.App.IsZero() {
    57  			opts.Refresh.App = opts.App
    58  		}
    59  		if err := opts.Refresh.Validate(); err != nil {
    60  			return err
    61  		}
    62  	}
    63  
    64  	if opts.VCS != nil {
    65  		if opts.VCS.App.IsZero() {
    66  			opts.VCS.App = opts.App
    67  		}
    68  		if err := opts.VCS.Validate(); err != nil {
    69  			return err
    70  		}
    71  	}
    72  
    73  	name := strings.ToLower(opts.App.Name.String())
    74  	for _, n := range opts.ForbiddenNames {
    75  		rx, err := regexp.Compile(n)
    76  		if err != nil {
    77  			return err
    78  		}
    79  		if rx.MatchString(name) {
    80  			return fmt.Errorf("name %s is not allowed, try a different application name", opts.App.Name)
    81  		}
    82  	}
    83  
    84  	keywords := []string{"buffalo", "test", "dev"}
    85  	for _, kw := range keywords {
    86  		if name != kw {
    87  			continue
    88  		}
    89  
    90  		return fmt.Errorf("name %s is not allowed, try a different application name", opts.App.Name)
    91  	}
    92  
    93  	if !nameRX.MatchString(name) {
    94  		return fmt.Errorf("name %s is not allowed, application name can only contain [a-Z0-9-_]", opts.App.Name)
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  var nameRX = regexp.MustCompile(`^[\w-]+$`)