Skip to content

Commit

Permalink
Add option to pretty-print JSON command output. Add trailing newlines…
Browse files Browse the repository at this point in the history
… to command output. (#150)

This commit adds a the --pretty option to format any JSON output with a 4-space indent using golang's json.MarshalIndent, making certain output easier to read when working manually on the command-line.

While updating a few printf calls, I also noticed a few commands that I believe were unintentionally omitting a trailing newline, so that after running those commands the user's command prompt would be at the end of the output of the previous command.  In principle this *could* break scripted commands that assume no newline? Hard to imagine...

Co-authored-by: danielpaulus <[email protected]>
  • Loading branch information
briankrznarich and danielpaulus authored Jul 27, 2022
1 parent 4604203 commit 96f7026
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (

//JSONdisabled enables or disables output in JSON format
var JSONdisabled = false
var prettyJSON = false

func main() {
Main()
Expand Down Expand Up @@ -108,7 +109,8 @@ Usage:
Options:
-v --verbose Enable Debug Logging.
-t --trace Enable Trace Logging (dump every message).
--nojson Disable JSON output (default).
--nojson Disable JSON output
--pretty Pretty-print JSON command output
-h --help Show this screen.
--udid=<udid> UDID of the device.
Expand Down Expand Up @@ -197,6 +199,11 @@ The commands work as following:
log.SetFormatter(&log.JSONFormatter{})
}

pretty, _ := arguments.Bool("--pretty")
if pretty{
prettyJSON = true
}

traceLevelEnabled, _ := arguments.Bool("--trace")
if traceLevelEnabled {
log.Info("Set Trace mode")
Expand Down Expand Up @@ -627,11 +634,11 @@ func mobileGestaltCommand(device ios.DeviceEntry, arguments docopt.Opts) bool {
plist, _ := arguments.Bool("--plist")
resp, _ := conn.MobileGestaltQuery(keys)
if plist {
fmt.Printf("%s", ios.ToPlist(resp))
fmt.Printf("%s\n", ios.ToPlist(resp))
return true
}
jb, _ := json.Marshal(resp)
fmt.Printf("%s", jb)
jb, _ := marshalJSON(resp)
fmt.Printf("%s\n", jb)
return true
}
return b
Expand Down Expand Up @@ -759,7 +766,7 @@ func deviceState(device ios.DeviceEntry, list bool, enable bool, profileTypeId s
if JSONdisabled {
outputPrettyStateList(profileTypes)
} else {
b, err := json.Marshal(profileTypes)
b, err := marshalJSON(profileTypes)
exitIfError("failed json conversion", err)
println(string(b))
}
Expand Down Expand Up @@ -1264,15 +1271,23 @@ func readPair(device ios.DeviceEntry) {
if err != nil {
exitIfError("failed reading pairrecord", err)
}
json, err := json.Marshal(record)
json, err := marshalJSON(record)
if err != nil {
exitIfError("failed converting to json", err)
}
fmt.Printf("%s", json)
fmt.Printf("%s\n", json)
}

func marshalJSON(data interface{}) ([]byte, error){
if prettyJSON{
return json.MarshalIndent(data,""," ")
}else{
return json.Marshal(data)
}
}

func convertToJSONString(data interface{}) string {
b, err := json.Marshal(data)
b, err := marshalJSON(data)
if err != nil {
fmt.Println(err)
return ""
Expand Down

0 comments on commit 96f7026

Please sign in to comment.