knife-solo_data_bag 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'tempfile'
|
1
2
|
require 'chef/knife'
|
2
3
|
|
3
4
|
class Chef
|
@@ -35,12 +36,48 @@ class Chef
|
|
35
36
|
|
36
37
|
private
|
37
38
|
def edit_content
|
38
|
-
|
39
|
+
content = Chef::JSONCompat.to_json_pretty(existing_bag_item_content)
|
40
|
+
updated_content = nil
|
41
|
+
loop do
|
42
|
+
unparsed = edit_text content
|
43
|
+
begin
|
44
|
+
updated_content = Chef::JSONCompat.from_json(unparsed)
|
45
|
+
break
|
46
|
+
rescue Yajl::ParseError => e
|
47
|
+
loop do
|
48
|
+
ui.stdout.puts e.to_s
|
49
|
+
question = "Do you want to keep editing (Y/N)? If you choose 'N', all changes will be lost"
|
50
|
+
continue = ui.ask question
|
51
|
+
case continue
|
52
|
+
when 'Y', 'y'
|
53
|
+
content = unparsed
|
54
|
+
break
|
55
|
+
when 'N', 'n'
|
56
|
+
raise e
|
57
|
+
else
|
58
|
+
ui.stdout.puts 'Please answer Y or N'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
39
63
|
item = Chef::DataBagItem.from_hash format_editted_content(updated_content)
|
40
64
|
item.data_bag bag_name
|
41
65
|
persist_bag_item item
|
42
66
|
end
|
43
67
|
|
68
|
+
def edit_text(text)
|
69
|
+
tf = Tempfile.new(['knife-edit', '.json'])
|
70
|
+
tf.sync = true
|
71
|
+
tf.puts text
|
72
|
+
tf.close
|
73
|
+
|
74
|
+
raise "Please set EDITOR environment variable" unless Kernel.system("#{config[:editor]} #{tf.path}")
|
75
|
+
|
76
|
+
output = File.read(tf.path)
|
77
|
+
tf.unlink
|
78
|
+
output
|
79
|
+
end
|
80
|
+
|
44
81
|
def existing_bag_item_content
|
45
82
|
content = Chef::DataBagItem.load(bag_name, item_name).raw_data
|
46
83
|
|
@@ -27,6 +27,18 @@ describe Chef::Knife::SoloDataBagEdit do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
context 'with valid arguments' do
|
30
|
+
let(:tf) do
|
31
|
+
double(
|
32
|
+
'Tempfile',
|
33
|
+
:sync= => nil,
|
34
|
+
:puts => nil,
|
35
|
+
:close => nil,
|
36
|
+
:path => tempfile_name,
|
37
|
+
:unlink => true
|
38
|
+
)
|
39
|
+
end
|
40
|
+
let(:tempfile_name) { '/tmp/foo' }
|
41
|
+
|
30
42
|
before do
|
31
43
|
@bags_path = '/var/chef/data_bags'
|
32
44
|
@bag_path = "#{@bags_path}/bag_1"
|
@@ -44,7 +56,10 @@ describe Chef::Knife::SoloDataBagEdit do
|
|
44
56
|
|
45
57
|
Chef::DataBagItem.should_receive(:load).with('bag_1', 'foo').
|
46
58
|
and_return(@bag_item_foo)
|
47
|
-
|
59
|
+
Tempfile.stub(:new).and_return(tf)
|
60
|
+
Kernel.stub(:system => true)
|
61
|
+
File.stub(:read).and_call_original
|
62
|
+
File.stub(:read).with(tempfile_name).and_return(@updated_data.to_json)
|
48
63
|
Chef::Config[:data_bag_path] = @bags_path
|
49
64
|
end
|
50
65
|
|
@@ -138,6 +153,55 @@ describe Chef::Knife::SoloDataBagEdit do
|
|
138
153
|
end
|
139
154
|
end
|
140
155
|
|
156
|
+
context 'with malformed JSON' do
|
157
|
+
let(:user_wants_to_reedit) { 'Y' }
|
158
|
+
|
159
|
+
before do
|
160
|
+
@knife.config[:editor] = 'vimacs'
|
161
|
+
@pass = 0
|
162
|
+
@asked_to_continue = 0
|
163
|
+
File.stub(:read).with(tempfile_name) do
|
164
|
+
@pass += 1
|
165
|
+
case @pass
|
166
|
+
when 1
|
167
|
+
'{,badjson}'
|
168
|
+
else
|
169
|
+
@updated_data.to_json
|
170
|
+
end
|
171
|
+
end
|
172
|
+
@knife.ui.stub(:ask) do
|
173
|
+
case @pass
|
174
|
+
when 1
|
175
|
+
@asked_to_continue += 1
|
176
|
+
user_wants_to_reedit
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'asks whether to re-edit' do
|
182
|
+
@knife.run
|
183
|
+
@asked_to_continue.should == 1
|
184
|
+
end
|
185
|
+
|
186
|
+
context 'when the user wants to re-edit' do
|
187
|
+
it 'the editor is re-opened' do
|
188
|
+
Kernel.should_receive(:system).with("vimacs #{tempfile_name}").
|
189
|
+
exactly(2).times.and_return(true)
|
190
|
+
@knife.run
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
context "the user doesn't want to re-edit" do
|
195
|
+
let(:user_wants_to_reedit) { 'N' }
|
196
|
+
|
197
|
+
it 'an error is thrown' do
|
198
|
+
lambda {
|
199
|
+
@knife.run
|
200
|
+
}.should raise_error(Yajl::ParseError)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
141
205
|
end
|
142
206
|
|
143
207
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knife-solo_data_bag
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
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: 2014-
|
12
|
+
date: 2014-04-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|