Add support for app key + token

This commit is contained in:
Glenn Y. Rolland 2019-09-04 07:35:49 +02:00
parent af6c6bf521
commit 296f16b122
4 changed files with 60 additions and 20 deletions

View file

@ -21,12 +21,19 @@ Make sure you have Docker installed, then type:
## Usage
## Creating a developper account
1. Create a trello account
2. Confirm your email
3. Enable developper account on <https://trello.com/app-key>
4. Get an developer API KEY
## Getting a Trello TOKEN
Open the following URL in your web browser and authenticate yourself. That will
give you the TRELLO_TOKEN that will be needed in the next step.
<https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Server%20Token&key=58117ebf843d49b05bca074c5fd520ee>
<https://trello.com/1/authorize?expiration=never&scope=read,write,account&response_type=token&name=Trello2Mail&key=YOUR-API-KEY>
## Normal use

View file

@ -31,8 +31,9 @@ type Config struct {
SmtpAuthType string `mapstructure:"smtp-auth-type"`
SmtpSecurityType string `mapstructure:"smtp-security-type"`
TrelloUrl string `mapstructure:"trello-url"`
TrelloToken string `mapstructure:"trello-token"`
TrelloUrl string `mapstructure:"trello-url"`
TrelloApiKey string `mapstructure:"trello-api-key"`
TrelloToken string `mapstructure:"trello-token"`
Parser *cobra.Command `mapstructure:"-"`
}
@ -53,7 +54,8 @@ func NewConfig() *Config {
cmd.PersistentFlags().StringVarP(&self.EmailSubject, "email-subject", "", "", "email subject")
cmd.PersistentFlags().StringVarP(&self.TrelloUrl, "trello-url", "", "", "url of trello board")
cmd.PersistentFlags().StringVarP(&self.TrelloToken, "trello-token", "", "", "url of trello token")
cmd.PersistentFlags().StringVarP(&self.TrelloUrl, "trello-api-key", "", "", "API KEY for trello access")
cmd.PersistentFlags().StringVarP(&self.TrelloToken, "trello-token", "", "", "TOKEN for trello access")
cmd.PersistentFlags().StringVarP(&self.SmtpHostname, "smtp-hostname", "", "", "address of smtp server")
cmd.PersistentFlags().StringVarP(&self.SmtpUsername, "smtp-username", "", "", "username for smtp server")
@ -67,6 +69,7 @@ func NewConfig() *Config {
viper.BindPFlag("email-subject", cmd.PersistentFlags().Lookup("email-subject"))
viper.BindPFlag("trello-url", cmd.PersistentFlags().Lookup("trello-url"))
viper.BindPFlag("trello-token", cmd.PersistentFlags().Lookup("trello-token"))
viper.BindPFlag("trello-api-key", cmd.PersistentFlags().Lookup("trello-api-key"))
viper.BindPFlag("smtp-hostname", cmd.PersistentFlags().Lookup("smtp-hostname"))
viper.BindPFlag("smtp-username", cmd.PersistentFlags().Lookup("smtp-username"))
viper.BindPFlag("smtp-password", cmd.PersistentFlags().Lookup("smtp-password"))

View file

@ -4,6 +4,7 @@ import (
"crypto/tls"
"fmt"
"github.com/go-mail/mail"
"os"
)
func main() {
@ -11,10 +12,15 @@ func main() {
fmt.Println("d: parsing config")
config := NewConfig()
config.Parse()
fmt.Printf("%+v\n", config)
// Get task list as markdown
fmt.Println("d: configuring trello")
trelloCtx := NewTrello(config.TrelloToken)
trelloCtx := NewTrello(config.TrelloApiKey, config.TrelloToken)
if trelloCtx == nil {
fmt.Println("ERROR: Unable to initialize trello context")
os.Exit(1)
}
fmt.Println("d: getting trello boards")
var trelloBoardsList []TrelloBoard

View file

@ -18,18 +18,9 @@ import (
"text/template"
)
const (
// See https://trello.com/app-key
APP_KEY string = "58117ebf843d49b05bca074c5fd520ee"
)
type TrelloConfig struct {
Url string
Token string
}
type TrelloCtx struct {
Token string
ApiKey string
Client *trello.Client
}
@ -53,13 +44,36 @@ func runcmd(command string) string {
return string(out)
}
func GetTokenProcessMessage() string {
func MessageForApiKey() string {
/*
url := strings.Join([]string{
"https://trello.com/1/authorize?expiration=never",
"name=Trello-To-Mail",
"scope=read",
"response_type=token",
fmt.Sprintf("key=%s", apiKey),
}, "&")
*/
url := "https://trello.com/app-key/"
text := strings.Join([]string{
"Wrong or empty TRELLO_API_KEY value. Please visit:",
url,
"Then enable developper account.",
"When you have your user api key, set TRELLO_API_KEY=<your-api-key>",
}, "\n\n")
return text
}
func MessageForToken(apiKey string) string {
url := strings.Join([]string{
"https://trello.com/1/authorize?expiration=never",
"name=taskell",
"name=Trello-To-Mail",
"scope=read",
"response_type=token",
fmt.Sprintf("key=%s", APP_KEY),
fmt.Sprintf("key=%s", apiKey),
}, "&")
text := strings.Join([]string{
@ -71,11 +85,21 @@ func GetTokenProcessMessage() string {
return text
}
func NewTrello(token string) *TrelloCtx {
client := trello.NewClient(APP_KEY, token)
func NewTrello(apiKey string, token string) *TrelloCtx {
if len(apiKey) == 0 {
fmt.Println(MessageForApiKey())
return nil
}
if len(token) == 0 {
fmt.Println(MessageForToken(apiKey))
return nil
}
ctx := TrelloCtx{}
ctx.ApiKey = apiKey
ctx.Token = token
client := trello.NewClient(apiKey, token)
ctx.Client = client
return &ctx