From d7311e23e8eeabce6858d2f95847f3f396d61d42 Mon Sep 17 00:00:00 2001 From: Tom Riley Date: Sat, 13 Nov 2021 12:13:54 -0600 Subject: [PATCH 1/4] change ctoken name to token --- cmd/logout.go | 2 +- cmd/root.go | 4 ++-- internal/requester.go | 4 +--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/cmd/logout.go b/cmd/logout.go index ec19e9b..67ca8a4 100644 --- a/cmd/logout.go +++ b/cmd/logout.go @@ -13,7 +13,7 @@ var logoutCmd = &cobra.Command{ Long: `logout allows the user to delete the access token for accessing a Clickup workspace`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("deleted authentication token") - viper.Set("ctoken", "") + viper.Set("token", "") viper.WriteConfigAs(config_file) }, } diff --git a/cmd/root.go b/cmd/root.go index 4f9193a..b242458 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -62,12 +62,12 @@ func initConfig() { } func checkToken() { - if !viper.InConfig("ctoken") || viper.GetString("ctoken") == "" { + if !viper.InConfig("token") || viper.GetString("token") == "" { token, err := internal.GetToken() if err != nil { log.Fatalln("auth failed") } - viper.Set("cToken", token) + viper.Set("Token", token) viper.WriteConfigAs(config_file) } } diff --git a/internal/requester.go b/internal/requester.go index 553f013..5df0f18 100644 --- a/internal/requester.go +++ b/internal/requester.go @@ -29,10 +29,8 @@ func init() { //Gets JSON data for any struct that implements Requester interface func getJSON(apiPath string) []byte { - token := viper.GetString("ctoken") req, _ := http.NewRequest(http.MethodGet, apiPath, nil) - - req.Header.Add("Authorization", token) + req.Header.Add("Authorization", viper.GetString("token")) req.Header.Add("Content-Type", "application/json") resp, err := Client.Do(req) if err != nil { From fa7f39059bb3e891d9d8a92dfa914ec669a1d243 Mon Sep 17 00:00:00 2001 From: Tom Riley Date: Sat, 13 Nov 2021 12:18:47 -0600 Subject: [PATCH 2/4] added token to set command for manual config --- cmd/set.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/set.go b/cmd/set.go index 57fe075..da05454 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -20,7 +20,7 @@ var setCmd = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { - keys := []string{"team", "port"} + keys := []string{"team", "port", "token"} for _, key := range keys { x, _ := cmd.Flags().GetString(key) if x != "" { @@ -37,4 +37,5 @@ func init() { rootCmd.AddCommand(setCmd) setCmd.Flags().StringP("team", "", "", "set the Team ID") setCmd.Flags().StringP("port", "", "", "set the Redirect URL Port number") + setCmd.Flags().StringP("token", "", "", "set the Auth Token manually") } From 5a6cdcf1c2828c96f3cd3b46047fd8b5632e66b2 Mon Sep 17 00:00:00 2001 From: Tom Riley Date: Sat, 13 Nov 2021 12:35:40 -0600 Subject: [PATCH 3/4] updated usage to include set token --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bf89f85..f688f02 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ export CLICKUP_CLIENT_SECRET=xxx export CLICKUP_PORT=9999 ``` -## SET command +### SET command 1. You can optionally set the local host port with the set command: ``` @@ -22,6 +22,8 @@ clickup set --team=1234567 ``` 1. Both parameters can be set in the same command, or provided as environment variables with the "CLICKUP_" prefix. +1. You can additionally set the token manually (--token), to use a personal token or for environments without a browser + ## Usage ### Authentication From 044e921d8d7ece0a5c8562d9f3d5e9607ca9f78a Mon Sep 17 00:00:00 2001 From: Tom Riley Date: Sun, 14 Nov 2021 15:43:16 -0600 Subject: [PATCH 4/4] format JSON output --- cmd/gettask.go | 7 ++++++- internal/requester.go | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cmd/gettask.go b/cmd/gettask.go index 471a0c6..0073682 100644 --- a/cmd/gettask.go +++ b/cmd/gettask.go @@ -3,6 +3,7 @@ package cmd import ( "errors" "fmt" + "log" "strings" "github.com/fantasticrabbit/ClickupCLI/internal" @@ -33,7 +34,11 @@ var taskCmd = &cobra.Command{ TeamID: viper.GetString("team"), Subtasks: viper.GetBool("subtasks"), } - fmt.Println(string(t.GetJSON(t.BuildPath()))) + x, err := internal.FormatJSON(string(t.GetJSON(t.BuildPath()))) + if err != nil { + log.Fatalln(err) + } + fmt.Println(x, err) }, } diff --git a/internal/requester.go b/internal/requester.go index 5df0f18..2a635c5 100644 --- a/internal/requester.go +++ b/internal/requester.go @@ -1,6 +1,8 @@ package internal import ( + "bytes" + "encoding/json" "io/ioutil" "log" "net/http" @@ -40,3 +42,11 @@ func getJSON(apiPath string) []byte { resp_body, _ := ioutil.ReadAll(resp.Body) return resp_body } + +func FormatJSON(str string) (string, error) { + var formattedJSON bytes.Buffer + if err := json.Indent(&formattedJSON, []byte(str), "", " "); err != nil { + return "", err + } + return formattedJSON.String(), nil +}