sshez 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ require 'singleton'
2
+
3
+ module Sshez
4
+ # Just a printing service that keeps track by all its output
5
+ # Mainly used for testing purposes
6
+ class PrintingManager
7
+ include Singleton
8
+ # An attribute reader to store printing logs
9
+ attr_reader :output
10
+
11
+ def initialize
12
+ @output = ''
13
+ @verbose = false
14
+ end
15
+
16
+ # adds to output then prints
17
+ def print(text)
18
+ @output += %(#{text}\n)
19
+ puts text
20
+ end # print(text)
21
+
22
+ #
23
+ # prints only if verbose set to true
24
+ #
25
+ def verbose_print(text)
26
+ @output += %(#{text}\n)
27
+ puts text if @verbose
28
+ end
29
+
30
+ #
31
+ # Did we print anything?
32
+ #
33
+ def output?
34
+ !@output.empty?
35
+ end
36
+
37
+ #
38
+ # Let the flooding begin!
39
+ #
40
+ def verbose!
41
+ @verbose = true
42
+ end
43
+
44
+ #
45
+ # Resets the printer for testing purposes
46
+ #
47
+ def clear!
48
+ @output = ''
49
+ @verbose = false
50
+ end
51
+ end # PrintingManager
52
+ end
@@ -0,0 +1,60 @@
1
+ module Sshez
2
+ #
3
+ # Our main class
4
+ # This will #process the command of the user
5
+ #
6
+ # Sshez::Runner.new.process(ARGV)
7
+ #
8
+ # Acts as the listener that the +Exec+ needs
9
+ # * :argument_error(+Command+)
10
+ # * :done_with_no_guarantee
11
+ # * :permission_error
12
+ # * :finished_successfully
13
+ #
14
+ #
15
+ #
16
+ class Runner
17
+ PRINTER = PrintingManager.instance
18
+
19
+ #
20
+ # Main method of the application
21
+ # takes un processed ARGS and pass it to our parser to start our processing
22
+ #
23
+ def process(args)
24
+ parser = Parser.new(Exec.new(self))
25
+ parser.parse(args)
26
+ PRINTER.output
27
+ end
28
+
29
+ #
30
+ # We've finished everything successfully
31
+ #
32
+ def finished_successfully
33
+ PRINTER.print 'Terminated Successfully!'
34
+ end
35
+
36
+ #
37
+ # Handles when the config file could not be accessed due to a problem in permissions
38
+ #
39
+ def permission_error
40
+ PRINTER.print "Premission denied!\nPlease check your ~/.ssh/config permissions then try again."
41
+ end
42
+
43
+ #
44
+ # Returns the appropriate error messages to the given command
45
+ #
46
+ def argument_error(command)
47
+ PRINTER.print(command.error)
48
+ end
49
+
50
+ #
51
+ # When no valid command was supplied (maybe only an option)
52
+ #
53
+ def done_with_no_guarantee
54
+ unless PRINTER.output?
55
+ PRINTER.print('Invalid input. Use -h for help')
56
+ end
57
+ end
58
+
59
+ end # class Runner
60
+ end
data/lib/sshez/version.rb CHANGED
@@ -1,3 +1,6 @@
1
1
  module Sshez
2
- VERSION = "0.3.0"
2
+ #
3
+ # Returns version
4
+ #
5
+ VERSION = "1.0.0"
3
6
  end
data/spec/sshez_spec.rb CHANGED
@@ -2,14 +2,14 @@ require 'spec_helper'
2
2
 
3
3
  describe Sshez do
4
4
 
5
- subject { Sshez::Exec.new }
6
- it 'has a version number' do
7
- expect(Sshez::VERSION).not_to be nil
8
- end
5
+ subject { Sshez::Runner.new }
9
6
 
10
7
  describe '#process' do
11
- let(:input) { 'google root@74.125.224.72 -p 80 -t' }
12
- let(:output) { subject.process(input.split(" ")) }
8
+ let(:input) { %w{add google root@74.125.224.72 -p 80 -t -b} }
9
+ let(:output) do
10
+ Sshez::PrintingManager.instance.clear!
11
+ subject.process(input)
12
+ end
13
13
 
14
14
  it 'begins with what it does' do
15
15
  expect(output).to start_with "Adding"
@@ -21,61 +21,108 @@ describe Sshez do
21
21
  expect(output).to match /Port 80/i
22
22
  end
23
23
 
24
+ it 'adds batch mode option' do
25
+ expect(output).to match /BatchMode yes/i
26
+ end
27
+
24
28
  it 'always appends "Done" if succeeds' do
25
- expect(output).to end_with 'Done!'
29
+ expect(output).to end_with "Terminated Successfully!\n"
26
30
  end
27
31
  end
28
32
 
29
33
  describe "fails" do
30
- let(:input) { 'root@74.125.224.72' }
31
- let(:output) { subject.process(input.split(" ")) }
34
+ let(:input) { %w{root@74.125.224.72} }
35
+ let(:output) do
36
+ Sshez::PrintingManager.instance.clear!
37
+ subject.process(input)
38
+ end
32
39
 
33
40
  it 'and printes start' do
34
41
  expect(output).to start_with "Invalid input"
35
42
  end
36
43
 
37
44
  it 'and then asks for help at the end' do
38
- expect(output).to end_with 'Use -h for help'
45
+ expect(output).to end_with "Use -h for help\n"
39
46
  end
40
47
 
41
48
  end
42
49
 
43
50
  describe "help works" do
44
51
  let(:input) { '-h' }
45
- let(:output) { subject.process(input.split(" ")) }
52
+ let(:output) do
53
+ Sshez::PrintingManager.instance.clear!
54
+ subject.process([input])
55
+ end
46
56
 
47
- it 'prints but outputs nothing' do
48
- expect(output).to eq nil
57
+ it 'prints but ending with this message' do
58
+ expect(output).to end_with "Show this message\n\n"
49
59
  end
50
60
  end
51
61
 
52
62
  describe "remove" do
53
- before { subject.process(%w{google root@74.12.32.42 -p 30}) }
54
- let(:input) { "google -r" }
55
- let(:output) { subject.process(input.split(" ")) }
63
+ before { subject.process(%w{add google root@74.12.32.42 -p 30}) }
64
+ let(:input) { %w{remove google} }
65
+ let(:output) do
66
+ Sshez::PrintingManager.instance.clear!
67
+ subject.process(input)
68
+ end
56
69
 
57
70
  it 'ends with 30' do
58
- expect(output).to end_with "30\n"
71
+ expect(output).to end_with "30\n\nTerminated Successfully!\n"
72
+ end
73
+ end
74
+
75
+ describe "remove and try to connect" do
76
+ before do
77
+ subject.process(%w{add google root@74.12.32.42 -p 30})
78
+ subject.process(%w{remove google})
79
+ end
80
+
81
+ let(:input) { %w{connect google} }
82
+
83
+ let(:output) do
84
+ Sshez::PrintingManager.instance.clear!
85
+ subject.process(input)
86
+ end
87
+
88
+ it 'fails to connect to removed host' do
89
+ expect(output).to end_with "Could not find host `google`\n"
59
90
  end
60
91
  end
61
92
 
62
93
  describe "list works" do
63
- before { subject.process(%w{google root@74.12.32.42 -p 30}) }
64
- let(:input) { "list" }
65
- let(:output) { subject.process(input.split(" ")) }
66
- after { subject.process(%w{google -r}) }
94
+ before { subject.process(%w{add google root@74.12.32.42 -p 30}) }
95
+ let(:input) { %w{list} }
96
+ let(:output) do
97
+ Sshez::PrintingManager.instance.clear!
98
+ subject.process(input)
99
+ end
100
+ after { subject.process(%w{remove google}) }
67
101
 
68
102
  it 'contains added alias' do
69
- expect(output).to end_with "google\n"
103
+ expect(output).to end_with "google\nTerminated Successfully!\n"
70
104
  end
71
105
  end
72
106
 
107
+ describe "printer works" do
108
+ before { Sshez::PrintingManager.instance.clear! }
109
+
110
+ let(:printer) { Sshez::PrintingManager.instance }
111
+ let(:output) { printer.print("this is it"); printer.output }
112
+
113
+ it 'should be printed and kept' do
114
+ expect(output).to end_with "this is it\n"
115
+ end
116
+
117
+ after { Sshez::PrintingManager.instance.clear! }
118
+ end
119
+
73
120
  describe "version works" do
74
121
  let(:input) { "-v" }
75
122
  let(:output) { subject.process(input.split(" ")) }
76
123
 
77
124
  it 'matches gem version' do
78
- expect(output).to end_with "#{Sshez.version}"
125
+ expect(output).to end_with "#{Sshez.version}\n"
79
126
  end
80
127
  end
81
128
  end
data/sshez.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
19
  spec.test_files = Dir["spec/**/*"]
20
20
  spec.require_paths = ["lib"]
21
- spec.executables = ["sshez"]
21
+ spec.executables = ["sshez"]
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.10"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshez
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Immortal Friday
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-12-14 00:00:00.000000000 Z
12
+ date: 2016-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -64,12 +64,15 @@ executables:
64
64
  extensions: []
65
65
  extra_rdoc_files: []
66
66
  files:
67
+ - ".codeclimate.yml"
67
68
  - ".gitignore"
68
69
  - ".rspec"
70
+ - ".rubocop.yml"
69
71
  - ".travis.yml"
70
72
  - CHANGELOG.md
71
73
  - CODE_OF_CONDUCT.md
72
74
  - Gemfile
75
+ - Gemfile.lock
73
76
  - LICENSE
74
77
  - LICENSE.txt
75
78
  - README.md
@@ -78,9 +81,11 @@ files:
78
81
  - bin/setup
79
82
  - bin/sshez
80
83
  - lib/sshez.rb
81
- - lib/sshez/config_file.rb
84
+ - lib/sshez/command.rb
82
85
  - lib/sshez/exec.rb
83
- - lib/sshez/params.rb
86
+ - lib/sshez/parser.rb
87
+ - lib/sshez/printing_manager.rb
88
+ - lib/sshez/runner.rb
84
89
  - lib/sshez/version.rb
85
90
  - spec/spec_helper.rb
86
91
  - spec/sshez_spec.rb
@@ -106,10 +111,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
111
  version: '0'
107
112
  requirements: []
108
113
  rubyforge_project:
109
- rubygems_version: 2.4.5
114
+ rubygems_version: 2.5.1
110
115
  signing_key:
111
116
  specification_version: 4
112
117
  summary: Easy ssh config handling
113
118
  test_files:
114
- - spec/sshez_spec.rb
115
119
  - spec/spec_helper.rb
120
+ - spec/sshez_spec.rb
@@ -1,83 +0,0 @@
1
- module Sshez
2
- class ConfigFile
3
- FILE_PATH = File.expand_path('~') + "/.ssh/config"
4
-
5
- def self.append(name, user, host, options)
6
- output = "Adding\n"
7
- config_append = form(name, user, host, options)
8
- output += config_append
9
- unless options.test
10
- file = File.open(FILE_PATH, "a+")
11
- file.write(config_append)
12
- file.close
13
- puts "Successfully added `#{name}` as an alias for `#{user}@#{host}`"
14
- system "chmod 600 #{FILE_PATH}"
15
- end
16
- output += "to #{FILE_PATH}\n"
17
- output += "try ssh #{name} \n"
18
-
19
- output
20
- end
21
-
22
- def self.form(name, user, host, options)
23
- retuned = "\n"
24
- retuned += "Host #{name}\n"
25
- retuned += " HostName #{host}\n"
26
- retuned += " User #{user}\n"
27
-
28
- options.file_content.each_pair do |key, value|
29
- retuned += value
30
- end
31
- retuned
32
-
33
- end
34
-
35
- def self.remove(name)
36
- output = ""
37
- started_removing = false
38
- file = File.open(FILE_PATH, "r")
39
- new_file = File.open(FILE_PATH+"temp", "w")
40
- file.each do |line|
41
- if line.include?("Host #{name}")|| started_removing
42
- if started_removing && line.include?("Host ") && !line.include?(name)
43
- started_removing = false
44
- else
45
- output += line
46
- started_removing = true
47
- end
48
- else
49
- new_file.write(line)
50
- end
51
- end
52
- file.close
53
- new_file.close
54
- File.delete(FILE_PATH)
55
- File.rename(FILE_PATH + "temp", FILE_PATH)
56
- system "chmod 600 #{FILE_PATH}"
57
-
58
- if output.empty?
59
- return "could not find host (#{name})"
60
- else
61
- return output
62
- end
63
- end
64
-
65
- def self.list
66
- file = File.open(FILE_PATH, "r")
67
- servers = []
68
- file.each do |line|
69
- if line.include?("Host ")
70
- servers << line.sub('Host', '')
71
- end
72
- end
73
- file.close
74
- if servers.empty?
75
- puts "No aliases added"
76
- else
77
- puts "Listing aliases:"
78
- servers.each{|x| puts "\t- #{x}"}
79
- end
80
- return servers.join(',')
81
- end
82
- end
83
- end
data/lib/sshez/params.rb DELETED
@@ -1,64 +0,0 @@
1
- module Sshez
2
- class Params
3
- #
4
- # Return a structure describing the options.
5
- #
6
- def self.parse(args)
7
- # The options specified on the command line will be collected in *options*.
8
- # We set default values here.
9
- options = OpenStruct.new
10
- options.file_content = OpenStruct.new
11
-
12
- opt_parser = OptionParser.new do |opts|
13
- opts.banner = "Usage:\n\tsshez <alias> (role@host|-r) [options]\n\tsshez remove <alias>\n\tsshez list"
14
-
15
- opts.separator ""
16
- opts.separator "Specific options:"
17
-
18
- # Mandatory argument.
19
- opts.on("-p", "--port PORT",
20
- "Specify a port") do |port|
21
- options.port = port
22
- options.file_content.port_text = " Port #{options.port}\n"
23
- end
24
-
25
- # Optional argument; multi-line description.
26
- opts.on("-i", "--identity_file [key]",
27
- "Add identity") do |key_path|
28
- options.identity_file = key_path
29
- options.file_content.identity_file_text = " IdentityFile #{options.identity_file}\n"
30
- end
31
-
32
- opts.on("-r", "--remove", "Remove handle") do
33
- options.remove = true
34
- end
35
-
36
- opts.on("-t", "--test", "Writes nothing") do
37
- options.test = true
38
- end
39
-
40
- opts.separator ""
41
- opts.separator "Common options:"
42
-
43
- opts.on("-l", "--list", "List aliases") do
44
- Sshez::ConfigFile.list
45
- return
46
- end
47
-
48
- # Another typical switch to print the version.
49
- opts.on("-v", "--version", "Show version") do
50
- puts Sshez.version
51
- return
52
- end
53
-
54
- opts.on_tail("-h", "--help", "Show this message") do
55
- puts opts
56
- puts "\n"
57
- end
58
- end
59
-
60
- opt_parser.parse!(args)
61
- options
62
- end # parse()
63
- end # class Params
64
- end