Skip to content

Instantly share code, notes, and snippets.

View xlson's full-sized avatar

Leonard Gram xlson

View GitHub Profile
@xlson
xlson / gist:1144188
Created August 13, 2011 19:36
Sample of using the autodetect feature of groovycsv 0.3
@GrabResolver(name='Nexus OSS Repo', root='https://oss.sonatype.org/content/groups/public/', m2Compatible='true')
@Grab('com.xlson.groovycsv:groovycsv:0.3-SNAPSHOT')
import com.xlson.groovycsv.CsvParser
def csv = '''Name:Lastname
Mark:Andersson
Pete:Hansen'''
def data = new CsvParser().parse(csv, autoDetect:true)
for(line in data) {
@xlson
xlson / carbon
Created July 11, 2011 08:28
Simple service script for Graphites carbon-cache daemon
#!/bin/bash
#
# chkconfig: 35 90 12
# description: carbon-cache script
#
LOCKFILE=/var/lock/subsys/carbon
# Get function from functions library
. /etc/init.d/functions
@xlson
xlson / lazyCollectImpl.groovy
Created June 15, 2011 13:59
Lazy implementation of collect for Groovy
class LazyCollectList extends AbstractList {
private Closure collectClosure
private List backingList
private Map cached = [:]
def get(int i) {
if(!cached.containsKey(i)) {
@xlson
xlson / batch-feature.groovy
Created June 15, 2011 12:48
Adds method that will batch the values in a list into a new list containing batches of items
Object.metaClass.partition = { partitionSize ->
def result = delegate.inject([[]]) { partitioned, it ->
if(partitioned.last().size() == partitionSize) {
partitioned << [it]
} else {
partitioned.last() << it
}
partitioned
}
result?.last().size() == 0 ? [] : result
@xlson
xlson / gfind
Created May 5, 2011 09:23
Search for text in certain files using grep (find + grep)
#!/bin/bash
NAME_PATTERN=$1
GREP_TEXT=$2
find . -name "$NAME_PATTERN" -exec grep -H "$GREP_TEXT" '{}' \;
@xlson
xlson / rerun-until-fail
Created April 29, 2011 16:04
Executes a command over and over until the command returns a non-zero exit code. Useful for running tests that should be, but aren't, deterministic.
#!/bin/bash
COMMAND=$1
i=0
echo "Executing: $COMMAND"
while true; do
i=$[$i+1]
echo "Run: $i ("`date`")"
$COMMAND 2>&1 1> run.log
@xlson
xlson / groovycsv-example.groovy
Created April 14, 2011 12:20
Example of using GroovyCSV 0.2 to parse csv
@Grab('com.xlson.groovycsv:groovycsv:0.2')
import com.xlson.groovycsv.CsvParse
def csv = '''Name-Lastname
Mark-'Anderson-Nielsen'
Pete-Hansen'''
def data = new CsvParser().parse(csv, separator: '-', quoteChar: "'")
for(line in data) {
println "$line.Name $line.Lastname"
}
@xlson
xlson / gist:901615
Created April 4, 2011 13:14
git bash completion and git info in bash prompt (taked from jboss instruction)
GIT_COMPLETION_PATH="/usr/local/git/contrib/completion/git-completion.bash"
if [ -f "$GIT_COMPLETION_PATH" ]; then
GIT_PS1_SHOWDIRTYSTATE=true
. "$GIT_COMPLETION_PATH"
ADD_PS1='$(__git_ps1)'
fi
if [[ ${EUID} == 0 ]] ; then
PS1="\[\033[01;31m\]\h\[\033[01;34m\] \w\[\033[33m\]$ADD_PS1\[\033[34m\] \$\[\033[00m\] "
else
@xlson
xlson / ppxml
Created January 18, 2011 16:13
Pretty-prints XML on the commandline (requires groovyserv)
#!/usr/bin/env groovyclient
println(prettyPrintXml(System.in.text))
def prettyPrintXml(text) {
def xmlNode = new XmlParser().parseText(text)
def writer = new StringWriter()
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.preserveWhitespace = true
printer.print(xmlNode)
@xlson
xlson / TestController.groovy
Created January 17, 2011 15:29
Example of using the Grails dataBind method to change values of a command object after it has already been created
package testcommandobjects
class TestController {
static final int VALID = 5
static final int INVALID = 50
def resetToValid = { TestCommand cmd ->
rebindAgeAndRenderResponse(cmd, true)
}