Skip to content

Commit

Permalink
Fix the copy command and add tests for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
pickhardt committed Sep 22, 2014
1 parent 1a65305 commit d7963bc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
67 changes: 31 additions & 36 deletions lib/files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,43 +69,38 @@ def self.interpret(command)
end

## Copy, Move, Scp files/folders
if action = command.match(/(copy|move)\s+(file(s?)|folder(s?))\s+(from?)\s/i)

paths = command.sub(/(copy|move)\s+(file(s?)|folder(s?))\s+(from?)\s/i,'').strip.split(' ')
if !(paths.size > 3 || paths.size <2)
paths -= ['to'] if(paths.size == 3) # Remove 'to' if exist

source, destination = paths

## Remote transfer ## !!!! SUGGESTION REQUIRED !!!!
if (source.split('@')[1].split(':')[0].match(/(.com|\d+)/) rescue false)|| (destination.split('@')[1].split(':')[0].match(/(.com|\d+)/) rescue false)
flags = ""
flags += (File.directory?(source) || File.directory?(destination)) ? "-r " : ''

responses << {
:command => "scp #{flags} #{source} #{destination}",
:explanation => "Remote Transfer"
}
elsif File.exist?(source)
## Move
if action[1] == 'move'
responses << {
:command => "mv #{source} #{destination}",
:explanation => "Move the file/folder"
}
## Copy
elsif action[1] == 'copy'
flags = ""
flags += File.directory?(source) ? "-R " : ''
responses << {
:command => "cp #{flags}#{source} #{destination}",
:explanation => "Move the file/folder"
}
end
end

if action = command.match(/(copy|move)\s+(file(s?)|folder(s?)\s+)?(from\s+)?/i)
paths = command.sub(/(copy|move)\s+(file(s?)|folder(s?)\s+)?(from\s+)?/i, '').strip.split(' ')
if !(paths.size > 3 || paths.size < 2)
paths -= ['to'] if paths.size == 3 # Remove 'to' if exist

source, destination = paths

## Remote transfer ## !!!! SUGGESTION REQUIRED !!!!
if (source.split('@')[1].split(':')[0].match(/(.com|\d+)/) rescue false)|| (destination.split('@')[1].split(':')[0].match(/(.com|\d+)/) rescue false)
flags = ""
flags += (File.directory?(source) || File.directory?(destination)) ? "-r " : ''

responses << {
:command => "scp #{flags}#{source} #{destination}",
:explanation => "Remote transfer"
}
else
if action[1].downcase == 'move'
responses << {
:command => "mv #{source} #{destination}",
:explanation => "Moves the file/folder"
}
elsif action[1].downcase == 'copy'
flags = ""
flags += File.directory?(source) ? "-R " : ''
responses << {
:command => "cp #{flags}#{source} #{destination}",
:explanation => "Copies the file/folder"
}
end
end
end # path

end

responses
Expand Down
13 changes: 13 additions & 0 deletions spec/files_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'spec_helper'

describe 'Files' do
context 'Copy myfile.txt to mycopy.txt' do
it { responds_with command: "cp myfile.txt mycopy.txt",
explanation: "Copies the file/folder" }
end

context 'copy folder mysongs/ to backup/' do
it { responds_with command: "cp mysongs/ backup/",
explanation: "Copies the file/folder" }
end
end

0 comments on commit d7963bc

Please sign in to comment.