Skip to content

Commit

Permalink
feat: Misc fixes (#175)
Browse files Browse the repository at this point in the history
* feat: cache results of keys_to_keep lookups

* fix: remove redefinition of :width to clear warnings

* fix: linting issues, better error messages

* fix: lint issues in unit tests

* fix: bump version to go with API 4.0 support

* feat: change whitelist to allowlist
  • Loading branch information
drstrangelooker authored Apr 13, 2023
1 parent 68d6df3 commit 20b334b
Show file tree
Hide file tree
Showing 47 changed files with 160 additions and 154 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
gazer (0.2.60)
gazer (0.3.0)
faraday (~> 1.10.3)
looker-sdk (~> 0.1.1)
net-http-persistent (~> 4.0, >= 4.0.1)
Expand Down
24 changes: 15 additions & 9 deletions lib/gzr/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def query(query_id)
data = @sdk.query(query_id)
rescue LookerSDK::Error => e
say_error "Error querying query(#{query_id})"
say_error e.message
say_error e
raise
end
data
Expand All @@ -72,7 +72,7 @@ def create_query(query)
end
rescue LookerSDK::Error => e
say_error "Error creating query(#{JSON.pretty_generate(query)})"
say_error e.message
say_error e
raise
end
data
Expand All @@ -86,7 +86,7 @@ def merge_query(merge_result_id)
say_error "The api endpoint merge_query(#{merge_result_id}) is not implemented on this Looker instance"
rescue LookerSDK::Error => e
say_error "Error querying merge_query(#{merge_result_id})"
say_error e.message
say_error e
raise
end
data
Expand All @@ -100,7 +100,7 @@ def create_merge_query(merge_query)
raise
rescue LookerSDK::Error => e
say_error "Error creating merge_query(#{JSON.pretty_generate(merge_query)})"
say_error e.message
say_error e
raise
end
data
Expand All @@ -111,7 +111,7 @@ def run_inline_query(query)
data = @sdk.run_inline_query("json",query)
rescue LookerSDK::Error => e
say_error "Error running inline_query(#{JSON.pretty_generate(query)})"
say_error e.message
say_error e
raise
end
data
Expand All @@ -127,7 +127,7 @@ def all_color_collections()
say_warning "The current user can't query all color collections"
rescue LookerSDK::Error => e
say_error "Error querying all_color_collections()"
say_error e.message
say_error e
raise
end
data
Expand All @@ -145,7 +145,7 @@ def default_color_collection()
say_warning "The current user can't query the default color collection"
rescue LookerSDK::Error => e
say_error "Error querying default_color_collection()"
say_error e.message
say_error e
raise
end
data
Expand All @@ -161,7 +161,7 @@ def color_collection(collection_id)
say_warning "The color_collection(#{collection_id}) is not found"
rescue LookerSDK::Error => e
say_error "Error querying color_collection(#{collection_id})"
say_error e.message
say_error e
raise
end
data
Expand Down Expand Up @@ -266,6 +266,7 @@ def update_color_palette!(o,default_colors,force_default=false)
end
end


##
# This method accepts the name of an sdk operation, then finds the parameter for that
# operation in the data structures from the swagger.json file. The parameter is a
Expand All @@ -282,6 +283,11 @@ def update_color_palette!(o,default_colors,force_default=false)
# end

def keys_to_keep(operation)
@keys_to_keep_cache ||= {}
@keys_to_keep_cache[operation] ||= keys_to_keep_internal(operation)
end

def keys_to_keep_internal(operation)
o = @sdk.operations[operation] || @sdk.operations[operation.to_sym]
begin
say_error "Operation #{operation} not found"
Expand Down Expand Up @@ -386,7 +392,7 @@ def field_expression(name)
##
# This method will accept two arrays, a and b, and create a third array
# like [ [a[0],b[0]], [a[1],b[1]], [a[2],b[2]], ...].
# If either array is longer than the other, additional pairs
# If either array is longer than the other, additional pairs
# will be generated with the shorter array padded out with nil values.
#
# Any additional args will be added to each inner array.
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def import(file)
desc: 'can a non-admin user view the value'
method_option :'can-edit', type: :boolean, default: true,
desc: 'can a user change the value themself'
method_option :'domain-whitelist', type: :string,
desc: 'what domains can receive the value of a hidden attribute.'
method_option :'domain-allowlist', type: :string,
desc: 'what domains can receive the value of a hidden attribute.'
def create(attr_name, attr_label=nil)
if options[:help]
invoke :help, ['create']
Expand Down
6 changes: 3 additions & 3 deletions lib/gzr/commands/attribute/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def execute(input: $stdin, output: $stdout)
source[:'value_is_hidden'] = true if @options[:'is-hidden']
source[:'user_can_view'] = true if @options[:'can-view']
source[:'user_can_edit'] = true if @options[:'can-edit']
source[:'hidden_value_domain_whitelist'] = @options[:'domain-whitelist'] if @options[:'is-hidden'] && @options[:'domain-whitelist']
source[:'hidden_value_domain_allowlist'] = @options[:'domain-allowlist'] if @options[:'is-hidden'] && @options[:'domain-allowlist']

attr = upsert_user_attribute(source, @options[:force], output: $stdout)
output.puts "Imported attribute #{attr.name} #{attr.id}" unless @options[:plain]
output.puts attr.id if @options[:plain]
output.puts "Imported attribute #{attr.name} #{attr.id}" unless @options[:plain]
output.puts attr.id if @options[:plain]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/attribute/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def execute(input: $stdin, output: $stdout)
with_session do
read_file(@file) do |source|
attr = upsert_user_attribute(source, @options[:force], output: $stdout)
output.puts "Imported attribute #{attr.name} #{attr.id}" unless @options[:plain]
output.puts attr.id if @options[:plain]
output.puts "Imported attribute #{attr.name} #{attr.id}" unless @options[:plain]
output.puts attr.id if @options[:plain]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/attribute/rm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ def execute(input: $stdin, output: $stdout)

delete_user_attribute(attr.id)

output.puts "Deleted attribute #{attr.name} #{attr.id}" unless @options[:plain]
output.puts attr.id if @options[:plain]
output.puts "Deleted attribute #{attr.name} #{attr.id}" unless @options[:plain]
output.puts attr.id if @options[:plain]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/connection/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def execute(input: $stdin, output: $stdout)
if @options[:csv] then
output.puts render_csv(table)
else
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, width: 1024, width: @options[:width] || TTY::Screen.width)
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, width: @options[:width] || TTY::Screen.width)
end
end if table
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/dashboard/mv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def execute(input: $stdin, output: $stdout)
if matching_title.empty? || matching_title.first[:deleted]
matching_title = false
end

if matching_title
raise Gzr::CLI::Error, "Dashboard #{dash[:title]} already exists in folder #{@target_folder_id}\nUse --force if you want to overwrite it" unless @options[:force]
say_ok "Deleting existing dashboard #{matching_title.first[:id]} #{matching_title.first[:title]} in folder #{@target_folder_id}", output: output
update_dashboard(matching_title.first[:id],{:deleted=>true})
end
update_dashboard(dash[:id],{:folder_id=>@target_folder_id})
output.puts "Moved dashboard #{dash[:id]} to folder #{@target_folder_id}" unless @options[:plain]
output.puts "Moved dashboard #{dash[:id]} to folder #{@target_folder_id}" unless @options[:plain]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/folder/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def execute(input: $stdin, output: $stdout)
folder = nil
with_session do
folder = create_folder(@name, @parent_folder)
output.puts "Created folder #{folder.id}" unless @options[:plain]
output.puts folder.id if @options[:plain]
output.puts "Created folder #{folder.id}" unless @options[:plain]
output.puts folder.id if @options[:plain]
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/gzr/commands/folder/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ def execute(input: $stdin, output: $stdout)

tree_data = Hash.new

folder_ids.each do |folder_id|
folder_ids.each do |folder_id|
s = query_folder(folder_id, "id,name,parent_id,looks(id,title),dashboards(id,title)")
folder_name = s.name
folder_name = "nil (#{s.id})" unless folder_name
folder_name = "nil (#{s.id})" unless folder_name
folder_name = "\"#{folder_name}\"" if ((folder_name != folder_name.strip) || (folder_name.length == 0))
folder_name += " (#{folder_id})" unless folder_ids.length == 1
tree_data[folder_name] =
Expand All @@ -88,7 +88,7 @@ def recurse_folders(folder_id)
tree_branch = Hash.new
data.each do |s|
folder_name = s.name
folder_name = "nil (#{s.id})" unless folder_name
folder_name = "nil (#{s.id})" unless folder_name
folder_name = "\"#{folder_name}\"" if ((folder_name != folder_name.strip) || (folder_name.length == 0))
tree_branch[folder_name] =
[ recurse_folders(s.id) ] +
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/look/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def execute(input: $stdin, output: $stdout)

look = upsert_look(@me.id,create_fetch_query(data[:query]).id,@dest_folder_id,data,output: output)
upsert_plans_for_look(look.id,@me.id,data[:scheduled_plans]) if data[:scheduled_plans]
output.puts "Imported look #{look.id}" unless @options[:plain]
output.puts look.id if @options[:plain]
output.puts "Imported look #{look.id}" unless @options[:plain]
output.puts look.id if @options[:plain]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/look/mv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def execute(input: $stdin, output: $stdout)
update_look(matching_title.first[:id],{:deleted=>true})
end
update_look(look[:id],{:folder_id=>@target_folder_id})
output.puts "Moved look #{look[:id]} to folder #{@target_folder_id}" unless @options[:plain]
output.puts "Moved look #{look[:id]} to folder #{@target_folder_id}" unless @options[:plain]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/permissions/ls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def execute(input: $stdin, output: $stdout)
if @options[:csv] then
output.puts render_csv(table)
else
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, width: 1024, width: @options[:width] || TTY::Screen.width)
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, width: @options[:width] || TTY::Screen.width)
end
end if table
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/plan/disable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}") if @options[:debug]
with_session do
plan = update_scheduled_plan(@plan_id, { :enabled=>false })
output.puts "Disabled plan #{plan.id}" unless @options[:plain]
output.puts plan.id if @options[:plain]
output.puts "Disabled plan #{plan.id}" unless @options[:plain]
output.puts plan.id if @options[:plain]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/plan/enable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def execute(input: $stdin, output: $stdout)
say_warning("options: #{@options.inspect}") if @options[:debug]
with_session do
plan = update_scheduled_plan(@plan_id, { :enabled=>true })
output.puts "Enabled plan #{plan.id}" unless @options[:plain]
output.puts plan.id if @options[:plain]
output.puts "Enabled plan #{plan.id}" unless @options[:plain]
output.puts plan.id if @options[:plain]
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/plan/failures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def execute(input: $stdin, output: $stdout)
if @options[:csv] then
output.puts render_csv(table)
else
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, width: @options[:width] || TTY::Screen.width, width: @options[:width] || TTY::Screen.width)
output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: alignments, width: @options[:width] || TTY::Screen.width)
end
end if table
end
Expand Down
6 changes: 3 additions & 3 deletions lib/gzr/commands/plan/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def execute(input: $stdin, output: $stdout)
with_session do

@me ||= query_me("id")


read_file(@plan_file) do |data|
plan = nil
Expand All @@ -58,8 +58,8 @@ def execute(input: $stdin, output: $stdout)
else
raise Gzr::CLI::Error, "Invalid type '#{obj_type}', valid types are look and dashboard"
end
output.puts "Imported plan #{plan.id}" unless @options[:plain]
output.puts plan.id if @options[:plain]
output.puts "Imported plan #{plan.id}" unless @options[:plain]
output.puts plan.id if @options[:plain]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gzr/commands/plan/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ def execute(input: $stdin, output: $stdout)
# The api call scheduled_plan_run_once is an odd duck. It accepts
# the output of any of the calls to retrieve a scheduled plan
# even though many of the attributes passed are marked read-only.
# Furthermore, if there is a "secret" - like the password for
# Furthermore, if there is a "secret" - like the password for
# sftp or s3 - it will match the plan body up with the plan
# as known in the server and if they are identical apart from
# the secret, the api will effectively include to secret in order
# execute the plan.
plan.delete(:id)
run_scheduled_plan(plan)
output.puts "Executed plan #{@plan_id}" unless @options[:plain]
output.puts "Executed plan #{@plan_id}" unless @options[:plain]
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/gzr/commands/query/runquery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ def execute(input: $stdin, output: $stdout)
@sdk.run_query(query_id,@options[:format]) { |data,progress| (f || output).write(data) }
rescue LookerSDK::Error => e
say_error "Error in run_query(#{query_id},#{@options[:format]})})"
say_error e.message
say_error e
raise
end
else
begin
@sdk.run_inline_query(@options[:format],query_hash) { |data,progress| (f || output).write(data) }
@sdk.run_inline_query(@options[:format],query_hash) { |data,progress| (f || output).write(data) }
rescue LookerSDK::Error => e
say_error "Error in run_inline_query(#{@options[:format]},#{JSON.pretty_generate(query_hash)})"
say_error e.message
say_error e
raise
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/role/group_add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(role_id,groups,options)

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]

with_session do
groups = query_role_groups(@role_id, 'id').collect { |g| g.id }
groups += @groups
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/role/group_rm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(role_id,groups,options)

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]

with_session do
groups = query_role_groups(@role_id, 'id').collect { |g| g.id }
groups -= @groups
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/role/user_add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(role_id,users,options)

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]

with_session do
users = query_role_users(@role_id, 'id').collect { |u| u.id }
users += @users
Expand Down
2 changes: 1 addition & 1 deletion lib/gzr/commands/role/user_rm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(role_id,users,options)

def execute(input: $stdin, output: $stdout)
say_warning(@options) if @options[:debug]

with_session do
users = query_role_users(@role_id, 'id').collect { |u| u.id }
users -= @users
Expand Down
Loading

0 comments on commit 20b334b

Please sign in to comment.