Class: Savedatasync::Command
- Inherits:
-
Struct
- Object
- Struct
- Savedatasync::Command
- Defined in:
- lib/savedatasync.rb
Instance Attribute Summary collapse
-
#force ⇒ Object
Returns the value of attribute force.
-
#local_path ⇒ Object
Returns the value of attribute local_path.
-
#remote_dir_path ⇒ Object
Returns the value of attribute remote_dir_path.
-
#remote_filename ⇒ Object
Returns the value of attribute remote_filename.
-
#sub_command ⇒ Object
Returns the value of attribute sub_command.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#force ⇒ Object
Returns the value of attribute force
7 8 9 |
# File 'lib/savedatasync.rb', line 7 def force @force end |
#local_path ⇒ Object
Returns the value of attribute local_path
7 8 9 |
# File 'lib/savedatasync.rb', line 7 def local_path @local_path end |
#remote_dir_path ⇒ Object
Returns the value of attribute remote_dir_path
7 8 9 |
# File 'lib/savedatasync.rb', line 7 def remote_dir_path @remote_dir_path end |
#remote_filename ⇒ Object
Returns the value of attribute remote_filename
7 8 9 |
# File 'lib/savedatasync.rb', line 7 def remote_filename @remote_filename end |
#sub_command ⇒ Object
Returns the value of attribute sub_command
7 8 9 |
# File 'lib/savedatasync.rb', line 7 def sub_command @sub_command end |
#title ⇒ Object
Returns the value of attribute title
7 8 9 |
# File 'lib/savedatasync.rb', line 7 def title @title end |
Class Method Details
.default_remote_dir ⇒ Object
19 20 21 |
# File 'lib/savedatasync.rb', line 19 def self.default_remote_dir ENV['sdsync_remote_dir'].encode("UTF-8") || "#{Dir.home.encode("UTF-8")}/Dropbox/sdsync" end |
.extract_argv(com, argv) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/savedatasync.rb', line 23 def self.extract_argv(com, argv) opt_parser = OptionParser.new do |parse| parse. = Savedatasync::USAGE desc = 'force to execute even if there is an existing file' parse.on('--force', desc) do com.force = true end desc = 'specify title. basename(dirname(savedata-file)) is used as default' parse.on('--title=TITLE', desc) do |title| com.title = title end desc = "specify the path of remote directory. ENV['sdsync_remote_dir'] or ~/Dropbox/sdsync is used as default" parse.on('--remote=PATH', desc) do |path| com.remote_dir_path = path end end sub_command, *args_without_option = opt_parser.parse(argv) unless ['get', 'put', 'cut', 'status'].include?(sub_command) STDERR.puts "invalid subcommand #{sub_command}" STDERR.puts opt_parser.help exit(1) end unless 1 <= args_without_option.size && args_without_option.size <= 2 STDERR.puts "invalid length of arguments" STDERR.puts opt_parser.help print_usage exit(1) end com.sub_command = sub_command com.local_path = File.(args_without_option[0]).encode("UTF-8") com.remote_filename = args_without_option[1] return com rescue OptionParser::InvalidOption => error STDERR.puts error. STDERR.puts opt_parser.help exit(1) end |
.from_argv(argv) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/savedatasync.rb', line 11 def self.from_argv(argv) com = extract_argv(new, argv.map { |a| a.encode("UTF-8") }) com.title ||= File.basename(File.dirname(com.local_path)).encode("UTF-8") com.remote_dir_path ||= File.(default_remote_dir).encode("UTF-8") com.remote_filename ||= File.basename(com.local_path).encode("UTF-8") return com end |
Instance Method Details
#run ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/savedatasync.rb', line 61 def run op = Operator.new(title, force, remote_dir_path) case sub_command when 'put' op.put(local_path, remote_filename) when 'get' op.get(local_path, remote_filename) when 'cut' op.cut(local_path, remote_filename) when 'status' op.status(local_path, remote_filename) end rescue Operator::Error => err STDERR.puts err. exit(1) end |