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

     1  package todo_plus
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/olebedev/config"
     7  	"github.com/wtfutil/wtf/cfg"
     8  )
     9  
    10  const (
    11  	defaultTitle     = "Todo"
    12  	defaultFocusable = true
    13  )
    14  
    15  type Settings struct {
    16  	*cfg.Common
    17  
    18  	backendType     string
    19  	backendSettings *config.Config
    20  }
    21  
    22  func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
    23  
    24  	backend, _ := ymlConfig.Get("backendSettings")
    25  
    26  	settings := Settings{
    27  		Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
    28  
    29  		backendType:     ymlConfig.UString("backendType"),
    30  		backendSettings: backend,
    31  	}
    32  
    33  	return &settings
    34  }
    35  
    36  func FromTodoist(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
    37  	apiKey := ymlConfig.UString("apiKey", ymlConfig.UString("apikey", os.Getenv("WTF_TODOIST_TOKEN")))
    38  	cfg.ModuleSecret(name, globalConfig, &apiKey).Load()
    39  	projects := ymlConfig.UList("projects")
    40  	backend, _ := config.ParseYaml("apiKey: " + apiKey)
    41  	_ = backend.Set(".projects", projects)
    42  
    43  	settings := Settings{
    44  		Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
    45  
    46  		backendType:     "todoist",
    47  		backendSettings: backend,
    48  	}
    49  
    50  	return &settings
    51  }
    52  
    53  func FromTrello(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
    54  
    55  	accessToken := ymlConfig.UString("accessToken", ymlConfig.UString("apikey", os.Getenv("WTF_TRELLO_ACCESS_TOKEN")))
    56  	apiKey := ymlConfig.UString("apiKey", os.Getenv("WTF_TRELLO_API_KEY"))
    57  	cfg.ModuleSecret(name, globalConfig, &apiKey).Load()
    58  	board := ymlConfig.UString("board")
    59  	username := ymlConfig.UString("username")
    60  	var lists []interface{}
    61  	list, err := ymlConfig.String("list")
    62  	if err == nil {
    63  		lists = append(lists, list)
    64  	} else {
    65  		lists = ymlConfig.UList("list")
    66  	}
    67  	backend, _ := config.ParseYaml("apiKey: " + apiKey)
    68  	_ = backend.Set(".accessToken", accessToken)
    69  	_ = backend.Set(".board", board)
    70  	_ = backend.Set(".username", username)
    71  	_ = backend.Set(".lists", lists)
    72  
    73  	settings := Settings{
    74  		Common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
    75  
    76  		backendType:     "trello",
    77  		backendSettings: backend,
    78  	}
    79  
    80  	return &settings
    81  }