Class: LaunchdTools::Launchd2CmdCli

Inherits:
Object
  • Object
show all
Defined in:
lib/launchd_tools/launchd2cmd_cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Launchd2CmdCli

Returns a new instance of Launchd2CmdCli.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/launchd_tools/launchd2cmd_cli.rb', line 6

def initialize(args)
  showing_help = false
  opt_parser = OptionParser.new do |opt|
    opt.banner = "Usage: #{$0} path/to/launchd.plist"
    opt.separator ""
    opt.separator "Options"
    opt.on("--help", "-h", "Displays this help message") do
      showing_help = true
    end
    opt.on("--version", "outputs version information for this tool") do 
      puts LaunchdTools::VERSION
    end
    opt.separator ""
  end

  opt_parser.parse!(args)

  if args.empty? || showing_help
    puts opt_parser
  else
    @args = args
    @paths = extract_paths(args)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



5
6
7
# File 'lib/launchd_tools/launchd2cmd_cli.rb', line 5

def args
  @args
end

#pathsObject (readonly)

Returns the value of attribute paths.



5
6
7
# File 'lib/launchd_tools/launchd2cmd_cli.rb', line 5

def paths
  @paths
end

Instance Method Details

#extract_paths(path_args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/launchd_tools/launchd2cmd_cli.rb', line 31

def extract_paths(path_args)
  path_args = path_args.map do |path_arg|
    if File.directory?(path_arg)
      path_arg += "/" if path_arg[-1] != "/"
      path_arg += "*"
    end
    path_arg
  end
  Dir.glob(path_args)
end

#process_each_pathObject



73
74
75
76
77
78
79
80
# File 'lib/launchd_tools/launchd2cmd_cli.rb', line 73

def process_each_path
  error_count = 0
  puts
  paths.each do |path_string|
    error_count += process_path(path_string)
  end
  return error_count
end

#process_path(path_string) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/launchd_tools/launchd2cmd_cli.rb', line 55

def process_path(path_string)
  error_count = 0
  begin
    path = Path.new(path_string).validate
    puts "# #{path.expanded}"
    puts path.parse.to_s
  rescue LaunchdTools::Path::UnparsablePlist
    puts "Error: unable to parse launchd job\n"
    error_count = 1
  rescue LaunchdTools::Path::PermissionsError
    require 'etc'
    username = Etc.getpwuid(Process.euid).name
    puts "Error: user #{username} does not have access to read launchd job\n"
    error_count = 1
  end
  return error_count
end

#runObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/launchd_tools/launchd2cmd_cli.rb', line 42

def run
  errors = 0
  if paths
    if paths.length > 0
      errors = process_each_path
    else
      args.each {|arg| puts "No launchd job found at '#{arg}'" }
      exit 1
    end
  end
  exit 2 if errors > 0
end