Wiki

Clone wiki

Gumper / Preparing a script for Gumper

Caveats

Gumper has one job: Run scripts. Gumper will return the results of a script as a json object, which is not as straightforward as it might sound. Gumper will return a json object with many attributes, one of which is the script result (scriptResult). Gumper will encode your script's results in one of (currently) three ways:

  1. If your script returns json-formatted text, Gumper will retain your json structure (this is the best route).
  2. If your script returns xml-formatted text, Gumper will convert the xml to a json object with the same structure.
  3. If your script returns some other format, it is treated as plain text. Although it will be converted to a json object, that really just means it will have all the proper escapes, etc., but will have no structure. In other words, it will be a string.

Format your output

Scripts can output a lot of excess information that you don't intend to be in the response Gumper sends. Take, for example:

#!php

Error: null pointer exception.
{"color" : "blue" }

Gumper will try to convert the above to a json object, which will fail because of the extra "Error: null pointer exception" error on the first line. There is a simple way around this. Gumper looks for two case-sensitive tags: [[OUTPUT]] and [[/OUTPUT]]. If Gumper finds the [[OUTPUT]] tag in your script's output, it will remove the tag and all preceding text. If Gumper finds the [[/OUPUT]] tag, it will remove the tag and all the subsequent text. This means, the best way to format your output is like so:

#!php

Error: null pointer exception.
[[OUTPUT]]
{"color" : "blue"}
[[/OUTPUT]]

Gumper will remove the tags and the error message, so that your processed output looks like this:

#!php
{"color" : "blue"}

Spare yourself the work

Several scripting languages have built-in functions for converting an array or object to json. Check your languages documentation for such a function and avoid having to write something custom yourself. Here is an example for PowerShell:

$files = gci | convertto-json#get all files in the current directory and convert the PowerShell object to json
write-host "[[OUTPUT]]"
$files#writes the object as json automatically
$write-host "[[/OUTPUT]]"

Why run gci (Get-ChildItem) and then write it out on a different line? Why not just do it all in a one-liner between the [[OUTPUT]] tags? That's quicker, right? True, but what if your Get-ChildItem command generates an error? That error will be included in the script's results and your results won't validate as json. Instead, they will get treated as a string and you probably won't be able to use the response the way you hope. By doing this in two steps, you can guarantee that only the content you want is included in the script's results.

Conclusion

With these two tips, you should be able to quickly prepare existing scripts for Gumper. Just remember to:

  • Wrap your intended output with the [[OUTPUT]] and [[/OUTPUT]] tags (case-sensitive).
  • Use a built-in function to convert your intended output to json.

Updated