Building network automation solutions

9 module online course

Start now!

The Never-Ending Story of CLI or API

Over the last weekend I almost got pulled into yet-another CLI-or-automation Twitter spat. The really sad part: I thought we were past that point. After all, I’ve been ranting about that topic for almost seven years… and yet I’m still hearing the same arguments I did in those days.

Just for the giggles I collected a few old blog posts on the topic (not that anyone evangelizing their opinions on Twitter would ever take the time to read them ;).

The basics:

CLI-or-API dilemma:

Real-life considerations:

Meanwhile on planet Earth:

Finally:

For those of you still loving Bash CLI

Fun fact: It took me less than 10 minutes to put together the above list using Bash on OSX (including figuring out the pipelines to get it done). Maybe there’s some value to using CLI after all ;)

Here’s what I did:

A) Get a list of blog posts mentioning CLI (a piece of cake after migrating the blog into a Git-backed directory tree):

cd $DIRECTORY/content/posts 
ack -l CLI

B) Limit the list to blog posts published between 2016 and 2019:

ack -l CLI|ack '201[6-9]'

C) Open all those blog posts in a web browser to see what I wrote in each one:

ack -l CLI|ack '201[5-9]'| \
  sort| \
  xargs -n 1 -I % open https://blog.ipspace.net/%

Of course I also wanted to automate generation of Markdown text pointing to each blog post.

D) Write a Bash script to extract title from a blog post and return a bulleted line in Markdown:

URL=$1
TITLE=$(wget -qO- $URL|\
  hxnormalize -x|\
  hxselect -c head title|\
  sed -e 's/ «.*//')
echo '*' "[$TITLE]($URL)"

Short explanation:

  • wget fetches the web page contents
  • hxnormalize (installed with brew install html-xml-utils) normalizes HTML, -x option returns the document in XML format
  • hxselect reads an XML document and returns elements matching a CSS selector (head title), or their content when used with -c option.
  • sed strips the fixed part at the end of page title.

E) Use the Bash script together with paste/copy OSX utilities to transform URL (on clipboard) into Markdown text (on clipboard)

pbpaste|xargs get-title.sh|pbcopy

Final workflow:

  • Find an interesting article
  • Copy article URL
  • Switch to Bash window, up-arrow, enter
  • Switch to IA Writer, paste
  • Lather, rinse, repeat.
Add comment
Sidebar