Class: Savedatasync::Command

Inherits:
Struct
  • Object
show all
Defined in:
lib/savedatasync.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#forceObject

Returns the value of attribute force

Returns:

  • (Object)

    the current value of force



7
8
9
# File 'lib/savedatasync.rb', line 7

def force
  @force
end

#local_pathObject

Returns the value of attribute local_path

Returns:

  • (Object)

    the current value of local_path



7
8
9
# File 'lib/savedatasync.rb', line 7

def local_path
  @local_path
end

#remote_dir_pathObject

Returns the value of attribute remote_dir_path

Returns:

  • (Object)

    the current value of remote_dir_path



7
8
9
# File 'lib/savedatasync.rb', line 7

def remote_dir_path
  @remote_dir_path
end

#remote_filenameObject

Returns the value of attribute remote_filename

Returns:

  • (Object)

    the current value of remote_filename



7
8
9
# File 'lib/savedatasync.rb', line 7

def remote_filename
  @remote_filename
end

#sub_commandObject

Returns the value of attribute sub_command

Returns:

  • (Object)

    the current value of sub_command



7
8
9
# File 'lib/savedatasync.rb', line 7

def sub_command
  @sub_command
end

#titleObject

Returns the value of attribute title

Returns:

  • (Object)

    the current value of title



7
8
9
# File 'lib/savedatasync.rb', line 7

def title
  @title
end

Class Method Details

.default_remote_dirObject



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.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) ⇒ 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.expand_path(default_remote_dir).encode("UTF-8")
  com.remote_filename ||= File.basename(com.local_path).encode("UTF-8")
  return com
end

Instance Method Details

#runObject



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.message
  exit(1)
end