kknife 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -8
- data/lib/kknife/command.rb +34 -12
- data/lib/kknife/dbg.rb +10 -11
- data/lib/kknife/knifecmd.rb +12 -10
- data/lib/kknife/log.rb +12 -10
- data/lib/kknife/version.rb +1 -1
- data/spec/knifecmd_spec.rb +19 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -1,18 +1,25 @@
|
|
1
|
-
## kknife 0.1.
|
1
|
+
## kknife 0.1.2
|
2
2
|
|
3
|
-
|
3
|
+
#### Shortcuts for the chef `knife` command
|
4
4
|
|
5
|
-
|
6
|
-
`k
|
7
|
-
|
5
|
+
kknife lets you use the shortest substring to uniquely identify a knife sub command.
|
6
|
+
kknife also provides a `k` script to launch the lookups
|
7
|
+
|
8
|
+
`k n e somenode` = `knife node edit somenode`
|
9
|
+
|
10
|
+
`k c u somebook` = `knife cookbook upload somebook`
|
11
|
+
|
12
|
+
`k e u ff somefile` = `knife cookbook from_file somefile`
|
8
13
|
|
9
14
|
`k -l` lists all your commands
|
10
15
|
|
11
16
|
`k -d` might tell you what's going wrong.
|
12
17
|
|
13
|
-
Lookups
|
18
|
+
#### Lookups
|
19
|
+
|
20
|
+
Shortcuts and ambiguous commands are currently statically defined in `lib/kknife/lookup.rb`. These will be user configurable/overridable. Some form of json/yml blob in ~/.chef/ should do.
|
14
21
|
|
15
|
-
|
22
|
+
##### Shortcuts
|
16
23
|
```
|
17
24
|
'ff' => [ 'from', 'file' ],
|
18
25
|
'db' => [ 'data', 'bag' ],
|
@@ -22,7 +29,7 @@ Lookups for shortcuts and ambiguous commands are currently statically defined in
|
|
22
29
|
'ns' => [ 'node', 'show' ],
|
23
30
|
'rl' => [ 'run', 'list' ],
|
24
31
|
```
|
25
|
-
|
32
|
+
##### Ambiguities
|
26
33
|
```
|
27
34
|
'd' => ['download'],
|
28
35
|
'e' => ['environment'],
|
data/lib/kknife/command.rb
CHANGED
@@ -17,16 +17,34 @@
|
|
17
17
|
require 'kknife/dbg'
|
18
18
|
|
19
19
|
### Command
|
20
|
-
#
|
21
|
-
#
|
20
|
+
#
|
21
|
+
# This is used to store a tree of Commands by name
|
22
|
+
# Like a hash, but with a few extra features
|
23
|
+
#
|
24
|
+
# knife
|
25
|
+
# node
|
26
|
+
# list
|
27
|
+
# edit
|
28
|
+
# environment
|
29
|
+
# list
|
30
|
+
# edit
|
31
|
+
#
|
32
|
+
# Allows you to navigate up and down the tree.
|
22
33
|
|
23
34
|
class Command
|
24
35
|
|
25
36
|
include Dbg
|
26
37
|
|
38
|
+
# For the end of the tree
|
27
39
|
class NoMoreSubCommands < StandardError; end
|
40
|
+
|
41
|
+
# For the end of the user commands
|
28
42
|
class NoMoreSuppliedCommands < StandardError; end
|
43
|
+
|
44
|
+
# When more than one Command is looked up
|
29
45
|
class AmbiguousCommand < StandardError; end
|
46
|
+
|
47
|
+
# When no Command's can be found
|
30
48
|
class NotFoundCommand < StandardError; end
|
31
49
|
|
32
50
|
attr_accessor :cmd, :cmd_short, :sub_commands, :source, :level, :parent, :root, :controller
|
@@ -62,7 +80,7 @@ class Command
|
|
62
80
|
debug_logger.debug( 'Command init' )
|
63
81
|
end
|
64
82
|
|
65
|
-
# return the command
|
83
|
+
# return the command string
|
66
84
|
def to_s
|
67
85
|
@cmd
|
68
86
|
end
|
@@ -78,7 +96,7 @@ class Command
|
|
78
96
|
end
|
79
97
|
|
80
98
|
|
81
|
-
# Print current command recurse through sub commands
|
99
|
+
# Print current command as a tree, recurse through sub commands
|
82
100
|
def pp
|
83
101
|
printf "%s%s\n", ' ' * level, cmd
|
84
102
|
@sub_commands.each_key do |key|
|
@@ -86,6 +104,8 @@ class Command
|
|
86
104
|
end
|
87
105
|
end
|
88
106
|
|
107
|
+
# Print the current command on a single line if it's the last
|
108
|
+
# in the tree, otherwise move down.
|
89
109
|
def pp_single
|
90
110
|
printf "%s\n", command_lookup.reverse.join(' ') if has_no_sub_commands?
|
91
111
|
@sub_commands.each_key do |key|
|
@@ -93,6 +113,7 @@ class Command
|
|
93
113
|
end
|
94
114
|
end
|
95
115
|
|
116
|
+
# Recurse back down the Command tree and return the full command
|
96
117
|
def command_lookup
|
97
118
|
return [ @cmd ] + @parent.command_lookup unless @parent.nil?
|
98
119
|
[ @cmd ]
|
@@ -147,6 +168,7 @@ class Command
|
|
147
168
|
dbg 'list', sub_command_list
|
148
169
|
dbg 'rest', rest_commands
|
149
170
|
|
171
|
+
# If we have a command or can look one up, all is good
|
150
172
|
if has_sub_command? first_command or sub_command_list.length == 1
|
151
173
|
|
152
174
|
cmd = sub_command first_command
|
@@ -159,9 +181,9 @@ class Command
|
|
159
181
|
return cmd.process_lookup rest_commands
|
160
182
|
|
161
183
|
|
184
|
+
# If there was more than one match, check the ambiguity config
|
185
|
+
# or error
|
162
186
|
elsif sub_command_list.length > 1
|
163
|
-
# If there was more than one match, check the ambiguity config
|
164
|
-
# or error
|
165
187
|
|
166
188
|
if Lookup.ambiguity( first_command )
|
167
189
|
process_lookup Lookup.ambiguity( first_command ) + rest_commands
|
@@ -171,9 +193,9 @@ class Command
|
|
171
193
|
end
|
172
194
|
|
173
195
|
|
196
|
+
# If there's an underscore, split it out
|
174
197
|
elsif first_command.index(/_/)
|
175
|
-
|
176
|
-
|
198
|
+
|
177
199
|
cmdsplit = first_command.split( /_/ )
|
178
200
|
|
179
201
|
# notify the controller of the command changes
|
@@ -187,17 +209,17 @@ class Command
|
|
187
209
|
process_lookup [ first_command ] + rest_commands
|
188
210
|
|
189
211
|
|
212
|
+
# Otherwise the command wasn't found,
|
213
|
+
# Look up shortcuts before giving up
|
190
214
|
else
|
191
|
-
# Otherwise the command wasn't found,
|
192
|
-
# Look up shortcuts before giving up
|
193
215
|
|
194
216
|
shortcut = Lookup.shortcut( first_command )
|
217
|
+
|
195
218
|
if shortcut
|
196
219
|
@controller.found_shortcut shortcut
|
197
220
|
process_lookup shortcut + rest_commands
|
198
|
-
|
199
221
|
else
|
200
|
-
raise NotFoundCommand, "not found [#{first_command}
|
222
|
+
raise NotFoundCommand, "sub command not found: [#{first_command}*]"
|
201
223
|
end
|
202
224
|
|
203
225
|
end
|
data/lib/kknife/dbg.rb
CHANGED
@@ -17,17 +17,16 @@
|
|
17
17
|
require 'logger'
|
18
18
|
|
19
19
|
### Dbg
|
20
|
-
#
|
21
|
-
#
|
22
|
-
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
|
30
|
-
# ```
|
20
|
+
#
|
21
|
+
# A quick debug logger so lasses can call `dbg`
|
22
|
+
#
|
23
|
+
# class Yours
|
24
|
+
# include Dbg
|
25
|
+
# def method
|
26
|
+
# dbg 'some', values, logged
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
|
31
30
|
|
32
31
|
module Dbg
|
33
32
|
|
data/lib/kknife/knifecmd.rb
CHANGED
@@ -20,9 +20,11 @@ require 'chef/knife'
|
|
20
20
|
require 'chef/application/knife'
|
21
21
|
|
22
22
|
### Knifecmd
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
23
|
+
#
|
24
|
+
# Used as the base for the knife commands.
|
25
|
+
#
|
26
|
+
# Stores information about the current lookup
|
27
|
+
# and the Command tree
|
26
28
|
|
27
29
|
class Knifecmd
|
28
30
|
|
@@ -128,10 +130,10 @@ class Knifecmd
|
|
128
130
|
@cmd_root.process_lookup commands
|
129
131
|
|
130
132
|
rescue Command::AmbiguousCommand => e
|
131
|
-
|
133
|
+
abort "error looking up [#{commands.to_s}]. #{e}"
|
132
134
|
|
133
135
|
rescue Command::NotFoundCommand => e
|
134
|
-
|
136
|
+
abort "error looking up [#{commands.to_s}]. #{e}"
|
135
137
|
|
136
138
|
rescue Command::NoMoreSubCommands
|
137
139
|
dbg "end of knife lookups", @cmd_found.join(','), @cmd_left.join(',')
|
@@ -145,7 +147,7 @@ class Knifecmd
|
|
145
147
|
end
|
146
148
|
|
147
149
|
|
148
|
-
#
|
150
|
+
# If the Command lookup needs to split a command
|
149
151
|
# remove the command from what's left and split it
|
150
152
|
# into the componenets
|
151
153
|
def cmd_split( split_command_array )
|
@@ -167,7 +169,7 @@ class Knifecmd
|
|
167
169
|
end
|
168
170
|
|
169
171
|
|
170
|
-
#
|
172
|
+
# If the Command lookup hits a shortcut, adjust local
|
171
173
|
# variables to match
|
172
174
|
def found_shortcut( shortcut )
|
173
175
|
dbg 'found_shortcut b4 ', shortcut.join(','), '|', @cmd_left.join(',')
|
@@ -177,20 +179,20 @@ class Knifecmd
|
|
177
179
|
end
|
178
180
|
|
179
181
|
|
180
|
-
#
|
182
|
+
# Reset the command found instance variables
|
181
183
|
def reset_found( commands = [] )
|
182
184
|
@cmd_found = []
|
183
185
|
@cmd_left = commands.dup
|
184
186
|
end
|
185
187
|
|
186
188
|
|
187
|
-
#
|
189
|
+
# Return the found command as a space seperated string
|
188
190
|
def found_string
|
189
191
|
([ @root ] + @cmd_found ).join(' ')
|
190
192
|
end
|
191
193
|
|
192
194
|
|
193
|
-
#
|
195
|
+
# Resolve a list of commands into real commands
|
194
196
|
def resolve( commands )
|
195
197
|
lookup commands
|
196
198
|
@cmd_found + @cmd_left
|
data/lib/kknife/log.rb
CHANGED
@@ -17,38 +17,40 @@
|
|
17
17
|
require 'logger'
|
18
18
|
|
19
19
|
### Log - a global logger instance for classes
|
20
|
-
|
20
|
+
#
|
21
21
|
# Allows you to call a single logger instance easily from your classes
|
22
|
-
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
# end
|
30
|
-
# ```
|
22
|
+
#
|
23
|
+
# class Yours
|
24
|
+
# include Log
|
25
|
+
# def method
|
26
|
+
# log.info 'something'
|
27
|
+
# end
|
28
|
+
# end
|
31
29
|
|
32
30
|
module Log
|
33
31
|
|
34
32
|
DefaultIO = STDOUT
|
35
33
|
|
34
|
+
# returns the singleton
|
36
35
|
def log
|
37
36
|
Logging.log
|
38
37
|
end
|
39
38
|
|
39
|
+
# replace the logger with a new target
|
40
40
|
def self.replace( io )
|
41
41
|
l = Logger.new io
|
42
42
|
l.level = @log.level
|
43
43
|
@log = l
|
44
44
|
end
|
45
45
|
|
46
|
+
# create a new logger
|
46
47
|
def self.create
|
47
48
|
l = Logger.new DefaultIO
|
48
49
|
l.level = Logger::INFO
|
49
50
|
l
|
50
51
|
end
|
51
52
|
|
53
|
+
# return the singleton or create it
|
52
54
|
def self.log
|
53
55
|
@log ||= create
|
54
56
|
end
|
data/lib/kknife/version.rb
CHANGED
data/spec/knifecmd_spec.rb
CHANGED
@@ -107,5 +107,24 @@ describe Knifecmd do
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
+
|
111
|
+
it "abort on ambiguous command" do
|
112
|
+
expect{
|
113
|
+
begin
|
114
|
+
@k.resolve( ['s'] )
|
115
|
+
rescue SystemExit
|
116
|
+
end
|
117
|
+
}.to stderr( "error looking up [[\"s\"]]. ambiguous [s] [search,show,ssh,status]\n" )
|
118
|
+
end
|
119
|
+
|
120
|
+
it "abort on ambiguous command" do
|
121
|
+
expect{
|
122
|
+
begin
|
123
|
+
@k.resolve( ['z','y'] )
|
124
|
+
rescue SystemExit
|
125
|
+
end
|
126
|
+
}.to stderr( "error looking up [[\"z\", \"y\"]]. sub command not found: [z*]\n" )
|
127
|
+
end
|
128
|
+
|
110
129
|
end
|
111
130
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kknife
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-12-
|
12
|
+
date: 2013-12-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: chef
|