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

     1  package pivotal
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/olebedev/config"
     6  	"github.com/wtfutil/wtf/cfg"
     7  	"os"
     8  )
     9  
    10  const (
    11  	defaultFocusable = true
    12  	defaultTitle     = "Pivotal"
    13  )
    14  
    15  type customQuery struct {
    16  	title   string `help:"Display title for this query"`
    17  	filter  string `help:"Pivotal search query filter"`
    18  	perPage int    `help:"Number of issues to show"`
    19  	project string `help:"Pivotal project id"`
    20  }
    21  
    22  type Settings struct {
    23  	*cfg.Common
    24  
    25  	filter        string
    26  	projectId     string
    27  	apiToken      string
    28  	status        string
    29  	customQueries []customQuery `help:"Custom queries allow you to filter pull requests and issues however you like. Give the query a title and a filter. Filters can be copied directly from GitHub’s UI." optional:"true"`
    30  }
    31  
    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  		filter:    ymlConfig.UString("filter", ymlConfig.UString("filter")),
    37  		projectId: ymlConfig.UString("projectId", ymlConfig.UString("projectId", os.Getenv("PIVOTALTRACKER_PROJECT"))),
    38  		apiToken:  ymlConfig.UString("apiToken", ymlConfig.UString("apiToken", os.Getenv("PIVOTALTRACKER_TOKEN"))),
    39  		status:    ymlConfig.UString("status"),
    40  	}
    41  
    42  	settings.customQueries = parseCustomQueries(ymlConfig)
    43  
    44  	cfg.ModuleSecret(name, globalConfig, &settings.apiToken).Load()
    45  
    46  	return &settings
    47  }
    48  
    49  func parseCustomQueries(ymlConfig *config.Config) []customQuery {
    50  	var result []customQuery
    51  
    52  	if customQueries, err := ymlConfig.Map("customQueries"); err == nil {
    53  		for _, query := range customQueries {
    54  			c := customQuery{}
    55  			for key, value := range query.(map[string]interface{}) {
    56  				switch key {
    57  				case "title":
    58  					c.title = value.(string)
    59  				case "filter":
    60  					c.filter = value.(string)
    61  				case "project":
    62  					switch value := value.(type) {
    63  					case bool, float64, int:
    64  						c.project = fmt.Sprint(value)
    65  					case string:
    66  						c.project = value
    67  					}
    68  				case "perPage":
    69  					c.perPage = value.(int)
    70  				}
    71  			}
    72  
    73  			if c.title != "" && c.filter != "" {
    74  				result = append(result, c)
    75  			}
    76  		}
    77  	}
    78  	return result
    79  }