Last active
January 9, 2019 04:54
-
-
Save krishnact/e2678892aae65d1c241b4ad27adbdff0 to your computer and use it in GitHub Desktop.
A small "find" utility implementation using Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grapes([ | |
@GrabResolver(name='jitpack.io', root='https://jitpack.io') , | |
@Grab('org.slf4j:slf4j-log4j12:1.7.7') , | |
@Grab('com.h2database:h2:1.4.196') , | |
@Grab('com.github.krishnact:commandlinetool-base:0.4.10') , | |
@GrabExclude(group = 'org.codehaus.groovy', module='groovy-sql') , | |
@GrabExclude(group = 'org.codehaus.groovy', module='groovy-cli-commons') , | |
@GrabExclude(group = 'org.codehaus.groovy', module='groovy-json') , | |
@GrabExclude(group = 'org.codehaus.groovy', module='groovy-xml') , | |
@GrabExclude(group = 'org.codehaus.groovy', module='groovy-templates') | |
]) | |
import org.himalay.commandline.Option; | |
import org.himalay.commandline.CLTBase; | |
import groovy.cli.commons.OptionAccessor; | |
def options = new CLTBase(){ | |
@Option(required = true) | |
String name | |
@Option(regex='f|d') | |
String type | |
@Option(regex=/[+\-][0-9\.]+/) | |
String mtime | |
@Option | |
groovy.text.Template print = new groovy.text.SimpleTemplateEngine().createTemplate('${path}') | |
@Option | |
groovy.text.Template exec | |
public void setExec(String ee) { | |
exec = new groovy.text.SimpleTemplateEngine().createTemplate(ee) | |
} | |
public void setPrint(String ee) { | |
print = new groovy.text.SimpleTemplateEngine().createTemplate(ee) | |
} | |
@Override | |
public void examples() { | |
System.out.println 'groovy examples\\findAll.groovy . --name "R(.*).class" --mtime -0.5 --print "${new Date(file.lastModified())} ${file.name}"' | |
} | |
} | |
def oa = options.parseArgs(args) | |
if ( oa == null) return; | |
new File (oa.arguments()[0]).eachFileRecurse {File aFile -> | |
boolean bCont = true; | |
if (bCont && options.type != null) { | |
bCont = ( | |
(aFile.isDirectory() && (options.type == null || options.type == 'd')) || | |
(aFile.isFile() && (options.type == null || options.type == 'f')) | |
); | |
} | |
if (bCont && options.mtime != null) { | |
double mTime = options.mtime as double | |
long cutofTime = System.currentTimeMillis() - Math.abs(mTime*86400*1000); | |
if ( mTime > 0) { | |
bCont = (aFile.lastModified() < cutofTime) | |
}else if ( mTime < 0) { | |
bCont = (aFile.lastModified() > cutofTime) | |
} | |
} | |
if (bCont && options.name != null) { | |
bCont = (aFile.name ==~ options.name) | |
} | |
if (bCont) { | |
if (options.exec != null) { | |
String cmd = options.exec.make(["path": aFile.path]).toString(); | |
def result = options.executeAnExternalCmd(cmd, "."); | |
System.out.println(result.sout) | |
System.err.println(result.serr) | |
}else { | |
println options.print.make([path: aFile.path, file : aFile]).toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment