Class: SSP::App::Bags

Inherits:
Thor
  • Object
show all
Defined in:
lib/ssp/application/bags.rb

Instance Method Summary collapse

Instance Method Details

#downloadObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ssp/application/bags.rb', line 17

def download
  bags = JSON.parse(knife_cmd("list"))
  bags.each do |bag|
    say "Downloading #{shell.set_color(bag, :bold)}: "
    bag_path = data_bag_root.join(bag)
    bag_path.mkpath
    items = JSON.parse(knife_cmd("show #{bag}"))
    items.each_with_index do |item, i|
      say "#{", " unless i == 0}#{item}", nil, false
      knife_cmd "show #{bag} #{item} > #{bag_path.join("#{item}.json")}"
    end
    puts
  end
end

#upload(selected_bag = nil, selected_item = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ssp/application/bags.rb', line 33

def upload(selected_bag = nil, selected_item = nil)
  bags = if selected_bag
    [data_bag_root.join(selected_bag)]
  else
    Pathname.glob(data_bag_root.join("*")).select {|p| p.directory?}
  end
  bags.each do |bag_path|
    bag = bag_path.basename
    say "Uploading #{shell.set_color(bag, :bold)}: "
    knife_cmd "create #{bag} > /dev/null"
    current_items = JSON.parse(knife_cmd("show #{bag}"))
    items = if selected_bag and selected_item
      [data_bag_root.join(selected_bag, "#{selected_item}.json")]
    else
      Pathname.glob(bag_path.join("*.json"))
    end
    items.each_with_index do |item, i|
      key = item.basename.to_s.split(".").first
      say "#{", " unless i == 0}#{key}", nil, false
      command = current_items.include?(key) ? "edit" : "create"
      knife_cmd "#{command} -e 'cat #{item} >' #{bag} #{key}"
    end
    puts
  end
end