Created
September 13, 2017 10:26
-
-
Save AdamSaleh/b983ac41895aa4c71fc84b936480f48f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const yargs = require('yargs'); | |
| const Configstore = require('configstore'); | |
| const request = require('request-promise'); | |
| const fs = require('fs'); | |
| let jsonRequest = request.defaults({ | |
| json: true | |
| }); | |
| const argv = yargs.usage('$0 <cmd> [args]') | |
| .command('target <url>', 'choose a target', {}) | |
| .command('current', 'get a target', {}) | |
| .command('create', 'create new locker', {}) | |
| .command('delete <id>', 'delete new locker', {}) | |
| .command('list <id>', 'list files in locker', {}) | |
| .command('upload <id> <file> <handle>', 'upload file to locker', {}) | |
| .command('download <id> <handle> <file>', 'download file from locker', {}) | |
| .help().argv; | |
| const conf = new Configstore('courselocker', {}); | |
| async function run (argv) { | |
| const command = argv._[0]; | |
| if (command === 'target') { | |
| conf.set('url', argv.url); | |
| console.log(`Targetting ${argv.url}`); | |
| } else if (command === 'current') { | |
| console.log(`Targetting ${conf.get('url')}`); | |
| } else if (command === 'create') { | |
| const locker = await jsonRequest.post(`${conf.get('url')}/`); | |
| console.log(locker.id); | |
| } else if (command === 'delete') { | |
| const locker = await jsonRequest.delete(`${conf.get('url')}/${argv.id}`); | |
| console.log(locker); | |
| } else if (command === 'list') { | |
| const listResponse = await jsonRequest.get(`${conf.get('url')}/${argv.id}/items`); | |
| for (let file of listResponse) { | |
| console.log(file); | |
| } | |
| } else if (command === 'upload') { | |
| const file = fs.createReadStream(argv.file); | |
| const upload = jsonRequest.post(`${conf.get('url')}/${argv.id}/item/${argv.handle}`); | |
| file.pipe(upload); | |
| const response = await upload; | |
| console.log(response); | |
| } else if (command === 'download') { | |
| const file = fs.createWriteStream(argv.file); | |
| const download = jsonRequest.get(`${conf.get('url')}/${argv.id}/item/${argv.handle}`); | |
| download.pipe(file); | |
| await download; | |
| console.log('Downloaded'); | |
| } else { | |
| console.log(`Command ${command} doesn't exist`); | |
| } | |
| } | |
| run(argv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment