From 296f16b12228da0b681cdff102e658bac3ce6447 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Wed, 4 Sep 2019 07:35:49 +0200 Subject: [PATCH] Add support for app key + token --- README.md | 9 ++++++- cmd/trello2mail/config.go | 9 ++++--- cmd/trello2mail/main.go | 8 +++++- cmd/trello2mail/trello.go | 54 ++++++++++++++++++++++++++++----------- 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b401789..8ae4f58 100644 --- a/README.md +++ b/README.md @@ -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 +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. - + ## Normal use diff --git a/cmd/trello2mail/config.go b/cmd/trello2mail/config.go index 6976387..9ddea19 100644 --- a/cmd/trello2mail/config.go +++ b/cmd/trello2mail/config.go @@ -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")) diff --git a/cmd/trello2mail/main.go b/cmd/trello2mail/main.go index 9806ee7..63f70cf 100644 --- a/cmd/trello2mail/main.go +++ b/cmd/trello2mail/main.go @@ -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 diff --git a/cmd/trello2mail/trello.go b/cmd/trello2mail/trello.go index 40db207..dcbd837 100644 --- a/cmd/trello2mail/trello.go +++ b/cmd/trello2mail/trello.go @@ -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=", + }, "\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