Class: Kdeploy::CommandExecutor

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

Overview

Executes a single command and records execution time

Instance Method Summary collapse

Constructor Details

#initialize(executor, output, debug: false) ⇒ CommandExecutor

Returns a new instance of CommandExecutor.



6
7
8
9
10
# File 'lib/kdeploy/command_executor.rb', line 6

def initialize(executor, output, debug: false)
  @executor = executor
  @output = output
  @debug = debug
end

Instance Method Details

#execute_run(command, host_name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kdeploy/command_executor.rb', line 12

def execute_run(command, host_name)
  cmd = command[:command]
  use_sudo = command[:sudo]
  show_command_header(host_name, :run, cmd)

  # Show progress indicator for long-running commands
  pastel = @output.respond_to?(:pastel) ? @output.pastel : Pastel.new

  result, duration = measure_time do
    @executor.execute(cmd, use_sudo: use_sudo)
  end

  # Show execution time if command took more than 1 second
  @output.write_line(pastel.dim("    [completed in #{format('%.2f', duration)}s]")) if duration > 1.0

  # Show command output only in debug mode
  show_command_output(result) if @debug
  { command: cmd, output: result, duration: duration, type: :run }
end

#execute_sync(command, host_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kdeploy/command_executor.rb', line 56

def execute_sync(command, host_name)
  source = command[:source]
  destination = command[:destination]
  ignore = command[:ignore] || []
  exclude = command[:exclude] || []
  delete = command[:delete] || false

  description = "sync: #{source} -> #{destination}"
  description += " (delete: #{delete})" if delete
  show_command_header(host_name, :sync, description)

  result, duration = measure_time do
    @executor.sync_directory(
      source,
      destination,
      ignore: ignore,
      exclude: exclude,
      delete: delete
    )
  end

  {
    command: "sync: #{source} -> #{destination}",
    duration: duration,
    type: :sync,
    result: result,
    uploaded: result[:uploaded],
    deleted: result[:deleted],
    total: result[:total]
  }
end

#execute_upload(command, host_name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kdeploy/command_executor.rb', line 32

def execute_upload(command, host_name)
  show_command_header(host_name, :upload, "#{command[:source]} -> #{command[:destination]}")
  _result, duration = measure_time do
    @executor.upload(command[:source], command[:destination])
  end
  {
    command: "upload: #{command[:source]} -> #{command[:destination]}",
    duration: duration,
    type: :upload
  }
end

#execute_upload_template(command, host_name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kdeploy/command_executor.rb', line 44

def execute_upload_template(command, host_name)
  show_command_header(host_name, :upload_template, "#{command[:source]} -> #{command[:destination]}")
  _result, duration = measure_time do
    @executor.upload_template(command[:source], command[:destination], command[:variables])
  end
  {
    command: "upload_template: #{command[:source]} -> #{command[:destination]}",
    duration: duration,
    type: :upload_template
  }
end