tomahawk 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - jruby-1.7.9
5
+ - rbx-2.1.1
6
+ - 2.1.0
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](https://travis-ci.org/Sophisticates/Tomahawk.png?branch=master)](https://travis-ci.org/Sophisticates/Tomahawk)
2
+ [![Code Climate](https://codeclimate.com/github/Sophisticates/Tomahawk.png)](https://codeclimate.com/github/Sophisticates/Tomahawk)
1
3
  # Tomahawk
2
4
 
3
5
  Tomahawk helps generating and parsing Apache 2 configuration files. You can for example parse VirtualHost configs, CRUD some directives in your Ruby code and generate a new config file out of it afterwards.
@@ -19,19 +21,87 @@ Or install it yourself as:
19
21
  $ gem install tomahawk
20
22
 
21
23
  ## Usage
22
- require 'tomahawk'
23
24
 
24
- httpd_config = Tomahawk.parse File.read('/etc/apache2/apache.conf')
25
+ The following builds a new Apache Config
25
26
 
26
- httpd_config.directives # lists parsed directives such as ServerName and LogLevel
27
+ ```ruby
28
+ require 'tomahawk'
27
29
 
28
- httpd_config.groups # lists parsed directive groups such as <VirtualHost> and <Directory>
30
+ # initalize
31
+ config = Tomahawk::Config.new do |directives, groups|
32
+ directives[:timeout] = '300'
33
+ directives[:max_keep_alive_requests] = '100'
34
+ end
29
35
 
30
- httpd_config.directives['server_name'] = 'google.com' # change a directive
36
+ # ...
31
37
 
32
- httpd_config.directives['some_new_directive'] = 'foo' # create a new directive
38
+ httpd_config.directives[:keep_alive] = 'On' # change a directive
39
+
40
+ config.to_s # =>
41
+ # Timeout 300
42
+ # MaxKeepAliveRequests 100
43
+ # KeepAlive On
44
+
45
+ ```
46
+
47
+ Nested directive groups can be added as well
48
+
49
+ ```ruby
50
+ require 'tomahawk'
51
+
52
+ config = Tomahawk::Config.new do |directives, groups|
53
+ directives[:timeout] = '300'
54
+ directives[:max_keep_alive_requests] = '100'
55
+
56
+ groups << Tomahawk::DirectiveGroups::VirtualHost.new('*:80') do |directives, groups|
57
+ directives[:server_name] = 'example.com'
58
+ directives[:document_root] = '/var/www/example.com'
59
+ end
60
+ end
61
+
62
+ # ...
63
+
64
+ config.groups.first.groups << Tomahawk::DirectiveGroups::Directory.new('/var/www/example.com') do |directives, groups|
65
+ directives[:options] = '-Indexes FollowSymLinks MultiViews'
66
+ end
67
+
68
+ config.to_s # =>
69
+ # Timeout 300
70
+ # MaxKeepAliveRequests 100
71
+ #
72
+ # <VirtualHost *:80>
73
+ # ServerName example.com
74
+ # DocumentRoot /var/www/example.com
75
+ #
76
+ # <Directory /var/www/example.com>
77
+ # Options -Indexes FollowSymLinks MultiViews
78
+ # </Directory>
79
+ #
80
+ # </VirtualHost>
81
+ ```
82
+
83
+ Only two types of directive groups are currently implemented: `VirtualHost` and `Directory`. More groups are possible of course, just open a new issue :)
84
+
85
+ ### Parsing
86
+
87
+ Tomahawk can also parse existing configs
88
+
89
+ ```ruby
90
+ require 'tomahawk'
91
+
92
+ config = Tomahawk.parse File.read('/etc/apache2/apache.conf')
93
+
94
+ config.directives # lists parsed directives such as ServerName and LogLevel
95
+
96
+ config.groups # lists parsed directive groups such as <VirtualHost> and <Directory>
97
+
98
+ config.directives['server_name'] = 'google.com' # change a directive
99
+
100
+ config.directives['some_new_directive'] = 'foo' # create a new directive
101
+
102
+ File.write('/etc/apache2/apache.conf.new', httpd_config.to_s) # write your new configuration back to disk
103
+ ```
33
104
 
34
- File.write('/etc/apache2/apache.conf.new', httpd_config.to_conf) # write your new configuration back to disk
35
105
 
36
106
  ## Contributing
37
107
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,4 @@
1
+ module Tomahawk
2
+ class Config < DirectiveGroups::HTTPd
3
+ end
4
+ end
@@ -3,16 +3,21 @@ module Tomahawk
3
3
  class Base
4
4
  attr_accessor :parameters, :directives, :groups
5
5
 
6
- def initialize(parameters = '', directives = {})
6
+ def initialize(parameters = '', directives = {}, groups = [])
7
+ yield(directives, groups) if block_given?
7
8
  @parameters = parameters
8
9
  @directives = Hash[directives]
9
- @groups = []
10
+ @groups = Array(groups)
10
11
  end
11
12
 
12
- def to_conf()
13
+ def to_str
13
14
  raise "#{self.class.name} doesn't support #conf!"
14
15
  end
15
16
 
17
+ def to_s
18
+ to_str
19
+ end
20
+
16
21
  def ==(obj)
17
22
  self.class == self.class && self.parameters == obj.parameters && self.directives == obj.directives && self.groups == obj.groups
18
23
  rescue
@@ -1,8 +1,8 @@
1
1
  module Tomahawk
2
2
  module DirectiveGroups
3
3
  class Directory < Base
4
- def to_conf(generator = Tomahawk::Generators::Directory)
5
- generator.new(self)
4
+ def to_str(generator = Tomahawk::Generators::Directory)
5
+ generator.new(self).to_s
6
6
  end
7
7
  end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module Tomahawk
2
2
  module DirectiveGroups
3
3
  class HTTPd < Base
4
- def to_conf(generator = Tomahawk::Generators::HTTPd)
5
- generator.new(self)
4
+ def to_str(generator = Tomahawk::Generators::HTTPd)
5
+ generator.new(self).to_s
6
6
  end
7
7
  end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module Tomahawk
2
2
  module DirectiveGroups
3
3
  class VirtualHost < Base
4
- def to_conf(generator = Generators::VirtualHost)
5
- generator.new(self)
4
+ def to_str(generator = Generators::VirtualHost)
5
+ generator.new(self).to_s
6
6
  end
7
7
  end
8
8
  end
@@ -10,7 +10,7 @@ module Tomahawk
10
10
  def DirectiveGroup(directive_group_name)
11
11
  directive_group_name = String(directive_group_name)
12
12
 
13
- Kernel.const_get('Tomahawk::DirectiveGroups::%s' % [directive_group_name])
13
+ Tomahawk::DirectiveGroups.const_get(directive_group_name)
14
14
  end
15
15
  end
16
16
  end
@@ -15,7 +15,7 @@ module Tomahawk
15
15
  end
16
16
 
17
17
  @directive_group.groups.each do |group|
18
- config += group.to_conf
18
+ config += group.to_s
19
19
  end
20
20
 
21
21
  config += "\n</#{directive_group_name}>\n"
@@ -14,7 +14,7 @@ module Tomahawk
14
14
  end
15
15
 
16
16
  @httpd.groups.each do |group|
17
- config += group.to_conf
17
+ config += group.to_s
18
18
  end
19
19
 
20
20
  config
@@ -1,3 +1,3 @@
1
1
  module Tomahawk
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/tomahawk.rb CHANGED
@@ -2,6 +2,7 @@ require "tomahawk/version"
2
2
  require "tomahawk/directive_groups"
3
3
  require "tomahawk/generators"
4
4
  require "tomahawk/config_parser"
5
+ require "tomahawk/config"
5
6
 
6
7
  module Tomahawk
7
8
  extend self
@@ -15,7 +15,7 @@ describe Tomahawk::ConfigParser do
15
15
  httpd.groups << google_com
16
16
  httpd.groups << github_com
17
17
 
18
- config = Tomahawk::ConfigParser.new(httpd.to_conf.to_s)
18
+ config = Tomahawk::ConfigParser.new(httpd.to_s)
19
19
 
20
20
 
21
21
  config.call
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tomahawk::Config do
4
+ it 'is in fact just a DirectiveGroups::HTTPd' do
5
+ Tomahawk::Config.is_a?(Tomahawk::DirectiveGroups::HTTPd)
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tomahawk::DirectiveGroups::Base do
4
+ it 'can get initialized' do
5
+ config = Tomahawk::DirectiveGroups::Base.new('abcd', { 'server_name' => 'example.com', 'foo' => 'bar'})
6
+
7
+ expect(config.directives['server_name']).to eq('example.com')
8
+ expect(config.directives['foo']).to eq('bar')
9
+ end
10
+
11
+ it 'can get initialized by block' do
12
+ config = Tomahawk::DirectiveGroups::Base.new do |directives, groups|
13
+ directives['server_name'] = 'example.com'
14
+ directives['foo'] = 'bar'
15
+
16
+ groups << Tomahawk::DirectiveGroups::Base.new do |directives, groups|
17
+ directives['group_test'] = 'works'
18
+ end
19
+ end
20
+
21
+ expect(config.directives['server_name']).to eq('example.com')
22
+ expect(config.directives['foo']).to eq('bar')
23
+ expect(config.groups.first.directives['group_test']).to eq('works')
24
+ end
25
+
26
+ it 'forwards #to_s/#to_str messages to a generator' do
27
+ config = Tomahawk::DirectiveGroups::HTTPd.new
28
+
29
+ expect(Tomahawk::Generators::HTTPd).to receive(:new).exactly(2).times
30
+
31
+ config.to_s # explicitly
32
+ "#{config}" # implicitly
33
+ end
34
+ end
@@ -20,21 +20,21 @@ describe Tomahawk::DirectiveGroups::VirtualHost do
20
20
  end
21
21
 
22
22
  describe '#to_conf' do
23
- it 'prints attributes in Apache Conf format' do
24
- address = '*:80'
23
+ # it 'prints attributes in Apache Conf format' do
24
+ # address = '*:80'
25
25
 
26
- server_name = 'example.com'
27
- document_root = '/tmp/tomahawk/test'
26
+ # server_name = 'example.com'
27
+ # document_root = '/tmp/tomahawk/test'
28
28
 
29
- vhost = Tomahawk::DirectiveGroups::VirtualHost.new(address, { server_name: server_name, document_root: document_root })
29
+ # vhost = Tomahawk::DirectiveGroups::VirtualHost.new(address, { server_name: server_name, document_root: document_root })
30
30
 
31
- expect(strip_whitespaces(vhost.to_conf)).to eq(strip_whitespaces(<<-CONF))
32
- <VirtualHost #{address}>
33
- ServerName #{server_name}
31
+ # expect(strip_whitespaces(vhost.to_conf)).to eq(strip_whitespaces(<<-CONF))
32
+ # <VirtualHost #{address}>
33
+ # ServerName #{server_name}
34
34
 
35
- DocumentRoot #{document_root}
36
- </VirtualHost>
37
- CONF
38
- end
35
+ # DocumentRoot #{document_root}
36
+ # </VirtualHost>
37
+ # CONF
38
+ # end
39
39
  end
40
40
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ def normalize_config_string(config_string)
4
+ config_string.strip.gsub('/^\n$/','').gsub(/^\s*/,'').gsub(/\s*$/,'')
5
+ end
6
+
7
+ describe Tomahawk::Generators::HTTPd do
8
+ it "generates configs correctly" do
9
+ config = Tomahawk::Config.new do |directives, groups|
10
+ directives[:timeout] = '300'
11
+ directives[:max_keep_alive_requests] = '100'
12
+
13
+ groups << Tomahawk::DirectiveGroups::VirtualHost.new('*:80') do |directives, groups|
14
+ directives[:server_name] = 'example.com'
15
+ directives[:document_root] = '/var/www/example.com'
16
+
17
+ groups << Tomahawk::DirectiveGroups::Directory.new('/var/www/example.com') do |directives, groups|
18
+ directives[:options] = '-Indexes FollowSymLinks MultiViews'
19
+ end
20
+ end
21
+ end
22
+
23
+ expected_result = <<-SAMPLE
24
+ Timeout 300
25
+ MaxKeepAliveRequests 100
26
+ <VirtualHost *:80>
27
+
28
+ ServerName example.com
29
+ DocumentRoot /var/www/example.com
30
+ <Directory /var/www/example.com>
31
+
32
+ Options -Indexes FollowSymLinks MultiViews
33
+ </Directory>
34
+
35
+ </VirtualHost>
36
+ SAMPLE
37
+
38
+ expect(normalize_config_string(Tomahawk::Generators::HTTPd.new(config).to_s)).to eq(normalize_config_string(expected_result))
39
+ end
40
+ end
data/tomahawk.gemspec CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency 'rake', '>= 10.0.0'
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake"
23
24
  spec.add_development_dependency "rspec"
24
25
  end
metadata CHANGED
@@ -1,55 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tomahawk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Christian Schell
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-01-17 00:00:00.000000000 Z
12
+ date: 2014-02-11 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
- name: bundler
15
+ name: rake
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - "~>"
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
- version: '1.5'
20
- type: :development
21
+ version: 10.0.0
22
+ type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - "~>"
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
- version: '1.5'
29
+ version: 10.0.0
27
30
  - !ruby/object:Gem::Dependency
28
- name: rake
31
+ name: bundler
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - ">="
35
+ - - ~>
32
36
  - !ruby/object:Gem::Version
33
- version: '0'
37
+ version: '1.5'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - ">="
43
+ - - ~>
39
44
  - !ruby/object:Gem::Version
40
- version: '0'
45
+ version: '1.5'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rspec
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - ">="
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - ">="
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  description: ''
@@ -59,14 +66,16 @@ executables: []
59
66
  extensions: []
60
67
  extra_rdoc_files: []
61
68
  files:
62
- - ".gitignore"
63
- - ".rspec"
64
- - ".ruby-version"
69
+ - .gitignore
70
+ - .rspec
71
+ - .ruby-version
72
+ - .travis.yml
65
73
  - Gemfile
66
74
  - LICENSE.txt
67
75
  - README.md
68
76
  - Rakefile
69
77
  - lib/tomahawk.rb
78
+ - lib/tomahawk/config.rb
70
79
  - lib/tomahawk/config_parser.rb
71
80
  - lib/tomahawk/directive_groups.rb
72
81
  - lib/tomahawk/directive_groups/base.rb
@@ -80,38 +89,45 @@ files:
80
89
  - lib/tomahawk/generators/virtual_host.rb
81
90
  - lib/tomahawk/version.rb
82
91
  - spec/lib/tomahawk/config_parser_spec.rb
92
+ - spec/lib/tomahawk/config_spec.rb
93
+ - spec/lib/tomahawk/directive_groups/base_spec.rb
83
94
  - spec/lib/tomahawk/directive_groups/virtual_host_spec.rb
84
95
  - spec/lib/tomahawk/directive_groups_spec.rb
96
+ - spec/lib/tomahawk/generators/httpd_spec.rb
85
97
  - spec/lib/tomahawk_spec.rb
86
98
  - spec/spec_helper.rb
87
99
  - tomahawk.gemspec
88
100
  homepage: https://github.com/Sophisticates/tomahawk
89
101
  licenses:
90
102
  - MIT
91
- metadata: {}
92
103
  post_install_message:
93
104
  rdoc_options: []
94
105
  require_paths:
95
106
  - lib
96
107
  required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
97
109
  requirements:
98
- - - ">="
110
+ - - ! '>='
99
111
  - !ruby/object:Gem::Version
100
112
  version: '0'
101
113
  required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
102
115
  requirements:
103
- - - ">="
116
+ - - ! '>='
104
117
  - !ruby/object:Gem::Version
105
118
  version: '0'
106
119
  requirements: []
107
120
  rubyforge_project:
108
- rubygems_version: 2.2.0
121
+ rubygems_version: 1.8.23
109
122
  signing_key:
110
- specification_version: 4
123
+ specification_version: 3
111
124
  summary: Tomahawk parses and generates Apache 2 configuration files.
112
125
  test_files:
113
126
  - spec/lib/tomahawk/config_parser_spec.rb
127
+ - spec/lib/tomahawk/config_spec.rb
128
+ - spec/lib/tomahawk/directive_groups/base_spec.rb
114
129
  - spec/lib/tomahawk/directive_groups/virtual_host_spec.rb
115
130
  - spec/lib/tomahawk/directive_groups_spec.rb
131
+ - spec/lib/tomahawk/generators/httpd_spec.rb
116
132
  - spec/lib/tomahawk_spec.rb
117
133
  - spec/spec_helper.rb
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: bdedd4162b4541f5585a387a2c57f58c3df00e99
4
- data.tar.gz: 14466f1fa3b839d42cadebcbfe4eaeb0cd9d76c2
5
- SHA512:
6
- metadata.gz: c92bf8f091b9f09c4f8ff3f16f881a2ca7c647fdc2185646a082f556ba8e22bf2763547abef2b41d81434006b3f39fd0a8650bf032c9e9f80147870020fd83be
7
- data.tar.gz: 69ce697949fe15130a26fc760a8c590fb38b6b971df899841b9f86ecf72a6f9ade930caa00b12385a8a46472147d088e23f202274109663d94f9686989814a1a