lib/gitlab/cli_helpers.rb in gitlab-3.1.0 vs lib/gitlab/cli_helpers.rb in gitlab-3.2.0
- old
+ new
@@ -23,10 +23,18 @@
else
[]
end
end
+ # Confirms command is valid.
+ #
+ # @return [Boolean]
+ def valid_command?(cmd)
+ command = cmd.is_a?(Symbol) ? cmd : cmd.to_sym
+ Gitlab.actions.include?(command)
+ end
+
# Confirms command with a desctructive action.
#
# @return [String]
def confirm_command(cmd)
if cmd.start_with?('remove_') || cmd.start_with?('delete_')
@@ -75,10 +83,24 @@
t.add_row row
end
end
end
+ # Decides which table to use.
+ #
+ # @return [String]
+ def output_table(cmd, args, data)
+ case data
+ when Gitlab::ObjectifiedHash
+ puts single_record_table(data, cmd, args)
+ when Array
+ puts multiple_record_table(data, cmd, args)
+ else
+ puts data.inspect
+ end
+ end
+
# Table for a single record.
#
# @return [String]
def single_record_table(data, cmd, args)
hash = data.to_h
@@ -134,8 +156,20 @@
t.add_row values
t.add_separator unless arr.size - 1 == index
end
end
+ end
+
+ # Helper function to call Gitlab commands with args.
+ def gitlab_helper(cmd, args=[])
+ begin
+ data = args.any? ? Gitlab.send(cmd, *args) : Gitlab.send(cmd)
+ rescue => e
+ puts e.message
+ yield if block_given?
+ end
+
+ data
end
end
end