Class: UnisonCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/unison.rb,
lib/unison/version.rb

Defined Under Namespace

Classes: InvalidOption, NonExistentOption

Constant Summary collapse

UNISON_DEFAULT_COMMAND =
'unison'
UNISON_OPTION_SPEC =
{
  :addprefsto => :array,
  :addversionno => :bool,
  :auto => :bool,
  :backup => :array,
  :backupcurrent => :array,
  :backupcurrentnot => :array,
  :backupdir => :string,
  :backuplocation => ['local', 'central'],
  :backupnot => :array,
  :backupprefix => :string,
  :backups => :bool,
  :backupsuffix => :string,
  :batch => :bool,
  :confirmbigdeletes => :bool,
  :confirmmerge => :bool,
  :contactquietly => :bool,
  :debug => ['all', 'verbose'],
  :dumbtty => :bool,
  :fastcheck => ['true', 'false', 'default'],
  :follow => :array,
  :force => :string,
  :forcepartial => :array,
  :group => :bool,
  :host => :string,
  :ignore => :array,
  :ignorecase => ['true', 'false', 'default'],
  :ignorelocks => :bool,
  :ignorenot => :array,
  :immutable => :array,
  :immutablenot => :array,
  :key => :array,
  :killserver => :bool,
  :label => :string,
  :log => :bool,
  :logfile => :string,
  :maxbackups => :number,
  :maxthreads => :number,
  :merge => :array,
  :mountpoint => :array,
  :numericids => :bool,
  :owner => :bool,
  :path => :array,
  :perms => :string, # check the option later
  :prefer => :array,
  :preferpartial => :array,
  :pretendwin => :bool,
  :repeat => :string,
  :retry => :number,
  :root => :array,
  :rootalias => :array,
  :rshargs => :string,
  :rshcmd => :string,
  :rsrc => ['true', 'false', 'default'],
  :rsync => :bool,
  :selftest => :bool,
  :servercmd => :string,
  :showarchive => :bool,
  :silent => :bool,
  :socket => :string,
  :sortbysize => :bool,
  :sortfirst => :array,
  :sortlast => :array,
  :sortnewfirst => :bool,
  :sshargs => :string,
  :sshcmd => :string,
  :terse => :bool,
  :testserver => :bool,
  :times => :bool,
  :xferbycopying => :bool
}
VERSION =
"0.0.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ UnisonCommand

args accepts the following three pattern:

  • root1, root2, opts = {}

  • profilename, opts = {}

  • profilename, root1, root2, opts = {}

We set option of unison command to optinal hash. The keys are symbols made from hypen-removed options of unison command and the values are booleans (true or false), strings, and arrays of strings corresponding to unison’s options. Note that to set boolean option we do not use a string ‘true’ or ‘false’, but we use TrueClass, FalseClass, or NilClass.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/unison.rb', line 97

def initialize(*args)
  if Hash === args[-1]
    @option = args[-1]
    args = args[0...-1]
  else
    @option = {}
  end
  case args.size
  when 1
    @profile = args[0]
    @root1 = nil
    @root2 = nil
  when 2
    @profile = nil
    @root1 = args[0]
    @root2 = args[1]
  when 3
    @profile = args[0]
    @root1 = args[1]
    @root2 = args[2]
  else
    raise ArgumentError, "Invalid"
  end
  @command = UNISON_DEFAULT_COMMAND
  @status = nil
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



84
85
86
# File 'lib/unison.rb', line 84

def command
  @command
end

#optionObject

Returns the value of attribute option.



84
85
86
# File 'lib/unison.rb', line 84

def option
  @option
end

#profileObject

Returns the value of attribute profile.



84
85
86
# File 'lib/unison.rb', line 84

def profile
  @profile
end

#root1Object

Returns the value of attribute root1.



84
85
86
# File 'lib/unison.rb', line 84

def root1
  @root1
end

#root2Object

Returns the value of attribute root2.



84
85
86
# File 'lib/unison.rb', line 84

def root2
  @root2
end

#statusObject (readonly)

Returns the value of attribute status.



85
86
87
# File 'lib/unison.rb', line 85

def status
  @status
end

Instance Method Details

#execute(dry_run = false) ⇒ Object

The method returns :success when all files are synchronized, :skipped when some files are skipped, :non_fatal_error when non fatal error occurs, and :fatal_error when fatal error occurs or process is interrupted. If dry_run is true, the method returns an array of a unison command to execute.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/unison.rb', line 190

def execute(dry_run = false)
  cmd = get_command
  if dry_run
    @status = nil
    return cmd
  end
  # Search exit code of unison command.
  Kernel.system(*cmd)
  case get_exit_status
  when 0
    :success
  when 1
    :skipped
  when 2
    :non_fatal_error
  when 3
    :fatal_error
  else
    raise StandardError, "Invalid exit code of unison: #{status.inspect.strip}."
  end
end

#versionObject



212
213
214
# File 'lib/unison.rb', line 212

def version
  `#{@command} -version`.split[2]
end