Skip to content

Commit

Permalink
feat: add --verbose option
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgomesxyz committed Oct 19, 2023
1 parent c5f1c43 commit 889343d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
12 changes: 12 additions & 0 deletions lib/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ def self.wait_option(title = "", required: false)
}
end

def self.verbose_option(required: false)
{
name: :verbose,
params: {
aliases: ["-d"],
desc: "Shows detailed logs",
type: :boolean,
required: required
}
}
end

def self.all_options
methods.grep(/_option$/).map { |method| send(method.to_s) }
end
Expand Down
2 changes: 2 additions & 0 deletions lib/core/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def initialize(args, options)

load_app_config
load_apps

Shell.verbose_mode(options[:verbose])
end

def [](key)
Expand Down
30 changes: 20 additions & 10 deletions lib/core/controlplane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def profile_exists?(profile)

def profile_create(profile, token)
cmd = "cpln profile create #{profile} --token #{token}"
cmd += " > /dev/null" if Shell.tmp_stderr
cmd += " > /dev/null" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

def profile_delete(profile)
cmd = "cpln profile delete #{profile}"
cmd += " > /dev/null" if Shell.tmp_stderr
cmd += " > /dev/null" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

Expand Down Expand Up @@ -58,25 +58,25 @@ def image_delete(image)

def image_login(org_name = config.org)
cmd = "cpln image docker-login --org #{org_name}"
cmd += " > /dev/null 2>&1" if Shell.tmp_stderr
cmd += " > /dev/null 2>&1" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

def image_pull(image)
cmd = "docker pull #{image}"
cmd += " > /dev/null" if Shell.tmp_stderr
cmd += " > /dev/null" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

def image_tag(old_tag, new_tag)
cmd = "docker tag #{old_tag} #{new_tag}"
cmd += " > /dev/null" if Shell.tmp_stderr
cmd += " > /dev/null" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

def image_push(image)
cmd = "docker push #{image}"
cmd += " > /dev/null" if Shell.tmp_stderr
cmd += " > /dev/null" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

Expand Down Expand Up @@ -145,7 +145,11 @@ def workload_get_replicas(workload, location:)
end

def workload_get_replicas_safely(workload, location:)
cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml 2> /dev/null"
cmd = "cpln workload get-replicas #{workload} #{gvc_org} --location #{location} -o yaml"
cmd += " 2> /dev/null" unless Shell.verbose

Shell.debug("CMD", cmd)

result = `#{cmd}`
$CHILD_STATUS.success? ? YAML.safe_load(result) : nil
end
Expand Down Expand Up @@ -177,7 +181,7 @@ def workload_deployments_ready?(workload, expected_status:)
def workload_set_image_ref(workload, container:, image:)
cmd = "cpln workload update #{workload} #{gvc_org}"
cmd += " --set spec.containers.#{container}.image=/org/#{config.org}/image/#{image}"
cmd += " > /dev/null" if Shell.tmp_stderr
cmd += " > /dev/null" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

Expand Down Expand Up @@ -205,7 +209,7 @@ def set_workload_suspend(workload, value)

def workload_force_redeployment(workload)
cmd = "cpln workload force-redeployment #{workload} #{gvc_org}"
cmd += " > /dev/null" if Shell.tmp_stderr
cmd += " > /dev/null" if Shell.tmp_stderr && !Shell.verbose
perform!(cmd)
end

Expand Down Expand Up @@ -280,7 +284,7 @@ def apply(data) # rubocop:disable Metrics/MethodLength
f.rewind
cmd = "cpln apply #{gvc_org} --file #{f.path} > /dev/null"
if Shell.tmp_stderr
cmd += " 2> #{Shell.tmp_stderr.path}"
cmd += " 2> #{Shell.tmp_stderr.path}" unless Shell.verbose
perform(cmd)
else
perform!(cmd)
Expand All @@ -291,14 +295,20 @@ def apply(data) # rubocop:disable Metrics/MethodLength
private

def perform(cmd)
Shell.debug("CMD", cmd)

system(cmd)
end

def perform!(cmd)
Shell.debug("CMD", cmd)

system(cmd) || exit(false)
end

def perform_yaml(cmd)
Shell.debug("CMD", cmd)

result = `#{cmd}`
$CHILD_STATUS.success? ? YAML.safe_load(result) : exit(false)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/core/controlplane_api_direct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def call(url, method:, host: :api, body: nil) # rubocop:disable Metrics/MethodLe
request["Authorization"] = api_token
request.body = body.to_json if body

Shell.debug(method.upcase, "#{uri} #{body&.to_json}")

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(request) }

case response
Expand Down
10 changes: 9 additions & 1 deletion lib/core/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class Shell
class << self
attr_reader :tmp_stderr
attr_reader :tmp_stderr, :verbose
end

def self.shell
Expand Down Expand Up @@ -50,4 +50,12 @@ def self.warn_deprecated(message)
def self.abort(message)
Kernel.abort(color("ERROR: #{message}", :red))
end

def self.verbose_mode(verbose)
@verbose = verbose
end

def self.debug(prefix, message)
stderr.puts("\n[#{color(prefix, :red)}] #{message}\n\n") if verbose
end
end
2 changes: 1 addition & 1 deletion lib/cpl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def self.all_base_commands
usage = command_class::USAGE.empty? ? name : command_class::USAGE
requires_args = command_class::REQUIRES_ARGS
default_args = command_class::DEFAULT_ARGS
command_options = command_class::OPTIONS
command_options = command_class::OPTIONS + [::Command::Base.verbose_option]
description = command_class::DESCRIPTION
long_description = command_class::LONG_DESCRIPTION
examples = command_class::EXAMPLES
Expand Down

0 comments on commit 889343d

Please sign in to comment.