Class: Marseditsync::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, opts) ⇒ Command

Returns a new instance of Command.



93
94
95
96
# File 'lib/marseditsync.rb', line 93

def initialize(config, opts)
  @config = config
  @opts = opts
end

Class Method Details

.cp_new(srcfile, dstfile, check_mtime = false) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/marseditsync.rb', line 74

def self.cp_new(srcfile, dstfile, check_mtime = false)
  return unless FileTest.file?(srcfile)

  srcfile_mtime = File.mtime(srcfile)
  dstfile_mtime = nil
  if FileTest.file?(dstfile)
    dstfile_mtime = File.mtime(dstfile)
  end

  new_file = dstfile_mtime.nil? || srcfile_mtime > dstfile_mtime

  puts "cp #{srcfile}(#{srcfile_mtime}) #{dstfile}(#{dstfile_mtime})"      
  if !check_mtime || new_file
    FileUtils.cp(srcfile, dstfile, :preserve => true)
  else
    puts "=>skip by mtime"
  end    
end

.create_dir(dir) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/marseditsync.rb', line 60

def self.create_dir(dir)
  # コピー先ディレクトリの確認
  if FileTest.directory?(dir)
    puts "dir already exists. #{dir}"
  else
    FileUtils.mkdir(dir)
  end
  unless FileTest.directory?(dir)
    puts "dir is not directory. #{dir}"
    return false
  end
  true
end

.run(argv) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/marseditsync.rb', line 35

def self.run(argv)
  STDOUT.sync = true
  opts = {}
  opt = OptionParser.new(argv)
  opt.banner = "Usage: #{opt.program_name} [options] <backup|restore>"
  opt.separator "options:"
  opt.on('-h', '--help', 'Show this message') do |v|
    puts opt.help
    exit
  end
  # 冗長メッセージ
  opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v}
  opt.on('-n', '--dry-run', 'Message only') {|v| opts[:n] = v}
  opt.on('-c', '--config', 'Config file') {|v| opts[:c] = v} 
  opt.order!(argv)

  # 最後の引数を:jに入れて送る
  cmd = ARGV.shift
  opts[:j] = cmd
  yamlfile = opts[:c] || '~/.marseditsyncrc'
  config = Config.new(YAML.load_file(File.expand_path(yamlfile)))
  command = Command.new(config, opts)
  command.run
end

Instance Method Details

#runObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/marseditsync.rb', line 98

def run
  puts "##### marseditsync start #####"
  result = false
  if @opts[:j] == 'backup'
    result = backup
  elsif @opts[:j] == 'restore'
    result = restore
  end
  puts "##### marseditsync end(#{result}) #####" 
end