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

BoxLang 1.0.0 Beta 23 Launched

BoxLang 1.0.0 Beta 23 Launched

The latest release of BoxLang, Beta 23, marks a significant step forward in our journey to create the ultimate dynamic language for the JVM. Packed with powerful new features, important bug fixes, and thoughtful optimizations, this update is designed to make your development experience smoother, faster, and more reliable, especially after now starting to take 100s of comments and bug reports from our community.

Luis Majano
Luis Majano
November 23, 2024
TestBox Latest Updates and News!

TestBox Latest Updates and News!

We’re thrilled to have launched the new TestBox website and TestBox 6.0! If you haven’t had a chance to explore yet, visit TestBox to discover updated documentation, powerful resources, and features that make testing more efficient than ever.

Maria Jose Herrera
Maria Jose Herrera
November 21, 2024
Is Your ColdFusion Application Ready for the Future?

Is Your ColdFusion Application Ready for the Future?

In a rapidly evolving digital world, maintaining performance, security, and scalability for ColdFusion applications is more challenging than ever. Whether you're using Lucee or Adobe ColdFusion, legacy systems can become a bottleneck for growth, innovation, and user satisfaction. The need to future-proof your ColdFusion applications has never been more critical.

But where do you start?


The Hidden Costs of an Outdated ColdFusion Application

As you...

Cristobal Escobar
Cristobal Escobar
November 21, 2024