Skip to content

Commit

Permalink
feat: add --trim to gzr role cat
Browse files Browse the repository at this point in the history
  • Loading branch information
drstrangelooker committed May 16, 2023
1 parent d47e645 commit d9e8899
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/gzr/commands/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ def rm(role_id)
desc: 'Display usage information'
method_option :dir, type: :string,
desc: 'Directory to get output file'
method_option :trim, type: :boolean,
desc: 'Trim output to minimal set of fields for later import'
def cat(role_id)
if options[:help]
invoke :help, ['cat']
Expand Down
7 changes: 4 additions & 3 deletions lib/gzr/commands/role/cat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ def initialize(role_id,options)
def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}") if @options[:debug]
with_session do
data = query_role(@role_id)
write_file(@options[:dir] ? "Role_#{data.id}_#{data.name}.json" : nil, @options[:dir], nil, output) do |f|
f.puts JSON.pretty_generate(data.to_attrs)
data = query_role(@role_id)&.to_attrs
data = trim_role(data) if @options[:trim]
write_file(@options[:dir] ? "Role_#{data[:id]}_#{data[:name]}.json" : nil, @options[:dir], nil, output) do |f|
f.puts JSON.pretty_generate(data)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/gzr/modules/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def query_role(role_id)
end
data
end
def trim_role(data)
trimmed = data.select do |k,v|
(keys_to_keep('create_role') + [:id]).include? k
end
trimmed[:permission_set] = data[:permission_set].select do |k,v|
(keys_to_keep('create_permission_set') + [:id,:built_in,:all_access]).include? k
end
trimmed[:model_set] = data[:model_set].select do |k,v|
(keys_to_keep('create_model_set') + [:id,:built_in,:all_access]).include? k
end
trimmed
end
def delete_role(role_id)
data = nil
begin
Expand Down
1 change: 1 addition & 0 deletions spec/integration/role/cat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Options:
-h, [--help], [--no-help] # Display usage information
[--dir=DIR] # Directory to get output file
[--trim], [--no-trim] # Trim output to minimal set of fields for later import
Output the JSON representation of a role to screen/file
OUT
Expand Down
1 change: 1 addition & 0 deletions spec/integration/role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
expected_output = <<-OUT
Commands:
gzr role cat ROLE_ID # Output the JSON representation of a role to screen/file
gzr role create ROLE_NAME PERMISSION_SET_ID MODEL_SET_ID # Create new role with the given permission and model sets
gzr role group_add ROLE_ID GROUP_ID GROUP_ID GROUP_ID ... # Add indicated groups to role
gzr role group_ls ROLE_ID # List the groups assigned to a role
gzr role group_rm ROLE_ID GROUP_ID GROUP_ID GROUP_ID ... # Remove indicated groups from role
Expand Down

0 comments on commit d9e8899

Please sign in to comment.