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

Thanksgiving Day deals: Access Exclusive Discounts Until November 30th!

Thanksgiving Day deals: Access Exclusive Discounts Until November 30th!

Prepare for the Ortus Thanksgiving Day Deals! This is your chance to enhance your development journey with amazing deals catered to everyone—from experienced developers to tech aficionados. Our unique offers are crafted to assist you in reaching your objectives while conserving your budget!

Don't Let These Incredible Offers Slip Away!

Utilize the code THKSGIVING24 at checkout to take advantage of these fantastic discounts before they end...

Cristobal Escobar
Cristobal Escobar
November 04, 2024
BoxLang Newsletter: October 2024

BoxLang Newsletter: October 2024

Welcome to the October edition of the BoxLang Newsletter! **This month has been packed with exciting updates, new features, and enhancements that continue to shape BoxLang into a robust and versatile programming language. In this edition, we highlight the latest beta releases, new integrations, and key features that have been introduced.

Maria Jose Herrera
Maria Jose Herrera
November 04, 2024
BoxLang 1.0.0 Beta 21 Launched

BoxLang 1.0.0 Beta 21 Launched

This release brings another round of powerful tools and refinements to the BoxLang community, making development more dynamic and robust than ever. We’ve added new capabilities for debugging and tracing, expanded context-sensitive controls for thread management, and introduced new methods for fluent attachment handling.

For deeper flexibility, our improvements enhance configurability, streamline session control, and add deeper levels of JSON serialization management. Plus, we’ve squashed a wide range of bugs, enhancing stability across database connections, date handling, and runtime compatibility with CFML.

In addition, CSRF Token functionality is now provided via the bx-csrf module.

Luis Majano
Luis Majano
November 01, 2024