etcd-completion 0.0.2 → 0.0.4

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: 838c65b812cf27f31626684e15e71e7459c6d896
4
- data.tar.gz: cee0dab9d704ae70f10f7ca1b0939381cd011579
3
+ metadata.gz: ef72e192e9d4f8bcddac95655bf6388c24fdd1ad
4
+ data.tar.gz: 6872c7b5aa8bebd8eb4c70b93d50a2921be8b9fd
5
5
  SHA512:
6
- metadata.gz: a63fa945007f5c6b161801993ae49cddd31c0c4200d1df1d44945725a6bb1813a81d655a7880947dd63721e6b9f40ff6e8700034deb440601ebb3627d8ea2e0b
7
- data.tar.gz: 38860b79acf047643e36aafd5b00b49cae64731c7dcbef7e0c409b8006c206cfd139477e5b031c4dc28cae6ec24996f21d4e4558606f93428acc58fa0825b832
6
+ metadata.gz: ab9a0a0b5b93acc1c7e43790b5e51a589aab6995cf9d149da7fb215f0b13a22bcff9c000215127ddb8393f937228b563cd44bde118fe78fe544803136ad1d2e2
7
+ data.tar.gz: 3700fab2ed814c1c9c4f58ab8ad6ce7d8bbf151054825631a5ddb8035d510ed149d12249a8433836163ad5cb003ccfc4e9c99607b2f1a042bab8160ef65fa2a8
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
- require "bundler/gem_tasks"
1
+ # Optional require
2
+ def optional(path)
3
+ require path
4
+ rescue LoadError
5
+ end
6
+
7
+ optional 'bundler/gem_tasks'
8
+ optional 'rspec/core/rake_task'
@@ -1,65 +1,23 @@
1
1
  #!/usr/bin/env ruby
2
- require 'etcd'
3
-
4
- def dir_ops
5
- %w'mkdir rmdir ls setdir updatedir'
6
- end
7
-
8
- def node_ops
9
- %w'mk rm get set update watch'
10
- end
2
+ require 'etcd-completion'
11
3
 
12
4
  def filter(prefix, key)
13
5
  key.start_with?(prefix)
14
6
  end
15
7
 
16
8
  def completions(op, prefix)
17
- prefix = '' if prefix.nil?
18
- if node_ops.include? op
19
- type = 'node'
20
- elsif dir_ops.include? op
21
- type = 'dir'
22
- else
23
- fail "Usage: etcdctl.rb <operation> <prefix>"
24
- end
25
-
26
- client = Etcd.client
9
+ prefix = '/' if prefix.nil?
27
10
 
28
- if (prefix == '/')
29
- node_name = '/'
30
- else
31
- index = prefix.rindex('/')
32
- if index.nil?
33
- node_name = '/'
34
- else
35
- node_name = prefix[0, prefix.rindex('/')+1]
36
- end
37
- end
11
+ completion = Etcd::Completion.new
12
+ matches = completion.matches(prefix)
38
13
 
39
- node = client.get(node_name)
40
- if node.directory?
41
- children = node.children
42
- case type
43
- when 'dir'
44
- result = children.select{|c|c.directory? && filter(prefix, c.key)}.map do |c|
45
- c.key + '/'
46
- end
47
- when 'node'
48
- result = children.select{|c|filter(prefix, c.key)}.map do |c|
49
- c.directory? ? (c.key + '/') : (c.key)
50
- end
51
- end
52
- else
53
- result = [filter(prefix, node.key)]
14
+ if Etcd::Operation.new(op).directory_only?
15
+ matches = matches.select{|m| m.directory?}
54
16
  end
55
17
 
56
- if (result.size == 1)
57
- new_prefix = result[0]
58
- if new_prefix.end_with?('/')
59
- result << completions(op, new_prefix)
60
- end
18
+ matches.map do |m|
19
+ (m.directory? && m.key != '/') ? m.key + '/' : m.key
61
20
  end
62
- result
63
21
  end
64
22
 
65
23
  puts completions(ARGV[0], ARGV[1]).join(' ')
@@ -1,11 +1,10 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'etcd/completion/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = "etcd-completion"
8
- spec.version = Etcd::Completion::VERSION
7
+ spec.version = "0.0.4"
9
8
  spec.authors = ["Michael Shea"]
10
9
  spec.email = ["mike.shea@gmail.com"]
11
10
  spec.summary = %q{Utility for etcd autocompletion}
@@ -20,6 +19,7 @@ Gem::Specification.new do |spec|
20
19
 
21
20
  spec.add_development_dependency "bundler", "~> 1.5"
22
21
  spec.add_development_dependency "rake"
23
-
22
+ spec.add_development_dependency "rspec", "~> 3.1"
23
+ spec.add_development_dependency 'simplecov', '~> 0.9'
24
24
  spec.add_dependency 'etcd', "~> 0.2"
25
25
  end
@@ -0,0 +1,55 @@
1
+ require 'etcd'
2
+ module Etcd
3
+
4
+ class Operation
5
+ attr_reader :name
6
+ def initialize(name)
7
+ @name = name
8
+ end
9
+
10
+ def directory_only?
11
+ %w'mkdir rmdir ls setdir updatedir'.include? name
12
+ end
13
+ end
14
+
15
+ class Completion
16
+ attr_reader :client
17
+
18
+ def initialize(opts={})
19
+ @client = opts[:client] || Etcd.client(opts)
20
+ end
21
+
22
+ def node_name(prefix)
23
+ if (prefix == '/')
24
+ '/'
25
+ else
26
+ index = prefix.rindex('/')
27
+ if index.nil?
28
+ '/'
29
+ else
30
+ prefix[0..index]
31
+ end
32
+ end
33
+ end
34
+
35
+ def filter(nodes, prefix)
36
+ nodes.select do |n|
37
+ n.key.start_with?(prefix)
38
+ end
39
+ end
40
+
41
+ def matches(prefix)
42
+ node = client.get(node_name(prefix)).node
43
+ nodes = [node] + node.children
44
+ nodes = filter(nodes, prefix)
45
+
46
+ if nodes.size == 1
47
+ if nodes[0].directory?
48
+ nodes += client.get(nodes[0].key).node.children
49
+ end
50
+ end
51
+
52
+ nodes
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,73 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'rspec'
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'etcd-completion'
6
+
7
+ module Etcd
8
+ %w'mkdir rmdir ls setdir updatedir'.each do |op|
9
+ describe "#{op} #directory_only?" do
10
+ let(:operation){ Operation.new(op) }
11
+ it 'should be true' do
12
+ expect(operation.directory_only?).to eq(true)
13
+ end
14
+ end
15
+ end
16
+
17
+ describe Completion do
18
+ describe '#matches' do
19
+ let (:completion){ Completion.new(:client=>client) }
20
+ let (:client) { double('client') }
21
+
22
+ it 'should return matches for the prefix' do
23
+ allow(client).to receive(:get).with('/'){root}
24
+ expect(completion.matches('/prefix').map(&:key)).to eq %w'/prefix1 /prefix2 /prefix3'
25
+ end
26
+
27
+ it 'should include children if only one immediate dir match' do
28
+ allow(client).to receive(:get).with('/'){root}
29
+ allow(client).to receive(:get).with('/prefix1'){subdir}
30
+ expect(completion.matches('/prefix1').map(&:key)).to eq %w'/prefix1 /prefix1/key'
31
+ end
32
+ end
33
+
34
+ def subdir
35
+ double('subdir-result').tap do |r|
36
+ allow(r).to receive(:node) {
37
+ dir('/prefix1', [ key('/prefix1/key') ])
38
+ }
39
+ end
40
+ end
41
+
42
+ def root
43
+ double('result').tap do |r|
44
+ allow(r).to receive(:node) do
45
+ dir(
46
+ '/',
47
+ [
48
+ dir('/prefix1'),
49
+ dir('/prefix2'),
50
+ key('/prefix3')
51
+ ])
52
+ end
53
+ end
54
+ end
55
+
56
+ def dir(name, children=[])
57
+ node(name, true).tap do |n|
58
+ allow(n).to receive(:children){children}
59
+ end
60
+ end
61
+
62
+ def key(name)
63
+ node(name, false)
64
+ end
65
+
66
+ def node(name, directory)
67
+ double(name).tap do |n|
68
+ allow(n).to receive(:key){name}
69
+ allow(n).to receive(:directory?){directory}
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1.0
5
+ - 1.9.3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etcd-completion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Shea
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-26 00:00:00.000000000 Z
11
+ date: 2014-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: etcd
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -67,8 +95,9 @@ files:
67
95
  - Rakefile
68
96
  - bin/etcd-completion
69
97
  - etcd-completion.gemspec
70
- - lib/etcd/completion.rb
71
- - lib/etcd/completion/version.rb
98
+ - lib/etcd-completion.rb
99
+ - spec/etcd-completion_spec.rb
100
+ - travis.yml
72
101
  homepage: ''
73
102
  licenses:
74
103
  - MIT
@@ -93,4 +122,5 @@ rubygems_version: 2.2.2
93
122
  signing_key:
94
123
  specification_version: 4
95
124
  summary: Utility for etcd autocompletion
96
- test_files: []
125
+ test_files:
126
+ - spec/etcd-completion_spec.rb
@@ -1,7 +0,0 @@
1
- require "etcd/completion/version"
2
-
3
- module Etcd
4
- module Completion
5
- # Your code goes here...
6
- end
7
- end
@@ -1,5 +0,0 @@
1
- module Etcd
2
- module Completion
3
- VERSION = "0.0.2"
4
- end
5
- end