knocker 0.2.3 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b4289625cc8b8ca43224fc4529d4472a91a2aeb
4
- data.tar.gz: eeda48021083930707232a70ead8f0d268d0aca4
3
+ metadata.gz: 4c96c78688e9f396b6164a1b2df98f3585d3b119
4
+ data.tar.gz: 80c5747c69c7d14e0a92ca187d17ccc0f8576772
5
5
  SHA512:
6
- metadata.gz: 70096d1ec8d32d66acee9e44f3a2b640d214f4ca61e7c8352dc7338f65d7048ec2acd964fb9ad9a7feaf50a9dee0ce1116ad78c588afe87336fdbd2c39a97f86
7
- data.tar.gz: 67a4544b1cc2c67de4e647b1d56ae498e7692399c61ce04d455a3ddce214b538324fc4cc0cc8810b87f33582d804df8654e010fd91e53265910f38fbfed6fe2e
6
+ metadata.gz: fc689efb5d4d067b0ffc275916a4ea37d952f705c86601764ad5720cbbc560e576e737525bd0b736914701f7a571b8b178e75be30434968f727a85f3567f4c51
7
+ data.tar.gz: e214473038ff51930d4adde2790d26a03b0308f8369428bed685a0469be4cbc62a9d81b63f21cd7f7e94659257340e54ec1047ba2b50eb603467955bb72efbe5
data/README.md CHANGED
@@ -49,7 +49,7 @@ pattern pvtchatops
49
49
  u 5445
50
50
  t 4456
51
51
  t 63854
52
- c xchat --url=irc://myvps.example.com:36777/chatops
52
+ c xchat --url=irc://$host:36777/chatops
53
53
  ```
54
54
 
55
55
  So when you run `nkr pvtchatops` it actually runs two commands:
@@ -63,10 +63,12 @@ Easy!
63
63
 
64
64
  As you saw, the config syntax is simple.
65
65
 
66
+ * use pattern and a name to name the pattern
66
67
  * h and then the hostname/ip to connect to
67
68
  * t and the port number for a tcp knock (multiples allowed)
68
69
  * u and the port number for a udp knock (multiples allowed)
69
70
  * c and the command to run after knocking (multiples allowed)
71
+ * on command lines use $host to use host defined for the pattern
70
72
 
71
73
  **Mix this with SSH config files and you will an unstoppable sysadmin!**
72
74
 
data/bin/nkr CHANGED
@@ -1,11 +1,23 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'knocker'
4
- require 'fileutils'
5
4
 
6
5
  name = ARGV[0]
7
6
 
7
+ def usage
8
+ <<-EOF
9
+ Usage: nkr <--edit|pattern-name>
10
+ EOF
11
+ end
12
+
13
+ if name == "--edit" or name == "-e"
14
+ system "#{ENV["EDITOR"]} #{Knocker.config}"
15
+ exit
16
+ end
17
+
8
18
  begin
19
+ raise ArgumentError, "Must provide pattern name" if name.nil? or name.empty?
20
+
9
21
  pattern = Knocker.pattern(name)
10
22
  system pattern.knock_command
11
23
 
@@ -14,15 +26,24 @@ begin
14
26
  end
15
27
  rescue Knocker::Errors::ConfigNotFound
16
28
  print "No config found, create one at #{Knocker.config}? [y/n]: "
17
- FileUtils.touch Knocker.config if STDIN.gets.match /y/i
29
+
30
+ if STDIN.gets.match /y/i
31
+ Knocker.create_config
32
+ puts "Created knocker config file (use --edit to edit it)"
33
+ else
34
+ abort "No config file... quitting"
35
+ end
36
+ rescue ArgumentError => e
37
+ puts "ERROR: #{e.message}"
38
+ abort usage
18
39
  rescue Knocker::Errors::PatternNotFound
19
- abort "No such pattern #{name}"
40
+ abort "ERROR: No such pattern #{name}"
20
41
  rescue Knocker::Errors::NoHostSpecified
21
- abort "No host specified in pattern #{name}"
42
+ abort "ERROR: No host specified in pattern #{name}"
22
43
  rescue Knocker::Errors::NoKnockSpecified
23
- abort "No port-knocks specified in pattern #{name}"
44
+ abort "ERROR: No port-knocks specified in pattern #{name}"
24
45
  rescue Knocker::Errors::InvalidProtocol => e
25
- abort "Invalid protocol #{e.message} in pattern #{name}"
46
+ abort "ERROR: Invalid protocol #{e.message} in pattern #{name}"
26
47
  rescue Knocker::Errors::InvalidPort => e
27
- abort "Invalid port #{e.message} in pattern #{name}"
48
+ abort "ERROR: Invalid port #{e.message} in pattern #{name}"
28
49
  end
@@ -2,6 +2,7 @@ require "knocker/version"
2
2
  require "knocker/errors"
3
3
  require "knocker/parser"
4
4
  require "knocker/pattern"
5
+ require 'fileutils'
5
6
 
6
7
  module Knocker
7
8
  class << self
@@ -10,7 +11,21 @@ module Knocker
10
11
  end
11
12
 
12
13
  def pattern(name)
13
- Knocker::Parser.new(config).find(name).to_hash
14
+ config_exists!
15
+ Knocker::Parser.new(File.read(config)).find(name)
16
+ end
17
+
18
+ def config_exists?
19
+ File.exist? config
20
+ end
21
+
22
+ def config_exists!
23
+ raise Knocker::Errors::ConfigNotFound unless config_exists?
24
+ end
25
+
26
+ def create_config
27
+ FileUtils.mkdir_p File.dirname(Knocker.config)
28
+ FileUtils.touch Knocker.config
14
29
  end
15
30
  end
16
31
  end
@@ -1,8 +1,9 @@
1
1
  module Knocker
2
2
  class Parser
3
3
  def initialize(config)
4
- raise Knocker::Errors::ConfigNotFound unless File.exist? config
5
- @config = File.read(config).reject! {|line| line.start_with? "#"}
4
+ @config = config.lines.reject { |line|
5
+ line.start_with? "#"
6
+ }.join("")
6
7
  end
7
8
 
8
9
  def find(name)
@@ -13,15 +14,16 @@ module Knocker
13
14
  private
14
15
 
15
16
  def has_pattern!(name)
16
- raise Knocker::Errors::PatterNotFound unless
17
+ raise Knocker::Errors::PatternNotFound unless
17
18
  @config.include? "pattern #{name}"
18
19
  end
19
20
 
20
21
  def extract(name)
21
- start = @config.lines.index("pattern #{name}")
22
+ start = @config.split("\n").index("pattern #{name}")
23
+ start+= 1
22
24
 
23
- @config.lines[start..-1].collect { |line|
24
- break if line.blank?
25
+ @config.split("\n")[start..-1].collect { |line|
26
+ break if line.empty?
25
27
  line
26
28
  }.join("\n")
27
29
  end
@@ -17,7 +17,8 @@ module Knocker
17
17
 
18
18
  def host
19
19
  host = @text.scan(/ h (.*)/).flatten.first
20
- raise Knocker::Errors::NoHostSpecified if host.nil? or host.blank?
20
+ raise Knocker::Errors::NoHostSpecified if host.nil? or host.empty?
21
+ host
21
22
  end
22
23
 
23
24
  def knock_command
@@ -34,9 +35,9 @@ module Knocker
34
35
  end
35
36
 
36
37
  def post_commands
37
- cmds = @text.scan(/^ c (.*)$/)
38
+ cmds = @text.scan(/^ c (.*)$/).flatten
38
39
  cmds.map! do |cmd|
39
- cmd.gsub! /\$host/, host
40
+ cmd.gsub(/\$host/, host)
40
41
  end
41
42
  end
42
43
 
@@ -1,3 +1,3 @@
1
1
  module Knocker
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Knocker::Parser do
4
+ let(:parser) { Knocker::Parser.new(config) }
5
+
6
+ it "should recognise a pattern" do
7
+ expect { parser.find("pvtchatops") }.to_not raise_error
8
+ end
9
+
10
+ it "should return a pattern object" do
11
+ expect(parser.find("pvtchatops")).to be_a Knocker::Pattern
12
+ end
13
+
14
+ it "should return a pattern object with the correct name" do
15
+ expect(parser.find("pvtchatops").name).to eq "pvtchatops"
16
+ end
17
+
18
+ it "should raise PatternNotFound for non-existent pattern" do
19
+ expect{parser.find("wut!")}.to raise_error Knocker::Errors::PatternNotFound
20
+ end
21
+
22
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Knocker::Pattern do
4
+ let(:pattern) { Knocker::Pattern.new("pvtchatops", pattern_text) }
5
+
6
+ it "should return the host" do
7
+ expect(pattern.host).to eq "myvps.example.com"
8
+ end
9
+
10
+ it "should return the knock command" do
11
+ expect(pattern.knock_command).to eq "knock myvps.example.com 5445:udp 4456:tcp 63854:tcp"
12
+ end
13
+
14
+ it "should return the commands in an array" do
15
+ expect(pattern.post_commands).to be_an Array
16
+ end
17
+
18
+ it "should substitute $host for the host name in commands" do
19
+ expect(pattern.post_commands.first).to include(pattern.host)
20
+ end
21
+
22
+ it "should return the correct post commands" do
23
+ expect(pattern.post_commands).to eq ["xchat --url=irc://myvps.example.com:36777/chatops"]
24
+ end
25
+
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'knocker'
2
+
3
+ def config
4
+ <<-EOF
5
+ # Knocker config file
6
+ # keep this secret, knock patterns are almost like passwords
7
+ pattern pvtchatops
8
+ h myvps.example.com
9
+ u 5445
10
+ t 4456
11
+ t 63854
12
+ c xchat --url=irc://$host:36777/chatops
13
+ EOF
14
+ end
15
+
16
+ def pattern_text
17
+ <<-EOF
18
+ h myvps.example.com
19
+ u 5445
20
+ t 4456
21
+ t 63854
22
+ c xchat --url=irc://$host:36777/chatops
23
+ EOF
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knocker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert McLeod
@@ -74,6 +74,9 @@ files:
74
74
  - lib/knocker/parser.rb
75
75
  - lib/knocker/pattern.rb
76
76
  - lib/knocker/version.rb
77
+ - spec/lib/knocker/parser_spec.rb
78
+ - spec/lib/knocker/pattern_spec.rb
79
+ - spec/spec_helper.rb
77
80
  homepage: https://github.com/penguinpowernz/knocker
78
81
  licenses:
79
82
  - MIT
@@ -98,4 +101,7 @@ rubygems_version: 2.2.2
98
101
  signing_key:
99
102
  specification_version: 4
100
103
  summary: Uses stored config to knocks on knock daemons
101
- test_files: []
104
+ test_files:
105
+ - spec/lib/knocker/parser_spec.rb
106
+ - spec/lib/knocker/pattern_spec.rb
107
+ - spec/spec_helper.rb