junoser 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/exe/junoser CHANGED
@@ -5,7 +5,7 @@ require 'pathname'
5
5
 
6
6
  $: << File.expand_path('../../lib', Pathname.new(__FILE__).realpath)
7
7
 
8
- require 'junoser/cli'
8
+ require 'junoser'
9
9
 
10
10
 
11
11
  command = nil
@@ -34,6 +34,7 @@ opts = OptionParser.new do |opts|
34
34
 
35
35
  opts.on_tail '-v', '--version', 'Show version' do
36
36
  puts Junoser::VERSION
37
+ exit
37
38
  end
38
39
  end
39
40
  opts.parse!
@@ -42,9 +43,9 @@ case command
42
43
  when :check
43
44
  abort unless Junoser::Cli.commit_check($<)
44
45
  when :display_set
45
- Junoser::Cli.display_set $<
46
+ puts Junoser::Cli.display_set($<)
46
47
  when :struct
47
- Junoser::Cli.struct $<
48
+ puts Junoser::Cli.struct($<)
48
49
  else
49
50
  puts opts
50
51
  abort
data/junoser.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{PEG parser for JUNOS configuration.}
13
13
  spec.description = %q{PEG parser to vefiry and translate into different formats for JUNOS configuration.}
14
- spec.homepage = "https://github.com/codeout/sakura-cli"
14
+ spec.homepage = "https://github.com/codeout/junoser"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
17
  spec.bindir = "exe"
@@ -24,4 +24,5 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "activesupport"
26
26
  spec.add_development_dependency "nokogiri"
27
+ spec.add_development_dependency "test-unit"
27
28
  end
data/lib/junoser/cli.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'parslet'
2
2
  require 'junoser/display'
3
+ require 'junoser/input'
3
4
  require 'junoser/parser'
4
5
 
5
6
 
@@ -7,11 +8,7 @@ module Junoser
7
8
  module Cli
8
9
  class << self
9
10
  def commit_check(io_or_string)
10
- config = if io_or_string.respond_to?(:read)
11
- io_or_string.read
12
- else
13
- io_or_string.to_s
14
- end
11
+ config = Junoser::Input.new(io_or_string).read
15
12
 
16
13
  if Junoser::Display.display_set?(config)
17
14
  commit_check_display_set config
@@ -37,18 +34,18 @@ module Junoser
37
34
 
38
35
  def commit_check_display_set(config)
39
36
  parser = Junoser::Parser.new
40
- failed = false
37
+ passed = true
41
38
 
42
39
  config.split("\n").each do |line|
43
40
  begin
44
41
  parser.parse line
45
42
  rescue Parslet::ParseFailed
46
43
  $stderr.puts "Invalid syntax: #{line}"
47
- failed = true
44
+ passed = false
48
45
  end
49
46
  end
50
47
 
51
- abort if failed
48
+ passed
52
49
  end
53
50
  end
54
51
  end
@@ -1,15 +1,21 @@
1
1
  require 'parslet'
2
- require 'junoser/display/base'
2
+ require 'junoser/input'
3
3
 
4
4
  module Junoser
5
5
  module Display
6
6
  class Set
7
- include Base
7
+ def initialize(io_or_string)
8
+ @input = io_or_string
9
+ end
8
10
 
9
11
  def transform
12
+ result = ''
13
+
10
14
  process do |current_stack, str|
11
- @output.puts transform_line(current_stack, str)
15
+ result << transform_line(current_stack, str) << "\n"
12
16
  end
17
+
18
+ result
13
19
  end
14
20
 
15
21
  def commit_check(&block)
@@ -40,7 +46,7 @@ module Junoser
40
46
  def process(&block)
41
47
  stack = []
42
48
 
43
- read_io_or_string.split("\n").each do |line|
49
+ Junoser::Input.new(@input).read.split("\n").each do |line|
44
50
  case line
45
51
  when /(.*){/
46
52
  stack.push $1.strip
@@ -1,3 +1,4 @@
1
+ require 'junoser/input'
1
2
  require 'junoser/display/config_store'
2
3
  require 'junoser/parser'
3
4
  require 'junoser/transformer'
@@ -5,10 +6,8 @@ require 'junoser/transformer'
5
6
  module Junoser
6
7
  module Display
7
8
  class Structure
8
- include Base
9
-
10
9
  def initialize(io_or_string)
11
- super
10
+ @input = io_or_string
12
11
  @config = Junoser::Display::ConfigStore.new
13
12
  end
14
13
 
@@ -16,13 +15,13 @@ module Junoser
16
15
  parser = Junoser::Parser.new
17
16
  transform = Junoser::Transformer.new
18
17
 
19
- read_io_or_string.split("\n").each do |line|
18
+ Junoser::Input.new(@input).read.split("\n").each do |line|
20
19
  transformed = transform.apply(parser.parse(line))
21
20
  raise "ERROR: parse failed" unless transformed.is_a?(String)
22
21
  @config << transformed
23
22
  end
24
23
 
25
- @output.puts @config.to_s
24
+ @config.to_s
26
25
  end
27
26
  end
28
27
  end
@@ -0,0 +1,34 @@
1
+ module Junoser
2
+ class Input
3
+ def initialize(io_or_string)
4
+ @io_or_string = io_or_string
5
+ end
6
+
7
+ def read
8
+ content = if @io_or_string.respond_to?(:read)
9
+ @io_or_string.read
10
+ else
11
+ @io_or_string.to_s
12
+ end
13
+
14
+ content = remove_blank_and_comment_line(content)
15
+ content = unify_carriage_return(content)
16
+ end
17
+
18
+
19
+ private
20
+
21
+ # As for comment line, a trailing comment after configuration will be processed by parslet
22
+ def remove_blank_and_comment_line(str)
23
+ str.gsub! /^\s*#.*/, ''
24
+ str.gsub! /^\s*\/\*((?!\*\/).)*\*\//m, ''
25
+ str.gsub! /\n\s*/, "\n"
26
+ str.strip
27
+ end
28
+
29
+ def unify_carriage_return(str)
30
+ str.gsub! /\r\n?/, "\n"
31
+ str
32
+ end
33
+ end
34
+ end