Blog

Brad Wood

December 14, 2017

Spread the word


Share your thoughts

It's that time of year again.  Trees are lit, presents are being wrapped, and relatives are coming to visit.  That's right, it's time again for the 12 days of (CommandBox) Christmas-- 2017 edition!  We'll publish one invaluable hint from today until Christmas right here on the Ortus blog.  Consider it our early Christmas gift to you.

Stay on task

Right, so let's get going.  Today's tip are about CommandBox task runners.  These are now the easiest and most robust way to do CLI scripting in CFML.  What's that you say-- you thought CFML was only for web pages?? Not so!  With the advent of CommandBox, CFML is now capable of being run from the command line anywhere, without even needing a server to be installed!  

Those crusty batch files?  That shell script that takes a PhD to understand?  Those... shudder... Node scripts!  All of those are candidates for you to write in CFML.  Face it, you're faster and more familiar with doing things in CFML and you already have all the tools like database access, file manipulations, and familiar data structures.  Rewrite your cron jobs, spruce up your Ant builds, and automate your lazy coworkers with the power of CFML!

What is a Task Runner?

Don't worry one bit.  Firstly, task runners are fully documented right here:

https://ortus.gitbooks.io/commandbox-documentation/content/task-runners.html

Secondly tasks are very very simple.  In fact, we'll create one right now.  All you need is:

  1. A CFC file called "task.cfc"
  2. A method in the CFC called run()
  3. Just put the code you want to run inside the method!

Here's a super simple task runner that works.

component {
  function run() {
    print.greenLine( 'This is my first task!' );
  }
}

And now let's run it.  You don't need any servers running.  Just CD into the same folder as the task.cfc and run this:

CommandBox> task run
This is my first task!

What else can I do?

A lot. In fact, you can do anything that's possible in CFML.  Your tasks run on the Lucee engine that powers your CommandBox CLI so keep in mind that you can even take advantage of some Lucee features like optional semicolons in script and Lucee-specific functions.  Let's take a look at some quick and easy things to add to your task.

Pass parameters

Consider this simple task;

component{
    function run( string name ){
        print.line( 'Well, hello there #name#!' );
    }
}

Pass in named parameters with a dolon before the parameter name that you want to pass through to the task. 

task run :name=Brad 

Console output

Tasks don't return HTML.  Instead, they can print colored text to the console with a handy print helper.

print.line( 'I like Spam.' );

print.somethingBlue( 'UHF ' );

print.blackOnWhiteText( 'Inverse!' );

print.boldRedOnBlueText( "Test dirt, don't wash." );

print.boldBlinkingUnderscoredBlueTextOnRedBackground( "That's just cruel" );

Interact with users

Just because this is the CLI doesn't mean you can't interact with the user!

// Prompt the user for a line of text
favoriteColor = ask( 'WHAT, is your favorite color??' );

response = ask( message='Enter installation Directory: ', defaultResponse='/etc/foo/bar` );

// Capture the next key
ASCIICode = waitForKey( 'Press any key, any key.' );
print.line().line( 'My magic tells me you pressed: #Chr( ASCIICode )#' );

// Get confirmation
if( confirm( 'Do you like Pizza? [y/n]' ) ) {
    print.greenLine( 'Good for you.' );
} else {
    print.boldRedLine( 'Heretic!!' );
}

Hit a Database

Lucee has a sweet trick up its sleeve that allows you to declare datasource connections on the fly.  Look how to connect to a connect to a local MySQL database.

ds = {
  class: 'org.gjt.mm.mysql.Driver',
  connectionString: 'jdbc:mysql://localhost:3306/bradwood?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=true',
  username: 'root',
  password: 'myPass'
};

var qry = queryExecute( sql='select * from cb_role', options={ datasource : ds } );

for( var row in qry ) {
  print.line( row.role );
}

 

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