Groovy is a powerfull development language with a lot of features and almost all we need.
As we all are familiar with Java, Groovy and many libraries, why don't we use it to write scripts for system-automation?
This session will show the best practices and how to overcome some obstacles when writing shell-scripts using Groovy.
12. BUT
DOESNOTWORKONLINUX
Alternative script for /usr/bin/env
Without defining system variables
#!/bin/bash
ARGS=( $1 ) # separate $1 into multiple space-delimited arguments.
shift # consume $1
PROG=`which ${ARGS[0]}` # find path for executable
unset ARGS[0] # discard executable name
ARGS+=( "$@" ) # remainder of arguments preserved "as-is".
# iterate array and expand variables
declare -a PARAMS
for i in "${ARGS[@]}"
do
PARAMS=("${PARAMS[@]}" "`eval "echo $i"`")
done
exec $PROG "${PARAMS[@]}" # execute the command
15. INSTALLATION
WINDOWS
GroovyServ is part of the Groovy Windows-Installer
LINUX
gvm install groovyserv
MACOSX
brew install groovyserv
BINARYPACKAGE
$ cd /tmp
$ unzip groovyserv-1.0.0-bin.zip
$ groovyserv-1.0.0-bin/bin/setup.sh
export PATH=/tmp/groovyserv-1.0.0/bin:$PATH
31. PARSINGCOMMANDLINE
ARGUMENTS
DSL ontop of Apache Commons CLI
!#/usr/bin/env groovy
def cli = new CliBuilder(usage: 'MyScript')
cli.with {
v(longOpt: 'version', 'show version information')
}
def opt = cli.parse(args)
if (!opt)
System.exit(2)
if (opt.v) {
println "Version: 1.0.0"
}
32. ASAMPLE
usage: MyScript [options] [args]
-?,--help usage information
--config <arg> A script for tweaking the configuration
-D <property=value> use value for given property
-s,--source <path> Aliases for '-source'
-source <path> Specify where to find the files
-v,--version version information
33. THECODEI
#!/usr/bin/env groovy
def cli = new CliBuilder(usage: 'MyScript [options] [args]')
cli.with {
source (args:1, argName: 'path', optionalArg: false,
'Specify where to find the files')
_ (longOpt: 'config', args:1, argName: 'arg', optionalArg: false,
'A script for tweaking the configuration')
s (longOpt: 'source', args:1, argName:'path', optionalArg: false,
"Aliases for '-source'")
'?' (longOpt: 'help', 'usage information')
v (longOpt: 'version', 'version information')
D (args: 2, valueSeparator: '=', argName: 'property=value',
'use value for given property')
}
34. THECODEII
def opt = cli.parse(args)
if (!opt) // usage already displayed by cli.parse()
System.exit(2)
if(opt.'?')
cli.usage()
else if (opt.v)
println "Version: 1.0.0"
else {
if (opt.config)
println "Configuration: $opt.config"
if(opt.D) {
println "Custom properties:"
println opt.Ds.collate(2).collect{ it.join('=') }.join('n')
}
def home = System.properties.'user.dir'
if (opt.s || opt.source)
home = opt.s ?: opt.source
println "Working on files:"
opt.arguments().each println "$home/$it"
}
37. @GRABANNOTATIONI
Annotatable everywhere
often seen at import statements
group (String)
Maven groupId or Ivy organization
module (String)
Maven artifactId or Ivy artifact
version (String)
literal
'1.1-RC3'
Ivy range
'[1.0, 2,0]' -> 1.0 or 2.0
'[2.1,)' -> 2.1 or greater
classifier (String)
'jdk15'
optional
43. QUESTIONS?
Alexander (Sascha) Klein
codecentric AG
Curiestr. 2
70563 Stuttgart
tel +49.711.67400-328
mobile +49.172.5294020
alexander.klein@codecentric.de
@saschaklein
http://www.codecentric.de
http://blog.codecentric.de