github.com/wtfutil/wtf@v0.43.0/modules/buildkite/settings.go (about)

     1  package buildkite
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/olebedev/config"
     7  	"github.com/wtfutil/wtf/cfg"
     8  	"github.com/wtfutil/wtf/utils"
     9  )
    10  
    11  const (
    12  	defaultTitle     = "Buildkite"
    13  	defaultFocusable = true
    14  )
    15  
    16  // PipelineSettings defines the configuration properties for a pipeline
    17  type PipelineSettings struct {
    18  	slug     string
    19  	branches []string
    20  }
    21  
    22  // Settings defines the configuration properties for this module
    23  type Settings struct {
    24  	*cfg.Common
    25  
    26  	apiKey    string             `help:"Your Buildkite API Token"`
    27  	orgSlug   string             `help:"Organization Slug"`
    28  	pipelines []PipelineSettings `help:"An array of pipelines to get data from"`
    29  }
    30  
    31  // NewSettingsFromYAML creates a new settings instance from a YAML config block
    32  func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
    33  	settings := Settings{
    34  		Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
    35  
    36  		apiKey:    ymlConfig.UString("apiKey", os.Getenv("WTF_BUILDKITE_TOKEN")),
    37  		orgSlug:   ymlConfig.UString("organizationSlug"),
    38  		pipelines: buildPipelineSettings(ymlConfig),
    39  	}
    40  
    41  	cfg.ModuleSecret(name, globalConfig, &settings.apiKey).Load()
    42  
    43  	return &settings
    44  }
    45  
    46  /* -------------------- Unexported Functions -------------------- */
    47  
    48  func buildPipelineSettings(ymlConfig *config.Config) []PipelineSettings {
    49  	pipelines := []PipelineSettings{}
    50  
    51  	for slug := range ymlConfig.UMap("pipelines") {
    52  		branches := utils.ToStrs(ymlConfig.UList("pipelines." + slug + ".branches"))
    53  		if len(branches) == 0 {
    54  			branches = []string{"master"}
    55  		}
    56  
    57  		pipeline := PipelineSettings{
    58  			slug:     slug,
    59  			branches: branches,
    60  		}
    61  
    62  		pipelines = append(pipelines, pipeline)
    63  	}
    64  
    65  	return pipelines
    66  }