class Savedatasync::Command

Public Class Methods

default_remote_dir() click to toggle source
# 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) click to toggle source
# File lib/savedatasync.rb, line 23
def self.extract_argv(com, argv)
  opt_parser = OptionParser.new do |parse|
    parse.banner = 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.expand_path(args_without_option[0]).encode("UTF-8")
  com.remote_filename = args_without_option[1]
  return com
rescue OptionParser::InvalidOption => error
  STDERR.puts error.message
  STDERR.puts opt_parser.help
  exit(1)
end
from_argv(argv) click to toggle source
# 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.expand_path(default_remote_dir).encode("UTF-8")
  com.remote_filename ||= File.basename(com.local_path).encode("UTF-8")
  return com
end

Public Instance Methods

run() click to toggle source
# 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.message
  exit(1)
end