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

     1  package ci
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/gobuffalo/buffalo/runtime"
     8  
     9  	"github.com/gobuffalo/meta"
    10  	"github.com/gobuffalo/pop/v5"
    11  )
    12  
    13  // Available CI implementations
    14  var Available = []string{"travis", "gitlab", "circleci"}
    15  
    16  // Options for CI
    17  type Options struct {
    18  	App      meta.App
    19  	DBType   string
    20  	Provider string
    21  	Version  string
    22  }
    23  
    24  // Validate that options are usuable
    25  func (opts *Options) Validate() error {
    26  	if opts.App.IsZero() {
    27  		opts.App = meta.New(".")
    28  	}
    29  
    30  	if len(opts.Version) == 0 {
    31  		opts.Version = runtime.Version
    32  	}
    33  
    34  	if len(opts.Provider) == 0 {
    35  		return fmt.Errorf("no provider chosen")
    36  	}
    37  	opts.Provider = strings.ToLower(opts.Provider)
    38  
    39  	var found bool
    40  	for _, a := range Available {
    41  		if opts.Provider == a {
    42  			found = true
    43  			break
    44  		}
    45  		if opts.Provider == a+"-ci" {
    46  			opts.Provider = a
    47  			found = true
    48  			break
    49  		}
    50  	}
    51  	if !found {
    52  		return fmt.Errorf("unknown provider %s expecting one of %s", opts.Provider, strings.Join(Available, ", "))
    53  	}
    54  
    55  	found = false
    56  	for _, d := range pop.AvailableDialects {
    57  		if d == opts.DBType {
    58  			found = true
    59  			break
    60  		}
    61  	}
    62  	if !found {
    63  		return fmt.Errorf("unknown dialect %q expecting one of %s", opts.DBType, strings.Join(pop.AvailableDialects, ", "))
    64  	}
    65  	return nil
    66  }