Skip to content

Commit

Permalink
allow nil values to be sent upon post
Browse files Browse the repository at this point in the history
  • Loading branch information
quiquelhappy committed Oct 17, 2021
1 parent a3bd07b commit 14468b7
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 44 deletions.
25 changes: 21 additions & 4 deletions src/api/API.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package api
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
)

func Request(path string, data map[string]string, hash interface{}) (response io.Reader,err error) {
func Request(path string, data map[string]interface{}, hash interface{}) (response io.Reader,err error) {
var result io.Reader = nil
var body []byte
err = nil
client := &http.Client{}
if data != nil {
if data != nil && len(data) > 0 {
postString, err := json.Marshal(&data)
if err!=nil {
return nil, err
Expand All @@ -23,14 +25,17 @@ func Request(path string, data map[string]string, hash interface{}) (response io
}
req.Header.Set("Accept","application/json")
req.Header.Set("User-Agent","grifpkg/cli")
if hash!=nil {
req.Header.Set("Authorization","Bearer "+hash.(string))
}
res, err := client.Do(req)
if err!=nil {
return nil, err
}
defer func(Body io.ReadCloser) {
err = Body.Close()
}(res.Body)
body, err := ioutil.ReadAll(res.Body)
body, err = ioutil.ReadAll(res.Body)
if err!=nil {
return nil, err
}
Expand All @@ -42,15 +47,27 @@ func Request(path string, data map[string]string, hash interface{}) (response io
}
req.Header.Set("Accept","application/json")
req.Header.Set("User-Agent","grifpkg/cli")
if hash!=nil {
req.Header.Set("Authorization","Bearer "+hash.(string))
}
res, err := client.Do(req)
defer func(Body io.ReadCloser) {
err = Body.Close()
}(res.Body)
body, err := ioutil.ReadAll(res.Body)
body, err = ioutil.ReadAll(res.Body)
if err!=nil {
return nil, err
}
result = bytes.NewReader(body)
}
type Error struct {
Error interface{} `json:"error"`
}
var errorInfo Error = Error{}
errorReader := bytes.NewReader(body)
_ = json.NewDecoder(errorReader).Decode(&errorInfo)
if errorInfo.Error!=nil {
return result,errors.New(errorInfo.Error.(string))
}
return result, err
}
34 changes: 11 additions & 23 deletions src/api/Log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
Expand All @@ -24,24 +25,24 @@ const (

var currentSpinner *spinner.Spinner = spinner.New(spinner.CharSets[40],0)

func Ask(questions []*survey.Question, answers interface{}){
LogTool(Question, []Message{}, questions, answers)
func Ask(questions []*survey.Question, answers interface{}) (err error){
return LogTool(Question, []Message{}, questions, answers)
}

func Log(level LogLevel, messages []Message){
LogTool(level, messages, nil, nil)
func Log(level LogLevel, messages []Message) (err error){
return LogTool(level, messages, nil, nil)
}

func LogOne(level LogLevel, message string){
LogTool(level, []Message{
_ = LogTool(level, []Message{ // ignore error, logOne usually just logs info/warns, not user input
{
Value: message,
Color: nil,
},
}, nil, nil)
}

func LogTool(level LogLevel, messages []Message, questions interface{}, answers interface{}) {
func LogTool(level LogLevel, messages []Message, questions interface{}, answers interface{}) (err error) {
if level== Progress {
if currentSpinner.Delay==0 {
currentSpinner = spinner.New(spinner.CharSets[40], 50*time.Millisecond)
Expand Down Expand Up @@ -79,21 +80,10 @@ func LogTool(level LogLevel, messages []Message, questions interface{}, answers
}))
if err != nil {
answers = nil
LogTool(Warn, []Message{
{
Value: "error while expecting user input: "+err.Error(),
Color: nil,
},
}, nil, nil)
return
return err
}
} else {
LogTool(Warn, []Message{
{
Value: "survey started but no questions/answers were provided",
Color: nil,
},
}, nil, nil)
return errors.New("no questions were provided")
}
} else {
prefix := " ¿ "
Expand All @@ -109,7 +99,7 @@ func LogTool(level LogLevel, messages []Message, questions interface{}, answers
prefix = " ✓ "
c = color.New(color.FgHiGreen)
}
_, err := fmt.Fprintf(color.Output, " %s", c.SprintFunc()(prefix))
_, err = fmt.Fprintf(color.Output, " %s", c.SprintFunc()(prefix))
for _, message := range messages {
if message.Color==nil {
_, err = fmt.Fprintf(color.Output, " %s", message.Value)
Expand All @@ -118,9 +108,7 @@ func LogTool(level LogLevel, messages []Message, questions interface{}, answers
}
}
fmt.Print("\n")
if err != nil {
return
}
}
}
return err
}
11 changes: 7 additions & 4 deletions src/elements/project/Project.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (project Project) InstallAll() (release []Release, err error) {
}

func (project Project) ParseInstallString(string string, version interface{}) (resourceName string, resourceAuthor interface{}, versionTag interface{}){
if version == nil || (version != nil && strings.HasPrefix(fmt.Sprintf("%v", version),"^")) {
if version == nil || strings.HasPrefix(fmt.Sprintf("%v", version),"^") {
versionTag=nil
} else {
versionTag=fmt.Sprintf("%v", version)
Expand Down Expand Up @@ -226,7 +226,7 @@ func createProjectFile(path string) (project Project, err error){
Software string
}{}

api.Ask([]*survey.Question{
err = api.Ask([]*survey.Question{
{
Name: "name",
Prompt: &survey.Input{
Expand All @@ -253,11 +253,14 @@ func createProjectFile(path string) (project Project, err error){
Name: "software",
Prompt: &survey.Select{
Message: "Which server software are you using (if you are using a fork, select the closest parent)",
Options: []string{"@spigotmc/spigot","@spigotmc/bungeecord","@papermc/paper","@papermc/waterfall"},
Options: []string{"@spigotmc/spigot", "@spigotmc/bungeecord", "@papermc/paper", "@papermc/waterfall"},
Default: "@minecraft/java",
},
},
},&answers)
}, &answers)
if err != nil {
return Project{}, err
}

project.Name=answers.Name
project.Software=answers.Software
Expand Down
11 changes: 7 additions & 4 deletions src/elements/project/Release.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (release Release) GetResolvedDependencies() (dependencies map[string]Resour

func (release Release) ListSuggestions() (suggestions []urlSuggestion.UrlSuggestion, err error){
api.LogOne(api.Progress, "querying url suggestions")
request, err := api.Request("resource/release/suggestion/list/", map[string]string{
request, err := api.Request("resource/release/suggestion/list/", map[string]interface{}{
"release": release.Id,
}, nil)
if err!=nil{
Expand Down Expand Up @@ -96,7 +96,7 @@ func (release Release) GetDownloadable(suggestionIdFallback interface{}) (downlo
Selection string
}{}

api.Ask([]*survey.Question{
err := api.Ask([]*survey.Question{
{
Name: "selection",
Prompt: &survey.Select{
Expand All @@ -105,6 +105,9 @@ func (release Release) GetDownloadable(suggestionIdFallback interface{}) (downlo
},
},
}, &answers)
if err != nil {
return DownloadableRelease{}, err
}

if strings.HasPrefix(answers.Selection,"suggest another URL"){
return DownloadableRelease{}, errors.New("suggest a download URL here: https://grifpkg.com/suggest/"+release.Parent.(Resource).Id)
Expand Down Expand Up @@ -146,11 +149,11 @@ func (release Release) GetDownloadable(suggestionIdFallback interface{}) (downlo

var request interface{} = nil
if release.Parent != nil && release.Parent.(Resource).Paid {
request, err = api.Request("resource/release/download/", map[string]string{
request, err = api.Request("resource/release/download/", map[string]interface{}{
"release": release.Id,
}, session.GetHash())
} else {
request, err = api.Request("resource/release/download/", map[string]string{
request, err = api.Request("resource/release/download/", map[string]interface{}{
"release": release.Id,
}, nil)
}
Expand Down
6 changes: 3 additions & 3 deletions src/elements/project/Resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Resource struct {
func QueryResources(name string, author interface{}, service interface{}) ([]Resource, error) {
api.LogOne(api.Progress, "querying resources")
var err error = nil
var data = make(map[string]string, 0)
var data = make(map[string]interface{}, 0)
data["name"]=name
if author!=nil {
data["author"]=author.(string)
Expand All @@ -39,7 +39,7 @@ func QueryResources(name string, author interface{}, service interface{}) ([]Res

func (resource Resource) GetReleases() (releases []Release, err error){
api.LogOne(api.Progress, "getting releases")
request, err := api.Request("resource/release/list/", map[string]string{
request, err := api.Request("resource/release/list/", map[string]interface{}{
"resource":resource.Id,
}, nil)

Expand All @@ -53,7 +53,7 @@ func (resource Resource) GetReleases() (releases []Release, err error){

func (resource Resource) GetRelease(version interface{}, id interface{}) (releases Release, err error){
api.LogOne(api.Progress, "getting release")
var data map[string]string = make(map[string]string)
var data map[string]interface{} = make(map[string]interface{})
data["resource"] = resource.Id
if version!=nil {
data["version"] = version.(string)
Expand Down
6 changes: 3 additions & 3 deletions src/elements/session/LoginRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ type LoginRequest struct {

func Start() (loginRequest LoginRequest, err error){
api.LogOne(api.Progress,"starting login request")
request, err := api.Request("login/request/start/", map[string]string{}, nil)
request, err := api.Request("login/request/start/", make(map[string]interface{},0), nil)
loginRequest = LoginRequest{}
err = json.NewDecoder(request).Decode(&loginRequest)
return loginRequest, err
}

func (loginRequest *LoginRequest) Get() (err error){
api.LogOne(api.Progress,"updating login request")
request, err := api.Request("login/request/get/", map[string]string{
request, err := api.Request("login/request/get/", map[string]interface{}{
"request": loginRequest.Hash,
}, nil)
err = json.NewDecoder(request).Decode(&loginRequest)
Expand All @@ -33,7 +33,7 @@ func (loginRequest *LoginRequest) Get() (err error){

func (loginRequest *LoginRequest) Validate(githubToken string) (err error){
api.LogOne(api.Progress,"validating login request")
request, err := api.Request("login/request/validate/", map[string]string{
request, err := api.Request("login/request/validate/", map[string]interface{}{
"request": loginRequest.Hash,
"token": githubToken,
}, nil)
Expand Down
Loading

0 comments on commit 14468b7

Please sign in to comment.