Skip to content

Commit

Permalink
use token at config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Sep 21, 2018
1 parent ed7a1a0 commit ce37a6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
7 changes: 2 additions & 5 deletions cmd/todoist/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import (
"bufio"
"encoding/json"
"fmt"
"github.com/kobtea/go-todoist/cmd/util"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"path"
"strings"
)

type config struct {
Token string `json:"token"`
}

// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Expand All @@ -26,7 +23,7 @@ var configCmd = &cobra.Command{
return err
}
}
var c config
var c util.Config
file := path.Join(dir, "config.json")
if b, err := ioutil.ReadFile(file); err != nil {
// initial config
Expand Down
25 changes: 24 additions & 1 deletion cmd/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,37 @@ package util

import (
"context"
"encoding/json"
"github.com/kobtea/go-todoist/todoist"
"github.com/spf13/viper"
"io/ioutil"
"os"
)

type Config struct {
Token string `json:"token"`
}

func resolveToken() string {
if s := viper.GetString("TODOIST_TOKEN"); len(s) != 0 {
return s
}
file := os.ExpandEnv("$HOME/.go-todoist/config.json")
if b, err := ioutil.ReadFile(file); err != nil {
return ""
} else {
var c Config
if err = json.Unmarshal(b, &c); err != nil {
return ""
}
return c.Token
}
}

func NewClient() (*todoist.Client, error) {
return todoist.NewClient(
"",
viper.GetString("TODOIST_TOKEN"),
resolveToken(),
"*",
"",
nil)
Expand Down

0 comments on commit ce37a6b

Please sign in to comment.