class LaunchdTools::Launchd2CmdCli

Attributes

args[R]
paths[R]

Public Class Methods

new(args) click to toggle source
# 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

Public Instance Methods

extract_paths(path_args) click to toggle source
# 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_path() click to toggle source
# 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) click to toggle source
# 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
run() click to toggle source
# 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