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 +4 -4
- data/Rakefile +8 -1
- data/bin/etcd-completion +8 -50
- data/etcd-completion.gemspec +3 -3
- data/lib/etcd-completion.rb +55 -0
- data/spec/etcd-completion_spec.rb +73 -0
- data/travis.yml +5 -0
- metadata +35 -5
- data/lib/etcd/completion.rb +0 -7
- data/lib/etcd/completion/version.rb +0 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef72e192e9d4f8bcddac95655bf6388c24fdd1ad
|
4
|
+
data.tar.gz: 6872c7b5aa8bebd8eb4c70b93d50a2921be8b9fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab9a0a0b5b93acc1c7e43790b5e51a589aab6995cf9d149da7fb215f0b13a22bcff9c000215127ddb8393f937228b563cd44bde118fe78fe544803136ad1d2e2
|
7
|
+
data.tar.gz: 3700fab2ed814c1c9c4f58ab8ad6ce7d8bbf151054825631a5ddb8035d510ed149d12249a8433836163ad5cb003ccfc4e9c99607b2f1a042bab8160ef65fa2a8
|
data/Rakefile
CHANGED
data/bin/etcd-completion
CHANGED
@@ -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
|
-
|
29
|
-
|
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
|
-
|
40
|
-
|
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
|
-
|
57
|
-
|
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(' ')
|
data/etcd-completion.gemspec
CHANGED
@@ -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 =
|
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
|
data/travis.yml
ADDED
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.
|
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-
|
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
|
71
|
-
-
|
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
|
data/lib/etcd/completion.rb
DELETED