Blog

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 google.com -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
510C13BF97C8FAFE976EBD976E3B328E

CommandBox> #listGetAt www.foo.com 2 . | #ucase | #reverse
OOF

 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]
2

CommandBox> package list --JSON | #structFind dependencies | #structKeyArray
[
    "coldbox",
    "cbmessagebox",
    "cbi18n"
]

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

ColdBox 7.2.0 Released

ColdBox 7.2.0 Released

ColdBox, a widely used development platform for ColdFusion (CFML), has unveiled version 7.2. Packed with compelling new features, bug fixes, and enhancements, this release is designed to empower developers by boosting productivity, refining scheduled task capabilities, and enhancing the overall reliability and efficiency of application development. This article will delve into the key highlights of ColdBox 7.2 and elucidate how these advancements can positively impact developers in their daily coding endeavors.

Luis Majano
Luis Majano
November 20, 2023
Into the Box 2023 Series on CFCast

Into the Box 2023 Series on CFCast

Excitement is in the air as we unleash the highly anticipated ITB 2023 series exclusively for our valued CFCast subscribers – and the best part? It's FREE for CFCast members! Now is the perfect time if you haven't joined the CFCast community yet. Plus, we've got an incredible End-of-Year deal that's too good to miss

Maria Jose Herrera
Maria Jose Herrera
November 20, 2023
Ortus Deals are Finally Here!

Ortus Deals are Finally Here!

The much-anticipated Ortus End-of-the-Year Sale has arrived, and it's time to elevate your development experience! Whether you're a seasoned developer, a tech enthusiast, or someone on the lookout for top-notch projects, Ortus has something special in store for you. Brace yourself for incredible discounts across a wide array of products and services, including Ortus annual events, books, cutting-edge services, and more.

Maria Jose Herrera
Maria Jose Herrera
November 15, 2023