Skip to content

Instantly share code, notes, and snippets.

@trevershick
Created March 30, 2012 02:40
Show Gist options
  • Save trevershick/2245992 to your computer and use it in GitHub Desktop.
Save trevershick/2245992 to your computer and use it in GitHub Desktop.
A simple groovy script that will allow a command line search of search.maven.org
#!/usr/bin/env groovy
import groovy.json.*
/**
* calculate the maximum value returned from the closure
* for the given collection.
*/
def maxLength(collection, closure) {
collection.inject(0) { acc,val -> Math.max(acc, closure(val)) }
}
/**
*
*/
def format(doc, widths) {
def fmt = "%-${widths.maxg}s :: %-${widths.maxa}s :: %-${widths.maxv}s"
if (doc.versionCount) fmt += " :: %s"
String.format(fmt , doc.g,
doc.a,
(doc.latestVersion ? doc.latestVersion : doc.v), doc.versionCount )
}
/**
*
*/
def printHelp() {
println '''
Usage #1 - search by artifact id
=============
\\Users\\me>groovy search.groovy guava
Found 24 Results
Group :: Artifact :: Version :: Version Count
--------------------------------------------------------------------------------------------------
biz.littlej.jreqs :: jreqs-guava :: 0.1.1 :: 2
com.fasterxml.jackson.datatype :: jackson-datatype-guava :: 2.0.0 :: 4
com.fasterxml.jackson :: jackson-datatype-guava :: 1.9.0 :: 2
com.google.guava :: guava-annotations :: r03 :: 1
Usage #2 - search by group id and artifact id
=============
\\Users\\me>groovy search.groovy com.google.guava guava
Found 15 Results
Group :: Artif :: Version
-------------------------------------
com.google.guava :: guava :: 10.0-rc1
com.google.guava :: guava :: 10.0-rc2
com.google.guava :: guava :: 10.0-rc3
com.google.guava :: guava :: 10.0.1
'''
}
def url = 'http://search.maven.org/solrsearch/select'
def rows = 100
def wt = 'json'
def docs
def resultsFound = 0
// crappy argument handling
switch (args.length) {
case 0:
System.err.println "You need to specify params";
printHelp()
System.exit(1);
case 1:
url += "?q=${args[0]}"
break;
case 2:
def g = args[0]
def a = args[1]
url += "?q=a:\"${a}\"+AND+g:\"${g}\""
url += "&core=gav"
break;
default:
println "eeeeeh! - too many arguments"
System.exit(1)
}
url += "&rows=${rows}&wt=${wt}"
try {
def jsonText = url.toURL().getText(connectTimeout:15000, readTimeout:2000)
def json = new JsonSlurper().parseText(jsonText)
assert json.response.docs
docs = json.response.docs
resultsFound = json.response.numFound
} catch (e) {
println "Oops ${e.message}"
System.exit(1)
}
maxg = maxLength(docs, { it.g.length()})
maxa = maxLength(docs, { it.a.length()})
maxv = maxLength(docs, { it.latestVersion? it.latestVersion.length() : it.v.length() })
def header = [
g:"Group"[0..Math.min(4, maxg-1)],
a:"Artifact"[0..Math.min(7, maxa-1)],
v:"Version"[0..Math.min(6, maxv-1)]]
// calculate out the header widths based on one or two args.
// this is pretty crappy , but works for now
def headerWidth = maxg + maxa + maxv;
if (args.length == 1) {
header += [versionCount:"Version Count"]
headerWidth += "Version Count".length()
}
headerWidth += " :: ".multiply(header.size() - 1).length()
def widths = [
maxa:maxa,
maxg:maxg,
maxv:maxv
]
println "Found ${resultsFound} Results"
println format(header, widths)
println "-".multiply(headerWidth)
docs
.sort { "${it.g}:${it.a}:${it.v}:${it.latestVersion}" }
.each { println format(it, widths) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment