launch-agent 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/launchagent CHANGED
@@ -1,31 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'rubygems'
3
4
  require 'launch_agent'
4
- require 'git-style-binary/command'
5
+ require 'docopt'
5
6
 
6
- GitStyleBinary.primary do
7
- version "launch-agent #{LaunchAgent::VERSION}"
8
-
9
- short_desc "load/unload a launchd agent"
10
- banner <<-EOS
11
- Usage: #{command.full_name} #{all_options_string} {full command}
12
-
13
- Load/Unload a launchd agent
7
+ program_name = File.basename($0)
8
+ doc =<<-EOS
9
+ Usage:
10
+ #{program_name} [--env=<env>] (--daemon | --interval <sec>) [--wdir=<dir>] [--] (<argument>...)
11
+ #{program_name} -h | --help
14
12
 
13
+ Options:
14
+ -h --help Show this screen.
15
+ -e --env=<env> Additional environmental variables to be set before running the job. Can specify multiple value with comma. e.g. FOO=bar,BAR=baz
16
+ -w --wdir=<dir> Specify a directory to chdir(2) to before running the job
17
+ -d --daemon Load as daemon. If it is set, --interval option is ignored.
18
+ -i --interval=<sec> Causes the job to be started every N seconds
15
19
  EOS
16
- 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
17
- opt :daemon, "load as daemon. if it is set, --interval option is ignored", :default => false
18
- opt :wdir, "specify a directory to chdir(2) to before running the job", :type => String
19
- opt :interval, "causes the job to be started every N seconds", :type => Integer
20
20
 
21
- run do |command|
22
- begin
23
- agent = LaunchAgent::CLI::OptionParser.new(command.opts, command.argv).agent
24
- action = agent.loaded? ? :unload : :load
25
- agent.send(action)
26
- puts '%s "%s"' % [action, command.argv.join(' ')]
27
- rescue => e
28
- abort e.message
29
- end
30
- end
21
+ options = Docopt(doc, LaunchAgent::VERSION)
22
+
23
+ begin
24
+ agent = LaunchAgent::CLI::OptionParser.new(options, ARGV).agent
25
+ action = agent.loaded? ? :unload : :load
26
+ agent.send(action)
27
+ puts '%s "%s"' % [action, ARGV.join(' ')]
28
+ rescue => e
29
+ abort e.message
31
30
  end
data/launch-agent.gemspec CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.version = LaunchAgent::VERSION
17
17
 
18
18
  gem.add_dependency('plist')
19
- gem.add_dependency('git-style-binaries')
19
+ gem.add_dependency('docopt')
20
20
  gem.add_development_dependency('rspec', ['~> 2.8.0'])
21
21
  gem.add_development_dependency('rake')
22
22
  end
@@ -11,14 +11,14 @@ module LaunchAgent
11
11
 
12
12
  daemon = @opts[:daemon]
13
13
  interval = @opts[:interval]
14
- env = @opts[:env]
14
+ env = @opts[:env].split(',')
15
15
  wdir = @opts[:wdir]
16
16
  agent = nil
17
17
 
18
18
  if daemon
19
19
  agent = LaunchAgent::Daemon.new(*@argv)
20
- elsif @opts[:interval]
21
- agent = LaunchAgent::Periodic.new(interval, *@argv)
20
+ elsif interval
21
+ agent = LaunchAgent::Periodic.new(interval.to_i, *@argv)
22
22
  else
23
23
  raise 'at least one of --daemon and --interval must be set'
24
24
  end
@@ -1,4 +1,4 @@
1
1
  module LaunchAgent
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
4
4
 
@@ -6,21 +6,46 @@ describe CLI::OptionParser do
6
6
  before do
7
7
  @argv = %w/ruby foo.rb/
8
8
  @opts = {
9
- :env => []
9
+ :env => ''
10
10
  }
11
-
12
- @agent = CLI::OptionParser.new(opts, @argv).agent
13
- @plist = agent2plist(@agent)
14
11
  end
15
12
 
16
13
  shared_examples_for 'valid agent' do
17
14
  it "should have necessary attributes" do
18
- @plist['Label'].should eql('com.buycheapviagraonlinenow.ruby__foo_rb')
19
- @plist['ProgramArguments'].should eql(@argv)
15
+ agent = CLI::OptionParser.new(opts, @argv).agent
16
+ plist = agent2plist(agent)
17
+
18
+ plist['Label'].should eql('com.buycheapviagraonlinenow.ruby__foo_rb')
19
+ plist['ProgramArguments'].should eql(@argv)
20
+ end
21
+ end
22
+
23
+ describe 'empty argv' do
24
+ let(:opts) do
25
+ @opts.merge(
26
+ :daemon => true)
27
+ end
28
+
29
+ it 'should raise if argv is empty' do
30
+ lambda {
31
+ CLI::OptionParser.new(opts, []).agent
32
+ }.should raise_error('full command must be supplied')
20
33
  end
21
34
  end
22
35
 
23
- describe 'damon' do
36
+ describe 'empty options' do
37
+ let(:opts) do
38
+ @opts
39
+ end
40
+
41
+ it 'should raise if option is empty' do
42
+ lambda {
43
+ CLI::OptionParser.new(opts, @argv).agent
44
+ }.should raise_error('at least one of --daemon and --interval must be set')
45
+ end
46
+ end
47
+
48
+ describe 'daemon' do
24
49
  let(:opts) do
25
50
  @opts.merge(
26
51
  :daemon => true)
@@ -28,11 +53,14 @@ describe CLI::OptionParser do
28
53
 
29
54
  it_should_behave_like 'valid agent'
30
55
 
31
- it 'should parse damon option' do
32
- @plist['KeepAlive'].should eql({
56
+ it 'should parse daemon option' do
57
+ agent = CLI::OptionParser.new(opts, @argv).agent
58
+ plist = agent2plist(agent)
59
+
60
+ plist['KeepAlive'].should eql({
33
61
  'SuccessfulExit' => false
34
62
  })
35
- @plist['RunAtLoad'].should be_true
63
+ plist['RunAtLoad'].should be_true
36
64
  end
37
65
  end
38
66
 
@@ -46,7 +74,10 @@ describe CLI::OptionParser do
46
74
  it_should_behave_like 'valid agent'
47
75
 
48
76
  it 'should parse wdir option' do
49
- @plist['WorkingDirectory'].should eql('/foo/bar')
77
+ agent = CLI::OptionParser.new(opts, @argv).agent
78
+ plist = agent2plist(agent)
79
+
80
+ plist['WorkingDirectory'].should eql('/foo/bar')
50
81
  end
51
82
  end
52
83
 
@@ -54,26 +85,32 @@ describe CLI::OptionParser do
54
85
  let(:opts) do
55
86
  @opts.merge(
56
87
  :daemon => true,
57
- :env => ['FOO=BAR','BAR=BAZ'])
88
+ :env => 'FOO=BAR,BAR=BAZ')
58
89
  end
59
90
 
60
91
  it_should_behave_like 'valid agent'
61
92
 
62
93
  it 'should parse env option' do
63
- @plist['EnvironmentVariables'].should eql({ 'FOO' => 'BAR', 'BAR' => 'BAZ' })
94
+ agent = CLI::OptionParser.new(opts, @argv).agent
95
+ plist = agent2plist(agent)
96
+
97
+ plist['EnvironmentVariables'].should eql({ 'FOO' => 'BAR', 'BAR' => 'BAZ' })
64
98
  end
65
99
  end
66
100
 
67
101
  describe 'interval' do
68
102
  let(:opts) do
69
103
  @opts.merge(
70
- :interval => 120)
104
+ :interval => "120")
71
105
  end
72
106
 
73
107
  it_should_behave_like 'valid agent'
74
108
 
75
109
  it 'should parse env option' do
76
- @plist['StartInterval'].should eql(120)
110
+ agent = CLI::OptionParser.new(opts, @argv).agent
111
+ plist = agent2plist(agent)
112
+
113
+ plist['StartInterval'].should eql(120)
77
114
  end
78
115
  end
79
116
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: launch-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-18 00:00:00.000000000 Z
12
+ date: 2012-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plist
16
- requirement: &70200854706280 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70200854706280
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
- name: git-style-binaries
27
- requirement: &70200854705860 !ruby/object:Gem::Requirement
31
+ name: docopt
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70200854705860
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rspec
38
- requirement: &70200854705340 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 2.8.0
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70200854705340
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.8.0
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rake
49
- requirement: &70200854704920 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70200854704920
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: A library to use launchd easily
59
79
  email:
60
80
  - youpy@buycheapviagraonlinenow.com
@@ -96,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
116
  version: '0'
97
117
  segments:
98
118
  - 0
99
- hash: 1175971151883054079
119
+ hash: -3804420004878315494
100
120
  required_rubygems_version: !ruby/object:Gem::Requirement
101
121
  none: false
102
122
  requirements:
@@ -105,10 +125,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
125
  version: '0'
106
126
  segments:
107
127
  - 0
108
- hash: 1175971151883054079
128
+ hash: -3804420004878315494
109
129
  requirements: []
110
130
  rubyforge_project:
111
- rubygems_version: 1.8.10
131
+ rubygems_version: 1.8.24
112
132
  signing_key:
113
133
  specification_version: 3
114
134
  summary: A library to use launchd easily