fantassh 0.1.0 → 0.2.0

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: 1a01ad29fdf51e3f06eb32b575bc144458babff7
4
- data.tar.gz: df7eb7fa10ad36d6d31d3ae7a3e60a271b4285d8
3
+ metadata.gz: 3313e2ac142946ca069a6aa41411ffba516b69db
4
+ data.tar.gz: 6b7ca5d40c94b48c95c0d3c4a588a99397fb41af
5
5
  SHA512:
6
- metadata.gz: 93607b03fd97dadbcafabcc40ade5a090de21cd8372be11f77f6f01100c7d1d8d71112722ab1c3457dfac825a41902ce5c7ef673cb2197fb86f627540b9438d3
7
- data.tar.gz: d055fa3ca230d3dc2e36ebc4def23626821cbfd3dfc8bb4d037505535ede1893b7265e3e1e18897331d4632ec3f7c4ea9667e2d5eb5250f6c021b6f87753acfb
6
+ metadata.gz: e1a8a47857b20d771656245ddc4db65a7aa1a1b66ce626c1d5359494892b99b3fc6537695db9579efdddac156a5c7ddf8076dc575c5309335caf6b08aeaa0fbe
7
+ data.tar.gz: a404aacc74a19a34c2ca713e515353543f03af7cc503716f2cbb985fd3f82d2a8bcf1c0fcc9427e0e710c8e3977d98280fbeb0f513cb6a7a1b2bc01cb894cdbf
@@ -2,4 +2,4 @@
2
2
 
3
3
  require 'fantassh'
4
4
 
5
- Fantassh::Application.new.run
5
+ Fantassh::Application.run
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
+ spec.add_dependency 'slop'
22
+
21
23
  spec.add_development_dependency 'bundler', '~> 1.3'
22
24
  spec.add_development_dependency 'rake'
23
25
  spec.add_development_dependency 'rspec'
@@ -1,17 +1,59 @@
1
+ require 'slop'
1
2
  require_relative 'bash_history'
2
3
  require_relative 'entries'
3
4
 
4
5
  module Fantassh
5
6
  class Application
6
- def initialize
7
- @entries = Entries.new
8
- @bash_history = BashHistory.new
9
- end
7
+ class << self
8
+ def run(argv = ARGV)
9
+ Slop.parse(argv, help: true) do
10
+ on '-v', '--version', 'Print the program version.' do
11
+ puts "#{File.basename($0)} v#{Fantassh::VERSION}"
12
+ exit
13
+ end
14
+
15
+ # default, runs when called without arguments
16
+ run do
17
+ Fantassh::Application.list
18
+ end
19
+
20
+ command :exclude do
21
+ banner "Usage: #{File.basename($0)} exclude <ssh command>"
22
+
23
+ run do |opts, args|
24
+ if args.empty?
25
+ puts help
26
+ exit
27
+ end
28
+
29
+ Fantassh::Application.exclude(args.join(' '))
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def list
36
+ entries.add(bash_history.entries)
37
+
38
+ selected_entry = `echo '#{entries.all.join("\n")}' | selecta`
39
+ # in case selecta receives ctrl+c we don't proceed
40
+ unless selected_entry.empty?
41
+ # indent by whitespace so it doesn't show up in the history
42
+ exec " ssh #{selected_entry}"
43
+ end
44
+ end
45
+
46
+ def exclude(entry)
47
+ entries.exclude([entry])
48
+ end
49
+
50
+ def entries
51
+ Entries.new
52
+ end
10
53
 
11
- def run
12
- @entries.add(@bash_history.entries)
13
- # indent by whitespace so it doesn't show up in the history
14
- exec " ssh $(cat #{@entries.entries_file} | selecta)"
54
+ def bash_history
55
+ BashHistory.new
56
+ end
15
57
  end
16
58
  end
17
59
  end
@@ -2,7 +2,7 @@ module Fantassh
2
2
  class BashHistory
3
3
  def entries
4
4
  File.readlines(File.join(Dir.home, '.bash_history')).
5
- grep(/^\s*ssh/).
5
+ grep(/^\s*ssh\s/).
6
6
  map(&:strip).
7
7
  uniq.
8
8
  map { |x| x.gsub(/^ssh\s+/, '') }
@@ -1,39 +1,30 @@
1
- require 'fileutils'
1
+ require_relative 'entries_file'
2
+ require_relative 'excluded_entries_file'
2
3
 
3
4
  module Fantassh
4
5
  class Entries
5
- attr_reader :entries_file
6
+ def initialize(entries_file: EntriesFile.new, excluded_entries_file: ExcludedEntriesFile.new)
7
+ @entries_file = entries_file
8
+ @excluded_entries_file = excluded_entries_file
6
9
 
7
- def initialize(config_dir: nil)
8
- @config_dir = config_dir || File.join(Dir.home, '.fantassh')
9
- @entries_file = File.join(@config_dir, 'entries')
10
- @excluded_entries_file = File.join(@config_dir, 'excluded_entries')
11
10
  init_file_structure
12
11
  end
13
12
 
14
- def all
15
- File.readlines(@entries_file).map(&:strip)
13
+ def add(entries)
14
+ @entries_file.add(entries)
16
15
  end
17
16
 
18
- def excluded
19
- File.readlines(@excluded_entries_file).map(&:strip)
17
+ def all
18
+ @entries_file.all - @excluded_entries_file.all
20
19
  end
21
20
 
22
- def add(new_entries)
23
- new_entries = new_entries.map(&:strip).delete_if { |x| x.empty? }
24
- entries = (all + new_entries).uniq - excluded
25
-
26
- File.open(@entries_file, 'w') do |f|
27
- f.puts(entries)
28
- end
21
+ def exclude(entries)
22
+ @excluded_entries_file.add(entries)
29
23
  end
30
24
 
31
25
  def init_file_structure
32
- unless Dir.exist?(@config_dir)
33
- FileUtils.mkdir(@config_dir)
34
- end
35
- FileUtils.touch(@entries_file)
36
- FileUtils.touch(@excluded_entries_file)
26
+ @entries_file.init_file_structure
27
+ @excluded_entries_file.init_file_structure
37
28
  end
38
29
  end
39
30
  end
@@ -0,0 +1,33 @@
1
+ require 'fileutils'
2
+
3
+ module Fantassh
4
+ class EntriesFile
5
+ def initialize(config_dir: nil)
6
+ @config_dir = config_dir || File.join(Dir.home, '.fantassh')
7
+ end
8
+
9
+ def all
10
+ File.readlines(file).map(&:strip)
11
+ end
12
+
13
+ def add(new_entries)
14
+ new_entries = new_entries.map(&:strip).delete_if { |x| x.empty? }
15
+ entries = (all + new_entries).uniq
16
+
17
+ File.open(file, 'w') do |f|
18
+ f.puts(entries)
19
+ end
20
+ end
21
+
22
+ def file
23
+ File.join(@config_dir, 'entries')
24
+ end
25
+
26
+ def init_file_structure
27
+ unless Dir.exist?(@config_dir)
28
+ FileUtils.mkdir(@config_dir)
29
+ end
30
+ FileUtils.touch(file)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,10 @@
1
+ require 'fileutils'
2
+ require_relative 'entries_file'
3
+
4
+ module Fantassh
5
+ class ExcludedEntriesFile < EntriesFile
6
+ def file
7
+ File.join(@config_dir, 'excluded_entries')
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module Fantassh
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ require 'tmpdir'
3
+
4
+ module Fantassh
5
+ describe Application do
6
+ context 'exclude' do
7
+ it 'adds an entry to the excluded entries' do
8
+ Dir.mktmpdir do |dir|
9
+ entries_file = Entries.new(
10
+ entries_file: EntriesFile.new(config_dir: dir),
11
+ excluded_entries_file: ExcludedEntriesFile.new(config_dir: dir))
12
+ Application.stub(entries: entries_file)
13
+
14
+ Application.run(['exclude', 'ssh bogus'])
15
+
16
+ File.readlines(File.join(dir, 'excluded_entries')).should == ["ssh bogus\n"]
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -6,12 +6,19 @@ module Fantassh
6
6
  subject { BashHistory.new }
7
7
 
8
8
  context "#entries" do
9
- it "returns entries that start with 'ssh'" do
9
+ it "returns entries that start with 'ssh '" do
10
10
  File.stub(readlines: ["ssh host1.com\n", "host2.com\n", "\n"])
11
11
 
12
12
  subject.entries.should == ['host1.com']
13
13
  end
14
14
 
15
+ it "doesn't return entries that start with 'ssh*'" do
16
+ File.stub(readlines: ["ssh host1.com\n",
17
+ "ssh-keygen -t rsa -C 'a@a.com'\n", "\n"])
18
+
19
+ subject.entries.should == ['host1.com']
20
+ end
21
+
15
22
  it "ignores duplicates" do
16
23
  File.stub(readlines: ["ssh host1.com\n", "ssh host1.com\n"])
17
24
 
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require 'fantassh/entries_file'
3
+
4
+ module Fantassh
5
+ describe EntriesFile do
6
+ subject { EntriesFile.new(config_dir: '/dir') }
7
+
8
+ context "#all" do
9
+ it "returns all entries" do
10
+ File.should_receive(:readlines).with('/dir/entries').
11
+ and_return(["host1.com\n", "user@host2.com\n"])
12
+
13
+ subject.all.should == ['host1.com', 'user@host2.com']
14
+ end
15
+ end
16
+
17
+ context "#add" do
18
+ before do
19
+ file = double('file')
20
+ File.should_receive(:open).with('/dir/entries', 'w').and_yield(file)
21
+ file.should_receive(:puts).with(["host1.com"])
22
+ end
23
+
24
+ it "adds a new entry" do
25
+ subject.stub(all: [])
26
+ subject.stub(excluded: [])
27
+
28
+ subject.add(["host1.com"])
29
+ end
30
+
31
+ it "doesn't add a duplicate" do
32
+ subject.stub(all: ["host1.com"])
33
+ subject.stub(excluded: [])
34
+
35
+ subject.add(["host1.com"])
36
+ end
37
+
38
+ it "doesn't add an empty entry" do
39
+ subject.stub(all: ["host1.com"])
40
+ subject.stub(excluded: [])
41
+
42
+ subject.add([''])
43
+ end
44
+
45
+ it "removes leading and trailing whitespace" do
46
+ subject.stub(all: [])
47
+ subject.stub(excluded: [])
48
+
49
+ subject.add([" host1.com\n\n"])
50
+ end
51
+ end
52
+ end
53
+ end
@@ -3,66 +3,35 @@ require 'fantassh/entries'
3
3
 
4
4
  module Fantassh
5
5
  describe Entries do
6
- before { Entries.any_instance.stub(:init_file_structure) }
7
- subject { Entries.new(config_dir: '/dir') }
6
+ let(:entries_file) { double(:entries_file) }
8
7
 
9
- context "#excluded" do
10
- it "returns the hidden entries" do
11
- File.should_receive(:readlines).and_return(["host1.com\n"])
8
+ let(:excluded_entries_file) { double(:excluded_entries_file) }
12
9
 
13
- subject.excluded.should == ['host1.com']
14
- end
15
- end
16
-
17
- context "#all" do
18
- it "returns all entries" do
19
- File.should_receive(:readlines).with('/dir/entries').
20
- and_return(["host1.com\n", "user@host2.com\n"])
10
+ context "#initialize" do
11
+ it "calls init_file_structure on both entry files" do
12
+ entries_file.should_receive(:init_file_structure)
13
+ excluded_entries_file.should_receive(:init_file_structure)
21
14
 
22
- subject.all.should == ['host1.com', 'user@host2.com']
15
+ Entries.new(entries_file: entries_file, excluded_entries_file: excluded_entries_file)
23
16
  end
24
17
  end
25
18
 
26
- context "#add" do
27
- before do
28
- file = double('file')
29
- File.should_receive(:open).with('/dir/entries', 'w').and_yield(file)
30
- file.should_receive(:puts).with(["host1.com"])
31
- end
32
-
33
- it "adds a new entry" do
34
- subject.stub(all: [])
35
- subject.stub(excluded: [])
36
-
37
- subject.add(["host1.com"])
38
- end
39
-
40
- it "doesn't add a duplicate" do
41
- subject.stub(all: ["host1.com"])
42
- subject.stub(excluded: [])
43
-
44
- subject.add(["host1.com"])
45
- end
46
-
47
- it "doesn't add a hidden entry" do
48
- subject.stub(all: ["host1.com"])
49
- subject.stub(excluded: ["user@host2.com"])
50
-
51
- subject.add(["user@host2.com"])
52
- end
19
+ context "#all" do
20
+ before { Entries.any_instance.stub(:init_file_structure) }
53
21
 
54
- it "doesn't add an empty entry" do
55
- subject.stub(all: ["host1.com"])
56
- subject.stub(excluded: [])
22
+ it "returns an entry" do
23
+ entries_file.should_receive(:all).and_return(["host1.com"])
57
24
 
58
- subject.add([''])
25
+ Entries.new(entries_file: entries_file).all.should ==
26
+ ["host1.com"]
59
27
  end
60
28
 
61
- it "removes leading and trailing whitespace" do
62
- subject.stub(all: [])
63
- subject.stub(excluded: [])
29
+ it "doesn't return a hidden entry" do
30
+ entries_file.should_receive(:all).and_return(["host1.com"])
31
+ excluded_entries_file.should_receive(:all).and_return(["user@host2.com"])
64
32
 
65
- subject.add([" host1.com\n\n"])
33
+ Entries.new(entries_file: entries_file, excluded_entries_file: excluded_entries_file).
34
+ all.should == ["host1.com"]
66
35
  end
67
36
  end
68
37
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+ require 'fantassh/excluded_entries_file'
3
+
4
+ module Fantassh
5
+ describe ExcludedEntriesFile do
6
+ subject { ExcludedEntriesFile.new(config_dir: '/dir') }
7
+
8
+ context "#all" do
9
+ it "returns all entries" do
10
+ File.should_receive(:readlines).with('/dir/excluded_entries').
11
+ and_return(["user@host2.com\n"])
12
+
13
+ subject.all.should == ['user@host2.com']
14
+ end
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fantassh
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
  - Alexis Reigel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-19 00:00:00.000000000 Z
11
+ date: 2014-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: slop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -72,9 +86,14 @@ files:
72
86
  - lib/fantassh/application.rb
73
87
  - lib/fantassh/bash_history.rb
74
88
  - lib/fantassh/entries.rb
89
+ - lib/fantassh/entries_file.rb
90
+ - lib/fantassh/excluded_entries_file.rb
75
91
  - lib/fantassh/version.rb
92
+ - spec/lib/application_spec.rb
76
93
  - spec/lib/bash_history_spec.rb
94
+ - spec/lib/entries_file_spec.rb
77
95
  - spec/lib/entries_spec.rb
96
+ - spec/lib/excluded_entries_file_spec.rb
78
97
  - spec/spec_helper.rb
79
98
  homepage: https://github.com/koffeinfrei/fantassh
80
99
  licenses:
@@ -101,6 +120,9 @@ signing_key:
101
120
  specification_version: 4
102
121
  summary: Provides a selectable history list for quickly accessing your past ssh connections.
103
122
  test_files:
123
+ - spec/lib/application_spec.rb
104
124
  - spec/lib/bash_history_spec.rb
125
+ - spec/lib/entries_file_spec.rb
105
126
  - spec/lib/entries_spec.rb
127
+ - spec/lib/excluded_entries_file_spec.rb
106
128
  - spec/spec_helper.rb