Class: Thor

Inherits:
Object
  • Object
show all
Defined in:
lib/idcf/cli/gem_ext/thor/init_util.rb

Overview

Thor extention

Class Method Summary collapse

Class Method Details

.command_help_string(command) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 58

def command_help_string(command)
  result = [command]
  map.each do |k, v|
    result << k if v.to_s == command.to_s
  end
  result.join('/')
end

.command_regist(command, require_path, arg) ⇒ Object

command regist

Parameters:

  • command (String)
  • require_path (String)
  • o (Hash)

    options



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 46

def command_regist(command, require_path, arg)
  require require_path

  class_const = require_path.classify.constantize
  class_const.init(arg) if arg.include?(command) || map.values.include?(command.to_sym)

  register class_const,
           command,
           "#{command_help_string(command)} [OPTION]",
           class_const.description
end

.descriptionObject

command description

Returns:

  • String

Raises:



20
21
22
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 20

def description
  raise Idcf::Cli::Error::CliError, 'Required Override'
end

.error_exit(e) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 88

def error_exit(e)
  STDERR.puts e.message
  Idcf::Cli::Lib::Util::CliLogger.error(e.message)
  Idcf::Cli::Lib::Util::CliLogger.cleaning(options)
  STDERR.puts "log path: #{Idcf::Cli::Lib::Util::CliLogger.current_path}"
  exit 1
end

.exit_on_failure?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 8

def exit_on_failure?
  true
end

.initObject

initialize settings



13
14
15
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 13

def init
  # When initialization is needed, override.
end

.module_pathObject

module path

Returns:

  • String



69
70
71
72
73
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 69

def module_path
  class_names = to_s.underscore.split('/')
  class_names.pop
  class_names.join('/')
end

.sub_command_regist(under_path, parent_dir_path = '', arg = {}) ⇒ Object

register sub command

Parameters:

  • dir (String)

    underlayer path

  • parent_dir_path (Stirng) (defaults to: '')
  • arg (Hash) (defaults to: {})

    options

Returns:

  • nil



30
31
32
33
34
35
36
37
38
39
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 30

def sub_command_regist(under_path, parent_dir_path = '', arg = {})
  b_path = Idcf::Cli::Lib::Util::Name.namespace(name).underscore
  Dir.glob(parent_dir_path + "/#{under_path}/*.rb").each do |f|
    file_name = File.basename(f, '.*')
    next if file_name == 'base'

    require_path = "#{b_path}/#{under_path}/#{file_name}"
    command_regist(file_name, require_path, arg)
  end
end

.subcommand_structureObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/idcf/cli/gem_ext/thor/init_util.rb', line 75

def subcommand_structure
  {}.tap do |result|
    commands.each_key do |k|
      result[k.to_s] = nil
    end
    result = subcommand_class_structure(result)

    map.each do |k, v|
      result[k.to_s] = result[v.to_s] unless result[v.to_s].nil?
    end
  end
end