Sha256: 63e83fbd58d67054d0e40cec7a98a6f3e657d7cd7bfc7d680e339082f0e235a3

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

#!/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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
launch-agent-0.4.1 bin/launchagent