Skip to content

Commit d6b120b

Browse files
committed
implement junoser-squash
1 parent 6e90a8e commit d6b120b

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

exe/junoser-squash

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env ruby
2+
require 'optparse'
3+
require 'pathname'
4+
5+
$: << File.expand_path('../../lib', Pathname.new(__FILE__).realpath)
6+
require 'junoser/squash'
7+
8+
command = :squash
9+
opts = OptionParser.new do |opts|
10+
opts.banner = 'junoser-squash: squash config file.'
11+
opts.define_head 'Usage: junoser-squash [path]'
12+
13+
opts.on_tail '-h', '--help', 'Show this message' do
14+
puts opts
15+
exit
16+
end
17+
end
18+
opts.parse!
19+
case command
20+
when :squash
21+
puts Junoser::Squash.new($<).transform
22+
else
23+
puts opts
24+
abort
25+
end

lib/junoser/squash.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require 'junoser'
2+
require 'parslet'
3+
4+
module Junoser
5+
class DeleteTransformer < Parslet::Transform
6+
rule(config: simple(:config)) do
7+
"(#{config.to_s} .*"
8+
end
9+
10+
rule(config: sequence(:configs)) do
11+
configs.join("\n")
12+
end
13+
14+
rule(arg: simple(:arg)) do
15+
arg
16+
end
17+
18+
rule(label: simple(:label)) do
19+
")#{Regexp.escape(label.to_s)}"
20+
end
21+
22+
rule(label: simple(:label), child: simple(:child)) do
23+
"#{Regexp.escape(label.to_s)} #{child}"
24+
end
25+
26+
rule(label: simple(:label), child: sequence(:children)) do
27+
%[#{Regexp.escape(label.to_s)} #{children.join(' ')}]
28+
end
29+
30+
rule(statement: simple(:statement), argument: simple(:argument)) do
31+
"#{statement} #{argument}"
32+
end
33+
34+
rule(statement: simple(:statement), argument: sequence(:arguments)) do
35+
%[#{statement} #{arguments.join(' ')}]
36+
end
37+
38+
rule(oneline: simple(:str)) do
39+
str
40+
end
41+
42+
rule(oneline: sequence(:strs)) do
43+
strs.join(' ')
44+
end
45+
end
46+
47+
class Squash
48+
def initialize(io_or_string)
49+
@input = io_or_string
50+
@lines = []
51+
@parser = Junoser::Parser.new
52+
@transformer = DeleteTransformer.new
53+
end
54+
55+
def transform
56+
config = Junoser::Input.new(@input).read.split("\n")
57+
config.each do |l|
58+
l.strip!
59+
case l
60+
when /^set /
61+
@lines << l
62+
when /^delete /
63+
to_delete = @parser.parse(l.gsub(/^delete /, 'set '))
64+
delete_lines @transformer.apply(to_delete)
65+
end
66+
end
67+
68+
@lines.uniq!
69+
remove_subcommand(@lines).map(&:strip).join("\n")
70+
end
71+
72+
private
73+
74+
def remove_subcommand(lines)
75+
lines.each_with_index do |l,i|
76+
lines[i..-1].each do |l2|
77+
if l.include?(l2) and l != l2
78+
lines.delete(l2)
79+
end
80+
end
81+
end
82+
end
83+
84+
def delete_lines(pattern)
85+
@lines.each do |l|
86+
l.sub!(/#{pattern}/) { $1 }
87+
end
88+
end
89+
end
90+
end

test/test_junoser-squash.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'pathname'
2+
3+
$: << File.expand_path('../', Pathname.new(__FILE__).realpath)
4+
require 'helper'
5+
6+
$: << File.expand_path('../../lib', Pathname.new(__FILE__).realpath)
7+
require 'junoser/squash'
8+
9+
10+
class TestCommentLine < Test::Unit::TestCase
11+
config_subcommand = <<-EOS
12+
set interfaces em0 unit 0 family inet address 192.0.2.1/32
13+
set interfaces em0 unit 0 family inet
14+
set interfaces em100 unit 0 family inet
15+
set interfaces em100 unit 0 family inet address 192.0.2.1/32
16+
EOS
17+
18+
config_delete = <<-EOS
19+
set interfaces em0 unit 0 family inet address 192.0.2.0/32
20+
set interfaces em10 unit 10 family inet address 192.0.2.0/32
21+
set interfaces em10 unit 10 family inet mtu 1500
22+
set interfaces em10 unit 20
23+
set interfaces em100 unit 100 family inet address 192.0.2.0/32
24+
set interfaces em100 unit 200 family inet6
25+
delete interfaces em0 unit 0 family inet address 192.0.2.0/32
26+
delete interfaces em10 unit 10
27+
delete interfaces em100 unit 200
28+
EOS
29+
30+
# config_insert
31+
# ...
32+
# config_squash
33+
34+
test 'check subcommand function' do
35+
assert_equal('set interfaces em0 unit 0 family inet address 192.0.2.1/32
36+
set interfaces em100 unit 0 family inet
37+
set interfaces em100 unit 0 family inet address 192.0.2.1/32',Junoser::Squash.new(config_subcommand).transform)
38+
39+
end
40+
41+
test 'check delete function' do
42+
assert_equal('set interfaces em0 unit 0 family inet address 192.0.2.0/32
43+
set interfaces em10
44+
set interfaces em10 unit 20
45+
set interfaces em100 unit 100 family inet address 192.0.2.0/32',Junoser::Squash.new(config_delete).transform)
46+
47+
end
48+
end

0 commit comments

Comments
 (0)