Skip to content
Permalink
Browse files

Support json export

  • Loading branch information...
Genki Sugawara
Genki Sugawara committed May 10, 2015
1 parent 6e42981 commit 7b9b99dd37bb4317a6d7c4a576ec57bbe9ccbf49
Showing with 29 additions and 14 deletions.
  1. +1 −0 .gitignore
  2. +22 −11 bin/miam
  3. +6 −3 lib/miam/client.rb
@@ -16,3 +16,4 @@ test.rb
IAMfile
*.iam
account.csv
*.json
@@ -48,6 +48,7 @@ ARGV.options do |opt|
opt.on('-o', '--output FILE') {|v| output_file = v }
opt.on('' , '--split') { split = true }
opt.on('' , '--split-more') { split = :more }
opt.on('', '--format=FORMAT', [:ruby, :json]) {|v| options[:format] = v }
opt.on('' , '--export-concurrency N', Integer) {|v| options[:export_concurrency] = v }
opt.on('' , '--target REGEXP') {|v| options[:target] = Regexp.new(v) }
opt.on('' , '--no-color') { options[:color] = false }
@@ -102,11 +103,12 @@ begin
output_file = DEFAULT_FILENAME if output_file == '-'
requires = []

client.export(:split_more => (split == :more)) do |args|
client.export(:split_more => (split == :more), :convert => (options[:format] == :ruby)) do |args|
type, dsl = args.values_at(:type, :dsl)
next if dsl.strip.empty?
next if dsl.empty?

type = type.to_s
dsl = JSON.pretty_generate(dsl) if options[:format] == :json
dir = File.dirname(output_file)

if split == :more
@@ -122,32 +124,41 @@ begin
requires << iam_filename
end

if options[:format] == :json
iam_file << '.json'
end

logger.info(" write `#{iam_file}`")

open(iam_file, 'wb') do |f|
f.puts MAGIC_COMMENT
f.puts MAGIC_COMMENT if options[:format] == :ruby
f.puts dsl
end
end

logger.info(" write `#{output_file}`")
if options[:format] == :ruby
logger.info(" write `#{output_file}`")

open(output_file, 'wb') do |f|
f.puts MAGIC_COMMENT
open(output_file, 'wb') do |f|
f.puts MAGIC_COMMENT

requires.each do |iam_file|
f.puts "require '#{iam_file}'"
requires.each do |iam_file|
f.puts "require '#{iam_file}'"
end
end
end
else
exported = client.export(:convert => (options[:format] == :ruby))
exported = JSON.pretty_generate(exported) if options[:format] == :json

if output_file == '-'
logger.info('# Export IAM')
puts client.export.strip
puts exported
else
logger.info("Export IAM to `#{output_file}`")
open(output_file, 'wb') do |f|
f.puts MAGIC_COMMENT
f.puts client.export.strip
f.puts MAGIC_COMMENT if options[:format] == :ruby
f.puts exported
end
end
end
@@ -10,6 +10,7 @@ def initialize(options = {})
end

def export(export_options = {})
export_options = {convert: true}.merge(export_options)
exported, group_users, instance_profile_roles = Miam::Exporter.export(@iam, @options)

if block_given?
@@ -21,15 +22,17 @@ def export(export_options = {})
more_splitted = splitted.dup
more_splitted[type] = {}
more_splitted[type][name] = attrs
yield(:type => type, :name => name, :dsl => Miam::DSL.convert(more_splitted, @options).strip)
dsl = export_options[:convert] ? Miam::DSL.convert(more_splitted, @options).strip : more_splitted
yield(:type => type, :name => name, :dsl => dsl)
end
else
splitted[type] = exported[type]
yield(:type => type, :dsl => Miam::DSL.convert(splitted, @options).strip)
dsl = export_options[:convert] ? Miam::DSL.convert(splitted, @options).strip : splitted
yield(:type => type, :dsl => dsl)
end
end
else
Miam::DSL.convert(exported, @options)
export_options[:convert] ? Miam::DSL.convert(exported, @options).strip : exported
end
end

0 comments on commit 7b9b99d

Please sign in to comment.
You can’t perform that action at this time.