Skip to content

Commit

Permalink
Support json export
Browse files Browse the repository at this point in the history
  • Loading branch information
Genki Sugawara committed May 10, 2015
1 parent 6e42981 commit 7b9b99d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@ test.rb
IAMfile
*.iam
account.csv
*.json
33 changes: 22 additions & 11 deletions bin/miam
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/miam/client.rb
Expand Up @@ -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?
Expand All @@ -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

Expand Down

0 comments on commit 7b9b99d

Please sign in to comment.