Module: CLIClassTool::Logger

Included in:
Common
Defined in:
lib/cli_class_tool/common.rb

Overview

Logger for CLIClassTool::Common

Instance Method Summary collapse

Instance Method Details

#confirm(opts, msg, confirm_opts = {}) ⇒ String

Prompt the user for confirmation

Parameters:

  • opts (Hash) —

    Options hash

  • msg (String) —

    Confirmation message

  • confirm_opts (Hash) (defaults to: {}) —

    Optional confirmation options

Options Hash (confirm_opts):

  • :ignored_default (Boolean) — default: false —

    Ignore default yes/no options

  • :ignore_default (Boolean) — default: false —

    Alias for :ignored_default

  • :allowed_reps (Array<String>) — default: ["y", "n"] —

    Allowed responses

  • :usage (String, Array<String>) —

    Custom usage string or list to display instead of allowed_reps.join("/")

Returns:

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cli_class_tool/common.rb', line 86

def confirm(opts, msg, confirm_opts = {})
    raise ArgumentError, "confirm options must be a Hash" unless confirm_opts.is_a?(Hash)

    conf_opts = confirm_opts.transform_keys(&:to_sym)

    ignored_default = conf_opts[:ignored_default]
    ignored_default = conf_opts[:ignore_default] if ignored_default.nil?
    ignored_default = false if ignored_default.nil?

    allowed_reps = (conf_opts[:allowed_reps] || [ "y", "n" ]).map(&:to_s)
    usage = conf_opts[:usage]
    usage_str = usage ? (usage.is_a?(Array) ? usage.join("/") : usage.to_s) : allowed_reps.join("/")

    rep = nil
    while allowed_reps.index(rep) == nil && rep != '' do
        puts "Do you wish to #{msg} ? (#{usage_str}): "
        case (ignored_default == true ? nil : opts&.[](:yn_default))
        when :no
            puts "Auto-replying no due to --no option"
            rep = 'n'
        when :yes
            puts "Auto-replying yes due to --yes option"
            rep = 'y'
        else
            rep = STDIN.gets.chomp()
        end
    end
    return rep
end

#log(lvl, str) ⇒ Object

Log a message with a specific level

Parameters:

  • lvl (Symbol) —

    Log level (:DEBUG, :INFO, :WARNING, :ERROR, etc.)

  • str (String) —

    Message to log



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cli_class_tool/common.rb', line 55

def log(lvl, str)
    case lvl
    when :DEBUG
        _log("DEBUG".magenta(), str) if ENV["DEBUG"].to_s() != ""
    when :DEBUG_CI
        _log("DEBUG_CI".magenta(), str) if ENV["DEBUG_CI"].to_s() != ""
    when :VERBOSE
        _log("INFO".blue(), str) if parent_module.verbose_log == true
    when :INFO
        _log("INFO".green(), str)
    when :PROGRESS
        _relog("INFO".green(), str)
    when :WARNING
        _log("WARNING".brown(), str)
    when :ERROR
        _log("ERROR".red(), str, $stderr)
    else
        _log(lvl, str)
    end
end