Created
September 22, 2016 16:38
-
-
Save kellyrob99/dd14aa3a20ebf66a185ea358fb808870 to your computer and use it in GitHub Desktop.
Uploads all files in a given directory to a Raw hosted Repository on Nexus 3.
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
@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.2') | |
import groovyx.net.http.HTTPBuilder | |
import org.apache.http.HttpRequest | |
import org.apache.http.HttpRequestInterceptor | |
import org.apache.http.protocol.HttpContext | |
import static groovy.io.FileType.FILES | |
import static groovyx.net.http.ContentType.BINARY | |
import static groovyx.net.http.ContentType.TEXT | |
import static groovyx.net.http.Method.PUT | |
CliBuilder cli = new CliBuilder( | |
usage: 'groovy clientDemo.groovy -u {user} -p {password} -d {path to content} -n {repoName} [-h {nx3Url}]') | |
cli.with { | |
u longOpt: 'username', args: 1, required: true, 'A User with permissions to push to the target repo' | |
p longOpt: 'password', args: 1, required: true, 'Password for given User' | |
n longOpt: 'repoName', args: 1, required: true, 'Name of repo to publish to' | |
d longOpt: 'dir', args: 1, required: true, 'Path of directory to recursively publish' | |
h longOpt: 'host', args: 1, 'NX3 host url (including port if necessary). Defaults to http://localhost:8081' | |
} | |
def options = cli.parse(args) | |
if (!options) { | |
return | |
} | |
File sourceFolder = new File(options.d) | |
assert sourceFolder.exists(): "${sourceFolder} does not exist" | |
def username = options.u | |
def password = options.password | |
def authInterceptor = new HttpRequestInterceptor() { | |
void process(HttpRequest httpRequest, HttpContext httpContext) { | |
httpRequest.addHeader('Authorization', 'Basic ' + "${username}:${password}".bytes.encodeBase64().toString()) | |
} | |
} | |
HTTPBuilder http = new HTTPBuilder(options.h ?: 'http://localhost:8081') | |
http.client.addRequestInterceptor(authInterceptor) | |
def resourcePath = "/repository/${options.n}/" | |
def files = [] | |
sourceFolder.eachFileRecurse(FILES) { file -> | |
if (file.name != '.DS_Store') { | |
files << file | |
} | |
} | |
println "Staging ${files.size()} files for publishing" | |
files.each { File file -> | |
println "pushing $file" | |
http.request(PUT) { | |
uri.path = "$resourcePath${relativeize(sourceFolder, file)}" | |
requestContentType = TEXT | |
body = file.text | |
response.success = { resp -> | |
println "POST response status: ${resp.statusLine}" | |
assert resp.statusLine.statusCode == 201 | |
} | |
} | |
} | |
String relativeize(File parent, File child) { | |
return parent.toURI().relativize(child.toURI()).getPath() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment