Last active
August 29, 2015 13:58
-
-
Save lstroud/10303967 to your computer and use it in GitHub Desktop.
Simple command line command for exporting file from gitlab to local file system using gitlab api.
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
Requires NodeJS and npm. | |
Install prerequisites: | |
npm install qs minimist path-extra | |
Usage: gitlab-file-export [options] url_to_export | |
This is intended for exporting individual files from | |
the respository and does not support tree exports. | |
Options: | |
-t Your secret token for gitlab access (found in gitab | |
profile). If not specified, will check ~/.gitlab/token. | |
-o Output file. If not specified will go to stdout. | |
-h Print this help. | |
Examples: | |
gitlab-file-export -t SOMETOKEN https://someserver/a-namesapce/aproject/blob/master/afolder/aconfig.cfg > /configfile | |
Or, with token file in ~/.gitlab/token: | |
gitlab-file-export -o /folder/aconfig.cfg https://someserver/a-namesapce/aproject/blob/master/afolder/aconfig.cfg |
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
#!/usr/bin/env node | |
var https = require('https') | |
var qs = require('qs') | |
var argv = require('minimist')(process.argv.slice(2), {string:['t', '_']}) | |
var fs = require('fs') | |
var path = require('path-extra'); | |
var util = require('util'); | |
var helpmsg = "Usage: gitlab-file-export [options] url_to_export" | |
+ "\n\tThis is intended for exporting individual files from " | |
+ "\n\tthe respository and does not support tree exports." | |
+ "\n " | |
+ "\nOptions:" | |
+ "\n\t-t\tYour secret token for gitlab access (found in gitab " | |
+ "\n\t \tprofile). If not specified, will check ~/.gitlab/token." | |
+ "\n\t-o\tOutput file. If not specified will go to stdout." | |
+ "\n\t-h\tPrint this help." | |
+ "\n\n" | |
+ "\nExamples:" | |
+ "\n\t gitlab-file-export -t SOMETOKEN https://someserver/a-namesapce/aproject/blob/master/afolder/aconfig.cfg > /configfile" | |
+ "\n" | |
+ "\n\t Or, with token file in ~/.gitlab/token:" | |
+ "\n\t gitlab-file-export -o /folder/aconfig.cfg https://someserver/a-namesapce/aproject/blob/master/afolder/aconfig.cfg" | |
if(argv.h){ | |
console.log(helpmsg); | |
process.exit(0); | |
} | |
var token = argv.t; | |
if(!token){ | |
var _token_file_path = path.homedir() + '/.gitlab/token'; | |
if(fs.existsSync(_token_file_path)){ | |
token = fs.readFileSync(_token_file_path); | |
} else { | |
console.error("No token provided and file " + _token_file_path + " not found or not readable."); | |
} | |
} | |
if(token) token = token.toString(); | |
var output_file = argv.o; | |
var file_url = argv._.toString(); | |
if(!file_url || !token){ | |
console.error(helpmsg); | |
process.exit(1); | |
} | |
var file_properties = parse_gitlab_url(file_url); | |
var options = { | |
hostname: file_properties.server, | |
port: 443, | |
method: 'GET', | |
rejectUnauthorized: false, | |
headers: { | |
"PRIVATE-TOKEN": token | |
} | |
} | |
var _search_format = '/api/v3/projects/search/%s' | |
var _fetch_format = '/api/v3/projects/%s/repository/files?%s' | |
options.path = util.format(_search_format, encodeURIComponent(file_properties.project_name)); | |
fetch_json(options, function(json){ | |
var querystring = { | |
file_path: file_properties.file_path, | |
ref: file_properties.ref | |
} | |
options.path = util.format(_fetch_format, json[0].id, qs.stringify(querystring)); | |
fetch_json(options, function(file_data){ | |
var _output = new Buffer(file_data.content, 'base64').toString('ascii'); | |
if(output_file){ | |
fs.writeFileSync(output_file, _output); | |
} else { | |
console.log(_output); | |
} | |
process.exit(0); | |
}) | |
}); | |
function parse_gitlab_url(url){ | |
var component_arr = url.split('\/'); | |
var components = { | |
protocol: component_arr[0], | |
server: component_arr[2], | |
group: component_arr[3], | |
project_name: component_arr[4], | |
ref: component_arr[6], | |
file_path: component_arr.slice(7, component_arr.length).join('/') | |
} | |
return components; | |
} | |
function fetch_json(options, callback){ | |
var req = https.request(options, function(res){ | |
var data = ""; | |
res.on('data', function(d){ | |
data += d; | |
}); | |
res.on('end', function(){ | |
var json = JSON.parse(data); | |
callback.call(this, json, res); | |
}); | |
}); | |
req.end(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment