Skip to content

Commit

Permalink
added writeout function to requester interface to generalize file-bas…
Browse files Browse the repository at this point in the history
…ed output
  • Loading branch information
fantasticrabbit committed Nov 10, 2021
1 parent e2d5807 commit 09c7e72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
15 changes: 1 addition & 14 deletions cmd/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package cmd

import (
"errors"
"fmt"
"log"
"os"
"strings"

"github.com/fantasticrabbit/ClickupCLI/internal"
Expand All @@ -31,17 +28,7 @@ var taskCmd = &cobra.Command{
TeamID: viper.GetString("team_id"),
Subtasks: subtasksFlag,
}
data := internal.GetJSON(t)

fileFlag, _ := cmd.Flags().GetBool("file")
if !fileFlag {
fmt.Println(string(data))
} else {
err := os.WriteFile("clickup_"+t.TaskID+".json", data, 0644)
if err != nil {
log.Fatalln("Error writing task JSON")
}
}
internal.GetJSON(t)
},
}

Expand Down
9 changes: 9 additions & 0 deletions internal/gettask.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package internal

import (
"fmt"
"log"
"os"
)

type TaskRequest struct {
Expand All @@ -21,3 +23,10 @@ func (t TaskRequest) BuildPath() string {
t.TaskID, t.CustomTask, t.TeamID, t.Subtasks)
}
}

func (t TaskRequest) WriteOut(payload []byte) {
err := os.WriteFile("clickup_"+t.TaskID+".json", payload, 0644)
if err != nil {
log.Fatalln("Error writing task JSON")
}
}
11 changes: 9 additions & 2 deletions internal/requester.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"fmt"
"io/ioutil"
"log"
"net/http"
Expand All @@ -11,10 +12,11 @@ import (
//Requester interface needs an API path to request JSON data
type Requester interface {
BuildPath() string
WriteOut([]byte)
}

//Gets JSON data for any struct that implements Requester interface
func GetJSON(r Requester) []byte {
func GetJSON(r Requester) {
apiPath := r.BuildPath()
token := viper.GetString("ctoken")
client := &http.Client{}
Expand All @@ -30,5 +32,10 @@ func GetJSON(r Requester) []byte {
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)

return resp_body
fileFlag := viper.GetBool("file")
if !fileFlag {
fmt.Println(string(resp_body))
} else {
r.WriteOut(resp_body)
}
}

0 comments on commit 09c7e72

Please sign in to comment.