class YleTf::CLI

Constants

HELP_ARGS
TF_OPTIONS

YleTf option arguments

VERSION_ARGS

Attributes

tf_command[R]
tf_command_args[R]
tf_env[R]
tf_options[R]

Public Class Methods

new(argv) click to toggle source
# File lib/yle_tf/cli.rb, line 15
def initialize(argv)
  @tf_options = {}
  @tf_command_args = []
  split_args(argv)
end

Public Instance Methods

debug=(value) click to toggle source
# File lib/yle_tf/cli.rb, line 81
def debug=(value)
  if value
    ENV['TF_DEBUG'] = '1'
  else
    ENV.delete('TF_DEBUG')
  end
end
debug?() click to toggle source
# File lib/yle_tf/cli.rb, line 89
def debug?
  ENV.key?('TF_DEBUG')
end
execute() click to toggle source
# File lib/yle_tf/cli.rb, line 21
def execute
  tf = YleTf.new(tf_options, tf_env, tf_command, tf_command_args)
  tf.run
rescue YleTf::Error => e
  raise e if debug?

  Logger.fatal e
  exit 1
end
key(arg) click to toggle source

Returns `Symbol` for the arg, e.g. `“–foo-bar”` -> `:foo_bar`

# File lib/yle_tf/cli.rb, line 77
def key(arg)
  arg.sub(/\A--?/, '').tr('-', '_').to_sym
end
split_args(argv) click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/BlockLength, Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/yle_tf/cli.rb, line 33
def split_args(argv)
  argv.each do |arg|
    if @tf_env && @tf_command
      if TF_OPTIONS.include?(arg)
        @tf_options[key(arg)] = true
      else
        @tf_command_args << arg
      end
    elsif HELP_ARGS.include?(arg)
      @tf_command = 'help'
      @tf_env = '_'
      break
    elsif VERSION_ARGS.include?(arg)
      @tf_command = 'version'
      @tf_env = '_'
      break
    elsif arg.start_with?('-')
      if TF_OPTIONS.include?(arg)
        @tf_options[key(arg)] = true
      else
        warn "Unknown option '#{arg}'"
        @tf_command = 'help'
        @tf_env = 'error'
        break
      end
    elsif !@tf_env
      @tf_env = arg
    else
      @tf_command = arg
    end
  end

  if !@tf_command || !@tf_env
    @tf_command = 'help'
    @tf_env = 'error'
  end

  self.debug = true if @tf_options[:debug]
  YleTf::Logger.color = false if @tf_options[:no_color]
end