Skip to content

Commit

Permalink
completed requester interface for get task
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticrabbit committed Nov 10, 2021
1 parent 2a55898 commit ea9b360
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 4 additions & 2 deletions cmd/task.go → cmd/gettask.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_<taskID>.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_<taskID>.json")

}
5 changes: 5 additions & 0 deletions internal/gettask.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"
"os"

"github.com/spf13/viper"
)

type TaskRequest struct {
Expand All @@ -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")
Expand Down
20 changes: 11 additions & 9 deletions internal/requester.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package internal

import (
"fmt"
"io/ioutil"
"log"
"net/http"
Expand All @@ -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{}

Expand All @@ -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)
}

0 comments on commit ea9b360

Please sign in to comment.