aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/actions.js61
-rwxr-xr-xcli/surfer.js17
2 files changed, 51 insertions, 27 deletions
diff --git a/cli/actions.js b/cli/actions.js
index 0c621f6..3fd2403 100644
--- a/cli/actions.js
+++ b/cli/actions.js
@@ -128,32 +128,27 @@ function logout() {
128 }); 128 });
129} 129}
130 130
131function put(filePath, otherFilePaths, options) { 131function putOne(filePath, destination, options, callback) {
132 checkConfig(options); 132 const absoluteFilePath = path.resolve(filePath);
133 133 const stat = safe.fs.statSync(absoluteFilePath);
134 var destination = ''; 134 if (!stat) return callback(`Could not stat ${filePath}: ${safe.error.message}`);
135 135
136 // take the last argument as destination 136 let files, base;
137 if (otherFilePaths.length > 0) { 137
138 destination = otherFilePaths.pop(); 138 if (stat.isFile()) {
139 if (otherFilePaths.length > 0 && destination[destination.length-1] !== '/') destination += '/'; 139 base = destination + path.basename(filePath);
140 files = [ absoluteFilePath ];
141 } else if (stat.isDirectory()) {
142 base = destination + (filePath.endsWith('.') ? '' : path.basename(filePath) + '/');
143 files = collectFiles([ absoluteFilePath ], options);
144 } else {
145 return callback(); // ignore
140 } 146 }
141 147
142 var files = collectFiles([ filePath ].concat(otherFilePaths), options);
143
144 async.eachSeries(files, function (file, callback) { 148 async.eachSeries(files, function (file, callback) {
145 var relativeFilePath; 149 let relativeFilePath = file.slice(absoluteFilePath.length + 1); // will be '' when filePath is a file
146 150 let destinationPath = base + relativeFilePath;
147 if (path.isAbsolute(file)) { 151 console.log('Uploading file %s -> %s', file.cyan, destinationPath.cyan);
148 relativeFilePath = path.basename(file);
149 } else if (path.resolve(file).indexOf(process.cwd()) === 0) { // relative to current dir
150 relativeFilePath = path.resolve(file).slice(process.cwd().length + 1);
151 } else { // relative but somewhere else
152 relativeFilePath = path.basename(file);
153 }
154
155 var destinationPath = (destination ? '/' + destination : '') + '/' + relativeFilePath;
156 console.log('Uploading file %s -> %s', relativeFilePath.cyan, destinationPath.cyan);
157 152
158 superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) { 153 superagent.post(gServer + API + destinationPath).query(gQuery).attach('file', file).end(function (error, result) {
159 if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed')); 154 if (result && result.statusCode === 403) return callback(new Error('Upload destination ' + destinationPath + ' not allowed'));
@@ -164,7 +159,25 @@ function put(filePath, otherFilePaths, options) {
164 159
165 callback(null); 160 callback(null);
166 }); 161 });
167 }, function (error) { 162 }, callback);
163}
164
165function put(filePaths, options) {
166 checkConfig(options);
167
168 if (filePaths.length < 2) {
169 console.log('target directory is required.'.red);
170 process.exit(1);
171 }
172
173 let destination = filePaths.pop();
174 if (!path.isAbsolute(destination)) {
175 console.log('target directory must be absolute'.red);
176 process.exit(1);
177 }
178 if (!destination.endsWith('/')) destination += '/';
179
180 async.eachSeries(filePaths, (filePath, iteratorDone) => putOne(filePath, destination, options, iteratorDone), function (error) {
168 if (error) { 181 if (error) {
169 console.log('Failed to put file.', error.message.red); 182 console.log('Failed to put file.', error.message.red);
170 process.exit(1); 183 process.exit(1);
diff --git a/cli/surfer.js b/cli/surfer.js
index 33b4db6..bd574f5 100755
--- a/cli/surfer.js
+++ b/cli/surfer.js
@@ -21,10 +21,21 @@ program.command('logout')
21 .description('Logout from server') 21 .description('Logout from server')
22 .action(actions.logout); 22 .action(actions.logout);
23 23
24program.command('put <file|dir> [files...]') 24program.command('put <file|dir...>')
25 .option('-a --all', 'Also include hidden files and folders.', false) 25 .option('-a --all', 'Also include hidden files and folders.', false)
26 .description('Put a file, last argument is destination if provided') 26 .description('Puts a list of files or dirs to the destination. The last argument is destination dir')
27 .action(actions.put); 27 .action(actions.put)
28 .on('--help', function() {
29 console.log();
30 console.log(' Examples:');
31 console.log();
32 console.log(' $ surfer put file.txt / # puts to /file.txt');
33 console.log(' $ surfer put file.txt /data # puts to /data/file.txt');
34 console.log(' $ surfer put dir /data # puts dir/* as /data/dir/*');
35 console.log(' $ surfer put dir/. / # puts dir/* as /app/data/*');
36 console.log(' $ surfer put dir1 dir2 file1 / # puts as /dir1/* /dir2/* and /file');
37 console.log();
38 });
28 39
29program.command('get [file|dir]') 40program.command('get [file|dir]')
30 .description('Get a file or directory listing') 41 .description('Get a file or directory listing')