Brad Wood

December 23, 2016

Spread the word

Share your thoughts

Over the river and through the woods to grandmother's house we go.  There's a lot of ways to get to grandmother's house just like there a lot of ways to use CommandBox in your workflow. In today's edition of the 12 tips of (CommandBox) Christmas, we'll hopefully teach you some new tricks. Oh-- and while you're at grandma's, tell her to stay away from those reindeer!  

Command line integrations

You should already be familiar with CommandBox's command-based architecture that gives names to things you can perform, grouping similar commands into white space-delimited lists.

CommandBox> version
CommandBox> server start
CommandBox> coldbox reinit
CommandBox> coldbox create handler

Commands aren't the only you can run from the interactive shell of CommandBox though.

CFML files

Run several lines of code from a .cfm file using the execute command.  This can be a great way to script some CFML code from a cron job, or create a nice reusable CF script that doesn't require a running web server to kick off.  Your code will run on the internal version of Lucee that powers the CommandBox CLI.

CommandBox> execute test.cfm

# accessed as $1 and $2
CommandBox> execute test.cfm positional parameters

# accessed as $named and #as
CommandBox> execute file=test.cfm named=parameters as=well

Operating system commands/binaries

Chances are your workflow involves some other CLIs or DOS/Bash commands that you use.  Possibly to interact with Git, npm, or running services.  You can pass commands along to your native OS's shell to execute for you.  Just preface your string with an ! and everything after will be run by your operating system with the result returned to CommandBox.  This is better than cfexecute.  We actually pass the commands through your local interpreter so non-binaries like "dir" on Windows or aliases like "ll" on *nix still work.  If you use a custom shell like Zsh, you can even configure that.  Your OS commands will run in the same working directory as CommandBox so other CLIs like Git or npm should integrate seamlessly.

CommandBox> !myApp.exe
CommandBox> !git init
CommandBox> !dir
CommandBox> !netstat -pan
CommandBox> !npm ll
CommandBox> !ipconfig
CommandBox> !ping -c 4
CommandBox> !java -jar myLib.jar

CFML Functions

You're a CFML programmer, right?  You eat, drink, and breath CFML so it's probably the first thing in your head when dealing with problems. CommandBox caters to you by allowing you to run any valid CFML function directly from the CLI simply by preceding it with a #.  The function behaves just like a command and lets you pass in named or positional parameters.  Remember, you can pipe input between functions to build up some nice one-liners.

CommandBox> #now
{ts '2016-12-25 12:00:00'}

CommandBox> #hash mypass

CommandBox> #listGetAt 2 . | #ucase | #reverse

 And watch what happens when you deal with structs and arrays.  Their output is sent to the console as JSON, which you can pipe back into another CFML function!

CommandBox> #arrayAvg [1,2,3]

CommandBox> package list --JSON | #structFind dependencies | #structKeyArray

Put it all together

Some of these may just seem like interesting parlor tricks, but consider what you can do when you combine everything in this post!  This example runs the native java binary with the "-version" flag, splits the output into an array on line breaks, grabs the first line, and uses the stream editor to parse out just the version.  This runs on Windows because sed is part of the CommandBox CLI.

CommandBox> !java -version | #listToArray \n | #arrayFirst | sed 's/java version "(.*)"/\1/'

Hopefully you script kiddies out there have a few ideas that have just popped into your mind on how you might use some of this scripting goodness.  The sky is the limit so get creative!



Add Your Comment

Recent Entries

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Hackers demand a ransom to restore data from my ColdFusion web applications!

Unfortunately, we often hear this message from clients who thought it would never happen to them... until it did. Some believed they could delay the expense of Implementing ColdFusion security best practices for one year, while others were tempted to put it off for just a few months. However, in today's rapidly evolving digital landscape, the security of web applications, including ColdFusio...

Cristobal Escobar
Cristobal Escobar
April 16, 2024
Ortus March Newsletter

Ortus March Newsletter

Welcome to Ortus Solutions’ monthly roundup, where we're thrilled to showcase cutting-edge advancements, product updates, and exciting events! Join us as we delve into the latest innovations shaping the future of technology.

Maria Jose Herrera
Maria Jose Herrera
April 01, 2024
Into the Box 2024 Last Early Bird Days!

Into the Box 2024 Last Early Bird Days!

Time is ticking, with less than 60 days remaining until the excitement of Into the Box 2024 unfolds! Don't let this golden opportunity slip away; our exclusive Early Bird Pricing is here for a limited time only, available until March 31st. Why wait? Secure your seat now and take advantage of this steal!

Maria Jose Herrera
Maria Jose Herrera
March 20, 2024