From ea9b360b9d809b845eb9132667b5ea4f75a17bef Mon Sep 17 00:00:00 2001 From: Tom Riley Date: Wed, 10 Nov 2021 16:50:54 -0600 Subject: [PATCH] completed requester interface for get task --- cmd/{task.go => gettask.go} | 6 ++++-- internal/gettask.go | 5 +++++ internal/requester.go | 20 +++++++++++--------- 3 files changed, 20 insertions(+), 11 deletions(-) rename cmd/{task.go => gettask.go} (96%) diff --git a/cmd/task.go b/cmd/gettask.go similarity index 96% rename from cmd/task.go rename to cmd/gettask.go index a758c8a..b484ba6 100644 --- a/cmd/task.go +++ b/cmd/gettask.go @@ -28,13 +28,15 @@ var taskCmd = &cobra.Command{ TeamID: viper.GetString("team_id"), Subtasks: subtasksFlag, } - internal.GetJSON(t) + t.WriteOut(t.GetJSON(t.BuildPath())) + }, } func init() { getCmd.AddCommand(taskCmd) - taskCmd.Flags().BoolP("file", "f", false, "output to file clickup_.json") taskCmd.Flags().BoolP("custom", "c", false, "task id provided is a clickup custom task id") taskCmd.Flags().BoolP("subtasks", "s", false, "include subtasks in output") + taskCmd.Flags().BoolP("file", "f", false, "output to file clickup_.json") + } diff --git a/internal/gettask.go b/internal/gettask.go index 87fcdd3..80e1e05 100644 --- a/internal/gettask.go +++ b/internal/gettask.go @@ -4,6 +4,8 @@ import ( "fmt" "log" "os" + + "github.com/spf13/viper" ) type TaskRequest struct { @@ -25,6 +27,9 @@ func (t TaskRequest) BuildPath() string { } func (t TaskRequest) WriteOut(payload []byte) { + if !viper.GetBool("file") { + fmt.Println(string(payload)) + } err := os.WriteFile("clickup_"+t.TaskID+".json", payload, 0644) if err != nil { log.Fatalln("Error writing task JSON") diff --git a/internal/requester.go b/internal/requester.go index 304c9b7..647879a 100644 --- a/internal/requester.go +++ b/internal/requester.go @@ -1,7 +1,6 @@ package internal import ( - "fmt" "io/ioutil" "log" "net/http" @@ -12,12 +11,12 @@ import ( //Requester interface needs an API path to request JSON data type Requester interface { BuildPath() string + GetJSON(string) []byte WriteOut([]byte) } //Gets JSON data for any struct that implements Requester interface -func GetJSON(r Requester) { - apiPath := r.BuildPath() +func getJSON(apiPath string) []byte { token := viper.GetString("ctoken") client := &http.Client{} @@ -32,10 +31,13 @@ func GetJSON(r Requester) { defer resp.Body.Close() resp_body, _ := ioutil.ReadAll(resp.Body) - fileFlag := viper.GetBool("file") - if !fileFlag { - fmt.Println(string(resp_body)) - } else { - r.WriteOut(resp_body) - } + return resp_body + // fileFlag := viper.GetBool("file") + // if !fileFlag { + // fmt.Println(string(resp_body)) + // } +} + +func (t TaskRequest) GetJSON(apiPath string) []byte { + return getJSON(apiPath) }