github.com/JarrahG/buffalocli@v0.0.0-20230801092127-b85bfd5d395a/internal/genny/ci/options.go (about)

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