Class: DockerSync::WatchStrategy::Fswatch

Inherits:
Object
  • Object
show all
Includes:
Execution, Thor::Shell
Defined in:
lib/docker-sync/watch_strategy/fswatch.rb

Instance Method Summary collapse

Methods included from Execution

#fork_exec, #thread_exec, #with_time

Constructor Details

#initialize(sync_name, options) ⇒ Fswatch

Returns a new instance of Fswatch.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 15

def initialize(sync_name, options)
  @sync_name = sync_name
  @options = options
  @events_to_watch = %w(AttributeModified Created Link MovedFrom MovedTo Renamed Removed Updated)

  begin
    Dependencies::Fswatch.ensure!
  rescue StandardError => e
    say_status 'error', e.message, :red
    exit 1
  end
end

Instance Method Details

#cleanObject



35
36
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 35

def clean
end

#get_sync_cli_callObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 61

def get_sync_cli_call
  sync_command = 'thor sync:'
  case @options['cli_mode']
    when 'docker-sync'
      say_status 'ok','Forcing cli mode docker-sync',:yellow if @options['verbose']
      sync_command = 'docker-sync '
    when 'thor'
      say_status 'ok','Forcing cli mode thor',:yellow if @options['verbose']
      sync_command = 'thor sync:'
    else # 'auto' or any other, try to guss
      say_status 'ok','Cli mode is auto, selecting .. ',:white if @options['verbose']
      exec_name = File.basename($PROGRAM_NAME)
      if exec_name != 'thor'
        sync_command = 'docker-sync '
      else
        say_status 'warning', 'Called user thor, not docker-sync* wise, assuming dev mode, using thor', :yellow
      end
      say_status 'ok',".. #{sync_command}",:white if @options['verbose']
  end

  # append the actual operation
  return "#{sync_command}sync"
end

#runObject



28
29
30
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 28

def run
  watch
end

#stopObject



32
33
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 32

def stop
end

#watchObject



38
39
40
41
42
43
44
45
46
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 38

def watch
  args = watch_options
  say_status 'success', "Starting to watch #{@options['src']} - Press CTRL-C to stop", :green
  cmd = 'fswatch ' + args.join(' ')
  say_status 'command', cmd, :white if @options['verbose']

  # run a thread here, since it is blocking
  @watch_thread = thread_exec(cmd, "Sync #{@sync_name}", :blue)
end

#watch_forkObject



85
86
87
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 85

def watch_fork
  return nil
end

#watch_optionsObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 48

def watch_options
  args = []
  unless @options['watch_excludes'].nil?
    args = @options['watch_excludes'].map { |pattern| "--exclude='#{pattern}'" } + args
  end
  args.push('-orIE')
  args.push(@events_to_watch.map { |pattern| "--event #{pattern}" })
  args.push(@options['watch_args']) if @options.key?('watch_args')
  args.push("'#{@options['src']}'")
  sync_command = get_sync_cli_call
  args.push(" | xargs -I -n1 #{sync_command} -n #{@sync_name} --config='#{@options['config_path']}'")
end

#watch_threadObject



89
90
91
# File 'lib/docker-sync/watch_strategy/fswatch.rb', line 89

def watch_thread
  return @watch_thread
end