tomahawk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3321497e0e37efc0950be9ece1d7b843beafb880
4
+ data.tar.gz: dd2c3437d9acf92cbb456f4e121c967e1ed8229e
5
+ SHA512:
6
+ metadata.gz: 27393b6bf0bb0072fc45746a1e941c62d4a2d92d3c22a42551317993dbbb2de11750c27015a9644c40d1b0dc10c8ee0ad4fb3d96ca755afb63d5d34d46a26d23
7
+ data.tar.gz: 80204485dcf3d2b26f80e5f1a6a558668e420042209e6b949eeb1009c190663d2e62a39cc1f89b3bb7ea16e84f2b365339ebd21a180c5f94771b2e7a07aa6388
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Christian Schell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Tomahawk
2
+
3
+ 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.
4
+
5
+ **This gem is still under an early alpha stage and may change it's API faster than you blink. Still any contributions are highly appreciated.**
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'tomahawk'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tomahawk
20
+
21
+ ## Usage
22
+ require 'tomahawk'
23
+
24
+ httpd_config = Tomahawk.parse File.read('/etc/apache2/apache.conf')
25
+
26
+ httpd_config.directives # lists parsed directives such as ServerName and LogLevel
27
+
28
+ httpd_config.groups # lists parsed directive groups such as <VirtualHost> and <Directory>
29
+
30
+ httpd_config.directives['server_name'] = 'google.com' # change a directive
31
+
32
+ httpd_config.directives['some_new_directive'] = 'foo' # create a new directive
33
+
34
+ File.write('/etc/apache2/apache.conf.new', httpd_config.to_conf) # write your new configuration back to disk
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,75 @@
1
+ module Tomahawk
2
+ class ConfigParser
3
+ attr_accessor :result
4
+ def initialize(config_string)
5
+ @config_string = config_string.to_s.strip
6
+ @result = []
7
+ end
8
+
9
+ def call
10
+ @config_lines = @config_string.split("\n")
11
+ parse
12
+ end
13
+
14
+ private
15
+
16
+ def parse
17
+
18
+ directive_groups = [Tomahawk::DirectiveGroups::HTTPd.new]
19
+
20
+ @config_lines.each do |line|
21
+
22
+ is_end_tag?(line) do
23
+ directive_groups.pop
24
+ end
25
+
26
+ is_start_tag?(line) do |group_name, group_parameters|
27
+ group = Tomahawk::DirectiveGroups.DirectiveGroup(group_name).new(group_parameters)
28
+ directive_groups.last.groups << group
29
+ directive_groups.push(group)
30
+ end
31
+
32
+ is_directive?(line) do |directive_name, directive_value|
33
+ directive_groups.last.directives[directive_name] = directive_value
34
+ end
35
+ end
36
+
37
+ @result = directive_groups.first
38
+ end
39
+
40
+ def is_end_tag?(line, &block)
41
+ if line[/<\/.+?>/]
42
+ yield if block
43
+ return true
44
+ end
45
+
46
+ false
47
+ end
48
+
49
+ def is_start_tag?(line, &block)
50
+ if line[/<\s*([^\/\s]+)(.*?)>/]
51
+ yield $1.strip, $2.strip if block
52
+ return true
53
+ end
54
+
55
+ false
56
+ end
57
+
58
+ def is_directive?(line, &block)
59
+ if line[/^\s*([^#<\s]+)(.+)$/]
60
+
61
+ if block
62
+ directive_value = $2.strip if $2
63
+ directive_name = $1.scan(/[A-Z][^A-Z\s]*/)
64
+ .map(&:downcase)
65
+ .join('_')
66
+
67
+ yield(directive_name, directive_value)
68
+ end
69
+ return true
70
+ end
71
+
72
+ false
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,23 @@
1
+ module Tomahawk
2
+ module DirectiveGroups
3
+ class Base
4
+ attr_accessor :parameters, :directives, :groups
5
+
6
+ def initialize(parameters = '', directives = {})
7
+ @parameters = parameters
8
+ @directives = Hash[directives]
9
+ @groups = []
10
+ end
11
+
12
+ def to_conf()
13
+ raise "#{self.class.name} doesn't support #conf!"
14
+ end
15
+
16
+ def ==(obj)
17
+ self.class == self.class && self.parameters == obj.parameters && self.directives == obj.directives && self.groups == obj.groups
18
+ rescue
19
+ false
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ module Tomahawk
2
+ module DirectiveGroups
3
+ class Directory < Base
4
+ def to_conf(generator = Tomahawk::Generators::Directory)
5
+ generator.new(self)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Tomahawk
2
+ module DirectiveGroups
3
+ class HTTPd < Base
4
+ def to_conf(generator = Tomahawk::Generators::HTTPd)
5
+ generator.new(self)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Tomahawk
2
+ module DirectiveGroups
3
+ class VirtualHost < Base
4
+ def to_conf(generator = Generators::VirtualHost)
5
+ generator.new(self)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'directive_groups/base'
2
+ require_relative 'directive_groups/httpd'
3
+ require_relative 'directive_groups/virtual_host'
4
+ require_relative 'directive_groups/directory'
5
+
6
+ module Tomahawk
7
+ module DirectiveGroups
8
+ extend self
9
+
10
+ def DirectiveGroup(directive_group_name)
11
+ directive_group_name = String(directive_group_name)
12
+
13
+ Kernel.const_get('Tomahawk::DirectiveGroups::%s' % [directive_group_name])
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ module Tomahawk
2
+ module Generators
3
+ class Base
4
+ include Generators
5
+
6
+ def initialize(directive_group)
7
+ @directive_group = directive_group
8
+ end
9
+
10
+ def to_str
11
+ config = "\n<#{directive_group_name} #{@directive_group.parameters}>\n"
12
+
13
+ @directive_group.directives.each do |directive, value|
14
+ config += generate_directive(directive, value)
15
+ end
16
+
17
+ @directive_group.groups.each do |group|
18
+ config += group.to_conf
19
+ end
20
+
21
+ config += "\n</#{directive_group_name}>\n"
22
+ end
23
+
24
+ alias_method :to_s, :to_str
25
+ end
26
+
27
+ def directive_group_name
28
+ @directive_group.class.name.split('::').last
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ module Tomahawk
2
+ module Generators
3
+ class Directory < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ module Tomahawk
2
+ module Generators
3
+ class HTTPd < Base
4
+
5
+ def initialize(httpd)
6
+ @httpd = httpd
7
+ end
8
+
9
+ def to_str
10
+ config = ''
11
+
12
+ @httpd.directives.each do |directive, value|
13
+ config += generate_directive(directive, value)
14
+ end
15
+
16
+ @httpd.groups.each do |group|
17
+ config += group.to_str
18
+ end
19
+
20
+ config
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,6 @@
1
+ module Tomahawk
2
+ module Generators
3
+ class VirtualHost < Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'generators/base'
2
+ require_relative 'generators/httpd'
3
+ require_relative 'generators/virtual_host'
4
+ require_relative 'generators/directory'
5
+
6
+ module Tomahawk
7
+ module Generators
8
+ def generate_directive(directive, value)
9
+ "\n %s %s" % [directive.to_s.split('_').map(&:capitalize).join, value]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Tomahawk
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tomahawk.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "tomahawk/version"
2
+ require "tomahawk/directive_groups"
3
+ require "tomahawk/generators"
4
+ require "tomahawk/config_parser"
5
+
6
+ module Tomahawk
7
+ extend self
8
+
9
+ def parse(input)
10
+ ConfigParser.new(input).call
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tomahawk::ConfigParser do
4
+ it "parses Vhost files correctly" do
5
+ google_com = Tomahawk::DirectiveGroups::VirtualHost.new('173.194.35.174:80', 'server_name' => 'google.com', 'server_alias' => 'www.google.com', 'document_root' => '/var/www/google.com')
6
+ google_com.groups << Tomahawk::DirectiveGroups::Directory.new('/var/www/google.com', 'options' => '-Indexes FollowSymLinks MultiViews', 'allow_override' => 'AuthConfig Limit Indexes Options=All,MultiViews FileInfo')
7
+
8
+ github_com = Tomahawk::DirectiveGroups::VirtualHost.new('192.30.252.131:80', 'server_name' => 'github.com', 'server_alias' => 'www.github.com github.io', 'document_root' => '/var/www/github.com')
9
+ github_com.groups << Tomahawk::DirectiveGroups::Directory.new('/var/www/github.com', 'options' => '-Indexes FollowSymLinks MultiViews', 'allow_override' => 'AuthConfig Indexes Options=All FileInfo')
10
+ github_com.groups << Tomahawk::DirectiveGroups::Directory.new('/var/www/github.com/private', 'order' => 'allow, deny', 'allow' => 'from all')
11
+
12
+ config = Tomahawk::ConfigParser.new(google_com.to_conf.to_s + github_com.to_conf.to_s)
13
+
14
+
15
+ config.call
16
+
17
+ expect(config.result.groups).to eq([google_com, github_com])
18
+ end
19
+ end
20
+
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ def strip_whitespaces(str)
4
+ String(str).gsub(/^\s+/, '').gsub(/\n{2,}/, '\n')
5
+ end
6
+
7
+ describe Tomahawk::DirectiveGroups::VirtualHost do
8
+ describe '.new' do
9
+ it 'initializes correctly' do
10
+ address = '*:80'
11
+
12
+ server_name = 'example.com'
13
+ document_root = '/tmp/tomahawk/test'
14
+
15
+ vhost = Tomahawk::DirectiveGroups::VirtualHost.new(address, { server_name: server_name, document_root: document_root })
16
+
17
+ expect(vhost.directives[:server_name]).to eq(server_name)
18
+ expect(vhost.directives[:document_root]).to eq(document_root)
19
+ end
20
+ end
21
+
22
+ describe '#to_conf' do
23
+ it 'prints attributes in Apache Conf format' do
24
+ address = '*:80'
25
+
26
+ server_name = 'example.com'
27
+ document_root = '/tmp/tomahawk/test'
28
+
29
+ vhost = Tomahawk::DirectiveGroups::VirtualHost.new(address, { server_name: server_name, document_root: document_root })
30
+
31
+ expect(strip_whitespaces(vhost.to_conf)).to eq(strip_whitespaces(<<-CONF))
32
+ <VirtualHost #{address}>
33
+ ServerName #{server_name}
34
+
35
+ DocumentRoot #{document_root}
36
+ </VirtualHost>
37
+ CONF
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tomahawk::DirectiveGroups do
4
+ describe '.DirectiveGroup' do
5
+ it 'returns corresponding DirectiveGroup if String given' do
6
+ expect(Tomahawk::DirectiveGroups.DirectiveGroup('VirtualHost')).to eq(Tomahawk::DirectiveGroups::VirtualHost)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tomahawk do
4
+ describe '.parse' do
5
+ it 'delegates it' do
6
+ input = 'some input'
7
+ expect_any_instance_of(Tomahawk::ConfigParser).to receive(:call)
8
+
9
+ Tomahawk.parse(input)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require 'tomahawk'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
data/tomahawk.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tomahawk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tomahawk"
8
+ spec.version = Tomahawk::VERSION
9
+ spec.authors = ["Christian Schell"]
10
+ spec.email = ["mail@chrisschell.de"]
11
+ spec.summary = 'Tomahawk parses and generates Apache 2 configuration files.'
12
+ spec.description = nil
13
+ spec.homepage = 'https://github.com/cschell/tomahawk'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tomahawk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Christian Schell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: ''
56
+ email:
57
+ - mail@chrisschell.de
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-version"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/tomahawk.rb
70
+ - lib/tomahawk/config_parser.rb
71
+ - lib/tomahawk/directive_groups.rb
72
+ - lib/tomahawk/directive_groups/base.rb
73
+ - lib/tomahawk/directive_groups/directory.rb
74
+ - lib/tomahawk/directive_groups/httpd.rb
75
+ - lib/tomahawk/directive_groups/virtual_host.rb
76
+ - lib/tomahawk/generators.rb
77
+ - lib/tomahawk/generators/base.rb
78
+ - lib/tomahawk/generators/directory.rb
79
+ - lib/tomahawk/generators/httpd.rb
80
+ - lib/tomahawk/generators/virtual_host.rb
81
+ - lib/tomahawk/version.rb
82
+ - spec/lib/tomahawk/config_parser_spec.rb
83
+ - spec/lib/tomahawk/directive_groups/virtual_host_spec.rb
84
+ - spec/lib/tomahawk/directive_groups_spec.rb
85
+ - spec/lib/tomahawk_spec.rb
86
+ - spec/spec_helper.rb
87
+ - tomahawk.gemspec
88
+ homepage: https://github.com/cschell/tomahawk
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.0
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Tomahawk parses and generates Apache 2 configuration files.
112
+ test_files:
113
+ - spec/lib/tomahawk/config_parser_spec.rb
114
+ - spec/lib/tomahawk/directive_groups/virtual_host_spec.rb
115
+ - spec/lib/tomahawk/directive_groups_spec.rb
116
+ - spec/lib/tomahawk_spec.rb
117
+ - spec/spec_helper.rb