Skip to content

Commit

Permalink
Merge pull request #11 from buzzy/main
Browse files Browse the repository at this point in the history
Minor improvements and bug-fixes
  • Loading branch information
buzzy authored Sep 19, 2022
2 parents 8224838 + ec6155c commit 653a7a4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
29 changes: 20 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"

Expand All @@ -18,13 +19,21 @@ import (

type Parameter struct {
varType string
orgName string
description string
required bool
enum []any
location string
value *string
}

//Rename flags with odd names that causes issues in some shells
func renameFlag(name string) string {
m := regexp.MustCompile(`\[(.*)\]`)

return m.ReplaceAllString(name, "-$1")
}

func parseCommand(format string, verbose bool) {

doc := loadAPI()
Expand Down Expand Up @@ -63,14 +72,15 @@ func parseCommand(format string, verbose bool) {
parameter.Value.Schema.Value.Type == "integer" ||
parameter.Value.Schema.Value.Type == "array" {

flags[parameter.Value.Name] = Parameter{
flags[renameFlag(parameter.Value.Name)] = Parameter{
location: parameter.Value.In,
varType: "string",
orgName: parameter.Value.Name,
required: parameter.Value.Required,
value: new(string),
}

subFlag.StringVar(flags[parameter.Value.Name].value, parameter.Value.Name, "", parameter.Value.Description)
subFlag.StringVar(flags[renameFlag(parameter.Value.Name)].value, renameFlag(parameter.Value.Name), "", parameter.Value.Description)

continue
}
Expand Down Expand Up @@ -191,11 +201,11 @@ func parseCommand(format string, verbose bool) {
switch parameter.location {
case "query":
//This flag value should be sent as a query parameter
query.Add(name, *parameter.value)
query.Add(parameter.orgName, *parameter.value)

case "path":
//This flag value goes in the URI
uri = strings.Replace(uri, "{"+name+"}", *parameter.value, 1)
uri = strings.Replace(uri, "{"+parameter.orgName+"}", *parameter.value, 1)
}

}
Expand Down Expand Up @@ -419,11 +429,7 @@ func callAPI(method string, uri string, query url.Values, body string, contentTy
fmt.Println(string(resBody))
}

switch res.StatusCode {
case 400:
showError(resBody)

case 404:
if res.StatusCode >= 300 {
showError(resBody)
}

Expand All @@ -444,6 +450,11 @@ func callAPI(method string, uri string, query url.Values, body string, contentTy
break
}

//If data is empty, just send empty array
if len(response.Search("data").Children()) == 0 && len(response.Search("data").ChildrenMap()) == 0 {
break
}

arrayResponse := response.Exists("data", "0")

newItems := parseData(response)
Expand Down
7 changes: 4 additions & 3 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ func printHelpCommand(command string) {
enum = parameter.Value.Schema.Value.Items.Value.Enum
}

flags[parameter.Value.Name] = Parameter{
flags[renameFlag(parameter.Value.Name)] = Parameter{
varType: parameter.Value.Schema.Value.Type,
description: parameter.Value.Description,
description: renameFlag(parameter.Value.Description),
required: parameter.Value.Required,
enum: enum,
}
Expand Down Expand Up @@ -285,6 +285,7 @@ func printHelpCommand(command string) {
varType = "LIST"
}

completeColor := "-" + flg + colorBlue + "=" + varType + colorReset
complete := "-" + flg + "=" + varType

//TODO: IF DESCRIPTION INCLUDES LINK, CONVERT IT TO A HTTP LINK TO THE DOCS
Expand All @@ -294,7 +295,7 @@ func printHelpCommand(command string) {
description = description + colorRed + " [*required]" + colorReset
}

fmt.Println(" ", complete, strings.Repeat(" ", maxLength-len(complete)+1), description)
fmt.Println(" ", completeColor, strings.Repeat(" ", maxLength-len(complete)+1), description)

if flags[flg].enum != nil {

Expand Down

0 comments on commit 653a7a4

Please sign in to comment.