Skip to content

Commit

Permalink
Merge pull request #12 from jakobilobi/dev-v1.0.2
Browse files Browse the repository at this point in the history
v1.0.2
  • Loading branch information
jakobilobi authored Aug 22, 2024
2 parents 6c5bcb3 + 69275e3 commit d5a3f9b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.1
1.0.2
33 changes: 22 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ var (
insecure bool

// Output flags
responseOnly bool
showVersion bool
rawOutput bool
quietOutput bool
showVersion bool

// Verbosity flags
basic bool
Expand All @@ -67,8 +68,8 @@ func init() {

flag.BoolVar(&insecure, "insecure", false, "Open an insecure WS connection in the case of no scheme being present in the input URL.")

// TODO: add flag for binary output, to allow piping to other commands
flag.BoolVar(&responseOnly, "ro", false, "Response only; print only the response. Has no effect if there's no expected response.")
flag.BoolVar(&rawOutput, "raw", false, "Force output to be the raw data of the response.")
flag.BoolVar(&quietOutput, "quiet", false, "Quiet all output but the response.")
flag.BoolVar(&showVersion, "version", false, "Print the version.")

flag.BoolVar(&basic, "b", false, "Print only basic output.")
Expand Down Expand Up @@ -120,7 +121,14 @@ func main() {
if err != nil {
handleConnectionError(err, url.String())
}
// TODO: add automatic decoding of detected byte response
if !rawOutput {
decodedMessage := make(map[string]interface{})
err := json.Unmarshal(response.([]byte), &decodedMessage)
if err != nil {
log.Fatalf("Error unmarshalling JSON message: %v", err)
}
response = decodedMessage
}
} else if jsonMessage != "" {
encodedMessage := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonMessage), &encodedMessage)
Expand Down Expand Up @@ -152,8 +160,8 @@ func main() {
}
}

// Print the results if there is no expected response or if the responseOnly flag is not set
if !responseOnly || (jsonMessage == "" && jsonMethod == "" && textMessage == "") {
// Print the results if there is no expected response or if the quietOutput flag is not set
if !quietOutput {
// Print details of the request
printRequestDetails(result)

Expand Down Expand Up @@ -303,14 +311,17 @@ func printResponse(response interface{}) {
return
}
baseMessage := colorWSOrange("Response") + ": "
if responseOnly {
if quietOutput {
baseMessage = ""
} else {
fmt.Println()
}
if responseMap, ok := response.(map[string]interface{}); ok {
if rawOutput {
// If raw output is requested, print the raw data before trying to assert any types
fmt.Printf("%s%v\n", baseMessage, response)
} else if responseMap, ok := response.(map[string]interface{}); ok {
// If JSON in request, print response as JSON
if jsonMessage != "" || jsonMethod != "" {
if _, isJSON := responseMap["jsonrpc"]; isJSON || jsonMessage != "" || jsonMethod != "" {
responseJSON, err := json.Marshal(responseMap)
if err != nil {
fmt.Printf("Could not marshal response to JSON. Response: %v, error: %v", responseMap, err)
Expand All @@ -325,7 +336,7 @@ func printResponse(response interface{}) {
} else if responseBytes, ok := response.([]byte); ok {
fmt.Printf("%s%v\n", baseMessage, responseBytes)
}
if !responseOnly {
if !quietOutput {
fmt.Println()
}
}
Expand Down

0 comments on commit d5a3f9b

Please sign in to comment.