#!/usr/bin/env ruby require 'launch_agent' require 'git-style-binary/command' GitStyleBinary.primary do version "launch-agent #{LaunchAgent::VERSION}" short_desc "load/unload a launchd agent" banner <<-EOS Usage: #{command.full_name} #{all_options_string} {full command} Load/Unload a launchd agent EOS opt :env, "additional environmental variables to be set before running the job. can specify multiple times. e.g. RAILS_ENV=development", :type => String, :multi => true opt :daemon, "load as daemon. if it is set, --interval option is ignored", :default => false opt :interval, "causes the job to be started every N seconds", :type => Integer run do |command| abort 'full command must be supplised' if command.argv.empty? daemon = command.opts[:daemon] interval = command.opts[:interval] env = command.opts[:env] agent = nil if daemon agent = LaunchAgent::Daemon.new(*command.argv) elsif command.opts.interval agent = LaunchAgent::Periodic.new(interval, *command.argv) else abort 'at least one of --daemon and --interval must be set' end agent['EnvironmentVariables'] = env.inject({}) do |memo, e| k, v = e.split('=') memo[k] = v memo end action = agent.loaded? ? :unload : :load agent.send(action) puts '%s "%s"' % [action, command.argv.join(' ')] end end