diff --git a/command.go b/command.go index 5b78114..0e74848 100644 --- a/command.go +++ b/command.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "os" + "regexp" "strconv" "strings" @@ -18,6 +19,7 @@ import ( type Parameter struct { varType string + orgName string description string required bool enum []any @@ -25,6 +27,13 @@ type Parameter struct { 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() @@ -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 } @@ -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) } } @@ -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) } @@ -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) diff --git a/help.go b/help.go index 1c248e2..c651216 100644 --- a/help.go +++ b/help.go @@ -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, } @@ -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 @@ -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 {