yapfac 0.1.0 → 0.2.0

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.
data/.travis.yml CHANGED
@@ -2,3 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  before_install: gem install bundler -v 1.10.4
5
+
6
+ addons:
7
+ code_climate:
8
+ repo_token: e8fed04b00f6d0616f413bd35be0423670a295988435bd3cf26c5b9c84bd25f9
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # YAPFAC: Yet Another Parser for Apache Configuration
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/yapfac.svg)](https://badge.fury.io/rb/yapfac)
4
+ [![Build Status](https://travis-ci.org/eiwi1101/yapfac.svg)](https://travis-ci.org/eiwi1101/yapfac)
5
+ [![Code Climate](https://codeclimate.com/github/eiwi1101/yapfac/badges/gpa.svg)](https://codeclimate.com/github/eiwi1101/yapfac)
6
+ [![Test Coverage](https://codeclimate.com/github/eiwi1101/yapfac/badges/coverage.svg)](https://codeclimate.com/github/eiwi1101/yapfac/coverage)
7
+
3
8
  YAPFAC provides a parser to read and write Apache configuration files. YAPFAC also provides a secure RESTful API for doing this remotely, providing seamless controll of a number of Apache servers.
4
9
 
5
10
  ## Installation
@@ -7,7 +12,7 @@ YAPFAC provides a parser to read and write Apache configuration files. YAPFAC al
7
12
  Add this line to your application's Gemfile:
8
13
 
9
14
  ```ruby
10
- gem 'yapfac', git: 'eiwi1101/yapfac'
15
+ gem 'yapfac'
11
16
  ```
12
17
 
13
18
  And then execute:
@@ -40,21 +45,33 @@ default = Yapfac::Apache::Site.new("/etc/apache2/sites-available/000-default.con
40
45
  puts default.to_s #=> Converts the config back into Apache Config format.
41
46
  puts default.to_h #=> Converts the config into a hash.
42
47
 
43
- sites = Yapfac::Apache.sites_available
44
- puts sites.first.to_s #=> Dumps the configuration file for the first site in sites-available
45
-
46
- online = Yapfac::Apache.sites_enabled #=> Just reads the sites-enabled dir.
47
-
48
- ### COMING SOON
48
+ # This will dump all the sites available to your terminal
49
+ # As the re-generated Apache config and JSON pretty format.
50
+ #
51
+ Yapfac::Apache.sites_available.each do |site|
52
+ v = Yapfac::Apache.load_site(site)
53
+
54
+ puts "=" * 80
55
+ puts v.name
56
+ puts "-" * 80
57
+ puts v.to_s
58
+ puts "-" * 80
59
+ puts JSON.pretty_generate v.to_h
60
+ end
49
61
 
50
- Yapfac::Apache.a2ensite "example.com.conf" #=> Enables site
51
- Yapfac::Apache.a2dissite "000-default.conf" #=> Disables site
62
+ # This will build and save a new basic site to your sites-available
63
+ # directory.
64
+ #
65
+ s = Yapfac::Apache.new_site('001-example.com') do |site|
66
+ site.add_directive "DocumentRoot /var/www/example"
52
67
 
53
- # Self-explanatory
54
- Yapfac::Apache.reload
55
- Yapfac::Apache.restart
68
+ site.add_scope "Directory", "/var/www/example" do |scope|
69
+ scope.add_directive "Order", "allow,deny"
70
+ scope.add_directive "Allow", "from", "all"
71
+ end
72
+ end
56
73
 
57
- Yapfac::Apache.a2ensite! "example.com.conf" #=> Enables site and reloads Apache. Same for ::a2dissite.
74
+ s.save #=> Writes output to sites-available
58
75
  ```
59
76
 
60
77
  ## Development
@@ -69,6 +86,11 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
69
86
 
70
87
  Bug reports and pull requests are welcome on GitHub at https://github.com/eiwi1101/yapfac. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
71
88
 
89
+ ## Contact
90
+
91
+ This gem is maintained by William Eisert [weisert@eisertdev.com](mailto:weisert@eisertdev.com).
92
+
93
+ <a href='https://ko-fi.com?i=15867V22TVFEL' target='_blank'><img style='border:0px;width:140px;' src='https://az743702.vo.msecnd.net/cdn/btn1.png' border='0' alt='Buy me a coffee at ko-fi.com' width="150" /></a>
72
94
 
73
95
  ## License
74
96
 
data/bin/quicktest CHANGED
@@ -5,7 +5,7 @@ require 'yapfac'
5
5
  require 'json'
6
6
 
7
7
  Yapfac::Apache.sites_available.each do |site|
8
- v = Yapfac::Apache::Site.new(site)
8
+ v = Yapfac::Apache.load_site(site)
9
9
 
10
10
  puts "=" * 80
11
11
  puts v.name
data/lib/yapfac.rb CHANGED
@@ -24,5 +24,9 @@ module Yapfac
24
24
  @sites_available_path = "sites-available"
25
25
  @sites_enabled_path = "sites-enabled"
26
26
  end
27
+
28
+ def reset!
29
+ initialize
30
+ end
27
31
  end
28
32
  end
data/lib/yapfac/apache.rb CHANGED
@@ -13,11 +13,24 @@ class Apache
13
13
  end
14
14
 
15
15
  def self.sites_available
16
- Dir[File.join(Yapfac.configuration.apache_path, Yapfac.configuration.sites_available_path, '*.conf')]
16
+ Dir[File.join(Yapfac.configuration.apache_path, Yapfac.configuration.sites_available_path, '*.conf')].collect { |f| File.basename(f, '.conf') }
17
17
  end
18
18
 
19
19
  def self.sites_enabled
20
- Dir[File.join(Yapfac.configuration.apache_path, Yapfac.configuration.sites_enabled_path, '*.conf')]
20
+ Dir[File.join(Yapfac.configuration.apache_path, Yapfac.configuration.sites_enabled_path, '*.conf')].collect { |f| File.basename(f, '.conf') }
21
+ end
22
+
23
+ def self.load_site(site_name)
24
+ file = File.join(Yapfac.configuration.apache_path, Yapfac.configuration.sites_available_path, site_name + '.conf')
25
+
26
+ return nil unless File.exists? file
27
+ return Yapfac::Apache::Site.load(file)
28
+ end
29
+
30
+ def self.new_site(site_name)
31
+ site = Yapfac::Apache::Site.new(site_name)
32
+ yield site
33
+ return site
21
34
  end
22
35
 
23
36
  end
@@ -4,11 +4,51 @@ class Directive
4
4
 
5
5
  attr_accessor :name, :params
6
6
 
7
- def initialize(name, params = nil)
7
+ # Initialize a new Directive, given an array and arbitrary number of params.
8
+ #
9
+ # @param [String] name The directive name (DocumentRoot, etc.)
10
+ # @param [Array] *params Any number of parameters for the directive.
11
+ #
12
+ def initialize(name, *params)
8
13
  @name = name
9
- @params = parse(params)
14
+ @params = params
10
15
  end
11
16
 
17
+ # Parses a directive string into a directive object. This is most often used
18
+ # when reading in from a file.
19
+ #
20
+ # @param [String] name The directive name (DocumentRoot, etc.)
21
+ # @param [String] params A string of directive parameters to parse.
22
+ #
23
+ # @overload parse(line)
24
+ # @param [String] line The full directive line to parse, with or without
25
+ # params.
26
+ #
27
+ # @return [Yapfac::Apache::Directive] New and parsed directive instance.
28
+ #
29
+ # @example Known Name
30
+ # d = Yapfac::Apache::Directive.parse("DocumentRoot", "/www/html")
31
+ #
32
+ # @example Full Line Parse
33
+ # d = Yapfac::Apache::Directive.parse("DocumentRoot /www/html")
34
+ #
35
+ # @example Parse Multiple Params
36
+ # # The real purpose of this method.
37
+ # d1 = Yapfac::Apache::Directive.parse("Deny from all")
38
+ # d2 = Yapfac::Apache::Directive.parse("Deny", "from all")
39
+ #
40
+ # # d1.params == d2.params == ["from", "all"]
41
+ #
42
+ def self.parse(name, params = nil)
43
+ name, params = name.split /\s+/, 2 if params.nil?
44
+ return Yapfac::Apache::Directive.new(name, *parse_params(params))
45
+ end
46
+
47
+ # Builds a string representation of the directive, which is valid for
48
+ # storing in an Apache configuration file.
49
+ #
50
+ # @return [String] String representation of Apache Directive
51
+ #
12
52
  def to_s
13
53
  "#{@name} #{@params.collect do |p|
14
54
  if p =~ /\s/
@@ -19,6 +59,15 @@ class Directive
19
59
  end.join(' ')}"
20
60
  end
21
61
 
62
+ # Builds a hash representation of the Apache Directive, useful for
63
+ # serialization.
64
+ #
65
+ # @return [Hash] A hash with +:name+ (String) and +:params+ (array) keys.
66
+ #
67
+ # @example Hashify an Apache Directive
68
+ # d = Yapfac::Apache::Directive.parse("Deny from all")
69
+ # d.to_h #=> { name: "Deny", params: [ "from", "all" ] }
70
+ #
22
71
  def to_h
23
72
  return ({
24
73
  name: @name,
@@ -28,7 +77,11 @@ class Directive
28
77
 
29
78
  private
30
79
 
31
- def parse(params)
80
+ # Tokenizes params, keeping quoted values together.
81
+ # @param [String] params Params string to tokenize
82
+ # @return [Array] Tokenized params
83
+ #
84
+ def self.parse_params(params = nil)
32
85
  return Array.new if params.nil?
33
86
 
34
87
  return params.
@@ -2,7 +2,12 @@ module Yapfac
2
2
  class Apache
3
3
  class Scope
4
4
 
5
- attr_reader :name, :params, :parent
5
+ attr_reader :directives
6
+ attr_reader :scopes
7
+
8
+ attr_accessor :parent
9
+ attr_accessor :name
10
+ attr_accessor :params
6
11
 
7
12
  def initialize(name = nil, params = nil, parent = nil)
8
13
  @name = name
@@ -13,16 +18,30 @@ class Scope
13
18
  @scopes = Array.new
14
19
  end
15
20
 
16
- def add_directive(line)
17
- @directives.push(line)
21
+ def add_directive(directive, *params)
22
+ if directive.kind_of? Yapfac::Apache::Directive
23
+ @directives.push(directive)
24
+ else
25
+ @directives.push(Yapfac::Apache::Directive.new(directive, *params))
26
+ end
18
27
  end
19
28
 
20
- def add_scope(scope)
21
- if scope == self
22
- raise "Scope can not be a child of itsself."
29
+ def add_scope(scope, *params)
30
+ if scope.kind_of? Yapfac::Apache::Scope
31
+ if scope == self
32
+ raise "Scope can not be a child of itsself."
33
+ end
34
+
35
+ scope.parent = self
36
+ @scopes.push(scope)
37
+ else
38
+ scope = Yapfac::Apache::Scope.new(scope, *params, self)
39
+ @scopes.push(scope)
23
40
  end
24
41
 
25
- @scopes.push(scope)
42
+ if block_given?
43
+ yield scope
44
+ end
26
45
  end
27
46
 
28
47
  def to_h
@@ -40,7 +59,7 @@ class Scope
40
59
  out = Array.new
41
60
  tab = @parent.nil? ? "" : "\t"
42
61
 
43
- unless parent.nil?
62
+ unless self.kind_of? Yapfac::Apache::Site
44
63
  out << "<#{@name} #{@params.join(' ')}>"
45
64
  end
46
65
 
@@ -52,7 +71,7 @@ class Scope
52
71
  out << @scopes.collect { |s| s.to_s.split("\n").collect { |v| v.prepend(tab) }.join("\n") }
53
72
  end
54
73
 
55
- unless parent.nil?
74
+ unless self.kind_of? Yapfac::Apache::Site
56
75
  out << "</#{@name}>"
57
76
  end
58
77
 
@@ -2,25 +2,36 @@ module Yapfac
2
2
  class Apache
3
3
  class Site < Scope
4
4
 
5
- def initialize(filename)
6
- super()
7
- @name = File.basename(filename, '.conf')
8
- @config_lines = read_file(filename)
9
- parse!
5
+ attr_reader :config_lines
6
+
7
+ def initialize(site_name)
8
+ super(site_name)
10
9
  end
11
10
 
12
- private
11
+ def self.load(filename)
12
+ s = Yapfac::Apache::Site.new(File.basename(filename, '.conf'))
13
+ s.load_file(filename)
14
+ s.parse!
15
+ return s
16
+ end
13
17
 
14
- def read_file(filename)
15
- lines = File.read(filename)
16
- lines.gsub!("\\\n", '')
18
+ def filename
19
+ File.join(Yapfac.configuration.apache_path, Yapfac.configuration.sites_available_path, @name + '.conf')
20
+ end
17
21
 
18
- lines_a = lines.split("\n").map(&:strip!)
19
- lines_a.reject! { |l| l =~ /^\s*(?:#.*)?$/ }
22
+ def save
23
+ File.write(filename, to_s)
24
+ end
25
+
26
+ def load_file(filename)
27
+ lines = File.read(filename)
28
+ lines.gsub!("\\\n", ' ')
29
+ lines_a = lines.split("\n").map(&:strip)
30
+ lines_a.reject! { |l| l =~ /^\s*#.*$/ }
20
31
  lines_a.reject! &:nil?
21
32
  lines_a.map { |l| l.gsub! /\s+/, ' ' }
22
33
 
23
- return lines_a
34
+ @config_lines = lines_a
24
35
  end
25
36
 
26
37
  def parse!
@@ -39,7 +50,7 @@ private
39
50
 
40
51
  # Add Directive
41
52
  elsif line =~ /^(\w+)\s*(.*)$/
42
- directive = Yapfac::Apache::Directive.new($1, $2)
53
+ directive = Yapfac::Apache::Directive.parse($1, $2)
43
54
  scope.add_directive(directive)
44
55
  end
45
56
  end
@@ -1,3 +1,3 @@
1
1
  module Yapfac
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/yapfac.gemspec CHANGED
@@ -22,4 +22,9 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.10"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec-collection_matchers"
26
+ spec.add_development_dependency "fuubar"
27
+ spec.add_development_dependency "codeclimate-test-reporter"
28
+
29
+ spec.add_dependency "sinatra"
25
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yapfac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - William Eisert
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-28 00:00:00.000000000 Z
11
+ date: 2016-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,62 @@ dependencies:
52
52
  - - ! '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-collection_matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fuubar
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sinatra
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
55
111
  description: Parsing and manipulation gem for Apache configuration files. Can also
56
112
  be used to control remote servers using a secure RESTful API. Think, Webmin.
57
113
  email:
@@ -60,8 +116,10 @@ executables: []
60
116
  extensions: []
61
117
  extra_rdoc_files: []
62
118
  files:
119
+ - .codeclimate.yml
63
120
  - .gitignore
64
121
  - .rspec
122
+ - .rubocop.yml
65
123
  - .travis.yml
66
124
  - CODE_OF_CONDUCT.md
67
125
  - Gemfile
@@ -103,3 +161,4 @@ signing_key:
103
161
  specification_version: 4
104
162
  summary: Yet Another Parser for Apache Configuration
105
163
  test_files: []
164
+ has_rdoc: