Friday, April 04, 2014

One liners

So I've always been a fan of the unix philosophy with a passion for trying to do most things in one line of/with Bash rather than a full script or program. I am by no means adept but battle through with sed, awk, paste, cut, tr, sort etc... So when one can potentially use curl on a RESTful service, and stay on the command line rather than logging in to a web app, I'll give it a go.

We use Saasu for our back-end invoicing and reconciliation and they provide access to their API via a secret key (tied to configurable users). I decided I wanted to see what we were owed and owing via the command line so with the help of some other simple tools I came up with the below. It's still a work in progress and I'm open to all the help and any suggestions I can get ;)

First you may need to ensure you have 'xmlstarlet', 'dialog', and 'cURL' installed on your *nix system via ports, apt, or otherwise. Ensure they are happily found in your $PATH. Then replace the below with your Saasu secret/access key 'XXXXXXXXXXXXXXXX' (preferably from a read only user account) and the file ID 'YYYYY' of your desired Saasu account. You can find out how to enable the web services API from Saasu here.

Note: I actually ended up putting the below one liners in respective files and calling them (but you can alias it just as easily... I just didn't want to have to keep sourcing it while testing/editing)... and bingo you have a command (you can call whatever you like).

Owed (the following is one single line):
dialog --title "Company Accounts Receivable" --msgbox "`echo -e "\r\n\r\n" && curl -s "https://secure.saasu.com/webservices/rest/r1/invoicelist?wsaccesskey=XXXXXXXXXXXXXXXXXXXX&FileUid=YYYYY&&transactiontype=s&PaidStatus=unpaid"  | xmlstarlet sel -t -m //invoiceListItem -o "Invoice #" -v invoiceNumber -o " is/was due by " -v dueDate -o " for " -v amountOwed -o " " -v ccy -o " by " -v contactOrganisationName -n | sort -n -k2 && echo -e "\r\n\r\n"`" 60 100 ; clear

Owing (the following is one single line):
dialog --title "Company Accounts Payable" --msgbox "`echo -e "\r\n\r\n" && curl -s "https://secure.saasu.com/webservices/rest/r1/invoicelist?wsaccesskey=XXXXXXXXXXXXXXXXXXXX&FileUid=YYYYY&&transactiontype=p&PaidStatus=unpaid"  | xmlstarlet sel -t -m //invoiceListItem -o "Invoice #" -v invoiceNumber -o " is/was due by " -v dueDate -o " for " -v amountOwed -o " " -v ccy -o " to " -v contactOrganisationName -n | sort -n -k2 && echo -e "\r\n\r\n"`" 60 100 ; clear

So now I just type 'owed' or 'owing' on the command line to get:


I know there's a lot more that could be done, tweaked, improved, and extended... so let me know what you're thinking via @irldexter on twitter if you want to get in touch!