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 7 Launched

BoxLang 1.0.0 Beta 7 Launched

We are pleased to announce the release of BoxLang 1.0.0-Beta 7! This latest beta version includes improvements and essential bug fixes, but more importantly it certifies the execution of ColdBox HMVC and TestBox.

What is BoxLang?

BoxLang is a modern dynamic JVM language that can be deployed on multiple runtimes: operating system (Windows/Mac/*nix/Embedded), web server, lambda, iOS, android, web assembly, and more. BoxLang combines many features from different progr

Luis Majano
Luis Majano
July 26, 2024
New BoxLang Feature: Java Method References and Higher-Order Functions

New BoxLang Feature: Java Method References and Higher-Order Functions

We’ve added more goodies to our BoxLang Java interop: method references and higher-order functions. CFML has never let you do these things, making Java Interop feel like a second-class citizen. But with BoxLang, we’re elevating Java integration to a new level.

Maria Jose Herrera
Maria Jose Herrera
July 26, 2024
Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Are you a CFML developer looking to take your skills to the next level? Look no further than the ColdBox from Zero to Hero Virtual Live Training! This intensive two-day course will equip you with the knowledge and expertise to build robust and scalable applications using ColdBox 7, the latest version of the most popular CFML MVC framework.

What You'll Learn:

  • Master the Fun...

Cristobal Escobar
Cristobal Escobar
July 24, 2024