Module: WormCLI

Defined in:
lib/ruby-progress/cli/worm_cli.rb,
lib/ruby-progress/cli/worm_options.rb

Overview

Enhanced Worm CLI (extracted from bin/prg)

Defined Under Namespace

Modules: Options

Class Method Summary collapse

Class Method Details

.resolve_pid_file(options, name_key = :daemon_name) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/ruby-progress/cli/worm_cli.rb', line 9

def self.resolve_pid_file(options, name_key = :daemon_name)
  return options[:pid_file] if options[:pid_file]

  return "/tmp/ruby-progress/#{options[name_key]}.pid" if options[name_key]

  RubyProgress::Daemon.default_pid_file
end

.runObject



17
18
19
20
21
22
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
# File 'lib/ruby-progress/cli/worm_cli.rb', line 17

def self.run
  trap('INT') do
    RubyProgress::Utils.show_cursor
    exit
  end

  options = WormCLI::Options.parse_cli_options

  if options[:status]
    pid_file = resolve_pid_file(options, :status_name)
    RubyProgress::Daemon.show_status(pid_file)
    exit
  elsif options[:stop]
    pid_file = resolve_pid_file(options, :stop_name)
    stop_msg = options[:stop_error] || options[:stop_success]
    is_error = !options[:stop_error].nil?
    RubyProgress::Daemon.stop_daemon_by_pid_file(
      pid_file,
      message: stop_msg,
      checkmark: options[:stop_checkmark],
      error: is_error
    )
    exit
  elsif options[:daemon]
    # Background without detaching so worm remains visible in current terminal
    PrgCLI.backgroundize

    run_daemon_mode(options)
  else
    progress = RubyProgress::Worm.new(options)

    if options[:command]
      progress.run_with_command
    else
      progress.run_indefinitely
    end
  end
end

.run_daemon_mode(options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby-progress/cli/worm_cli.rb', line 56

def self.run_daemon_mode(options)
  pid_file = resolve_pid_file(options, :daemon_name)
  FileUtils.mkdir_p(File.dirname(pid_file))
  File.write(pid_file, Process.pid.to_s)

  progress = RubyProgress::Worm.new(options)

  begin
    progress.run_daemon_mode(
      success_message: options[:success],
      show_checkmark: options[:checkmark],
      control_message_file: RubyProgress::Daemon.control_message_file(pid_file),
      icons: { success: options[:success_icon], error: options[:error_icon] }
    )
  ensure
    FileUtils.rm_f(pid_file)
  end
end