Class: Launchr::CLI

Inherits:
Object
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/launchr/cli.rb

Overview

Defines options for the launchr command line utility

Instance Attribute Summary

Attributes included from Mixlib::CLI

#arguments, #banner, #config, #filtered_argv, #footer, #header, #opt_parser, #options, #options_arguments, #spaced_summary, #summary_indent, #summary_width

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixlib::CLI

#build_option_arguments, #filter_options_summary, #guess_and_switchify_arguments, #initialize, #parse_options

Class Method Details

.launchr_cli_optionsObject

The Launchr CLI Options



10
11
12
13
14
15
16
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
55
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/launchr/cli.rb', line 10

def self.launchr_cli_options

  spaced_summary true
  # banner false
  summary_indent ""
  summary_width 29

  header "brew launchd - an extension to start and stop Launchd services."
  header "              `man brew-launchd` for more information"
  header ""
  banner "Usage: brew launchd [options]"

  argument :start,
    :long  => "start service,(s)", 
    :type => Array,
    :description => ["Start launchd service(s)",
      "Equivalent to launchctl load -w files..."],
    :example => "start dnsmasq memcached couchdb",
    :default => nil

  argument :stop,
    :long  => "stop service,(s)", 
    :type => Array,
    :description => ["Stop launchd service(s)",
    "Equivalent to launchctl unload -w files..."],
    :example => "stop mamcached dnsmasq",
    :default => nil

  argument :restart,
    :long  => "restart service,(s)", 
    :type => Array,
    :description => ["Restart launchd service(s)"],
    :example => "restart couchdb",
    :default => nil

  option :user,
    :indent => true,
    :long  => "--user", 
    :description => ["At user login.",
      "Otherwise, the default setting will be used."],
    :example => "start --user openvpn ddclient",
    :requires => Proc.new { |args| (args[:boot] ^ args[:user]) && true || raise("--boot|--user") },
    :default => nil

  option :boot,
    :indent => true,
    :long  => "--boot", 
    :description => ["At boot time. Requires sudo/root privelidges.",
      "Otherwise, the default setting will be used."],
    :example => "sudo brew start --boot nginx mysql",
    :requires => Proc.new { |args| (args[:boot] ^ args[:user]) && true || raise("--boot|--user") },
    :default => nil


  argument :info,
    :long  => "info [service,(s)]", 
    :type => Array,
    :proc => Proc.new { |l| (l == true) ? [] : l },
    :description => ["Info for launchd service(s)","With no arguments prints info for all services."],
    :example => "brew launchd info",
    :default => nil

  argument :clean,
    :long  => "clean", 
    :description => ["Clean missing/broken launchd service(s)."],
    :example => ["brew launchd clean", "sudo brew launchd clean"],
    :default => nil

  argument :default,
    :long  => "default [--user|--boot]",
    :description => [
      "Set the default target to start launchd services.",
      "The initial setting, --user will start daemons at",
      "user login - from the Loginwindow (not over ssh).",
      " ",
      "Wheras --boot will set services to start at boot",
      "time. But be aware that brew should be installed",
      "to the root filesystem, not on a mounted volume."],

    :example => [
      "brew launchd default --boot",
      "brew launchd default --user"
      ],
    :requires => Proc.new { |args| (args[:boot] ^ args[:user]) && true || raise("--boot|--user") },
    :default => nil

  option :help, 
    :long => "--help",
    :description => "Show this message",
    :show_options => true,
    :exit => 0

  option :version, 
    :long => "--version",
    :description => "Print version information",
    :default => nil
end

Instance Method Details

#parse(argv = ARGV) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/launchr/cli.rb', line 109

def parse argv=ARGV
  parse_options(argv)

  [:start,:stop,:restart].each do |cmd|
    case config[cmd]
    when Array
      [:user, :boot].each do |level|
        if config[cmd].include? "--#{level}"
          config[level] = true
        end
      end
      config[cmd] -= ["--user","--boot"]
    end
  end

  raise "Please choose one of --user|--boot" if config[:user] && config[:boot]

  unless filtered_argv.empty?
    start_stop_restart_value = [config[:start],config[:stop],config[:restart],config[:info]].compact!

    if start_stop_restart_value.size == 1
      services = *start_stop_restart_value
      extra_services = filtered_argv

      services << extra_services
      services.flatten!
    end
  end

  config
end