Class: Minfra::Cli::CliStarter

Inherits:
Object
  • Object
show all
Defined in:
lib/minfra/cli/cli_starter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CliStarter

Returns a new instance of CliStarter.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/minfra/cli/cli_starter.rb', line 16

def initialize(argv)
  @argv = argv
  @options = {} # base_path, env, argv_file
  @minfrarc_loaded = false
  @minfrarc_me_loaded = false

  parse_global_options

  @base_path = Pathname.new(@options[:base_path] || ENV.fetch('MINFRA_PATH', nil)).expand_path
  @env_name = @options[:env] || ENV['MINFRA_ENVIRONMENT'] || 'dev'
  init_config

  init_logger

  @logger.debug("Minfra: loglevel: #{@logger.level}, env: #{@config.orch_env}, options: #{@options.inspect}")

  init_minfrarc unless @options[:norc]
  init_envs
  
  init_hiera
  init_plugins

  register_subcommands

  @plugins.setup
  require_relative 'main_command'

  setup_subcommands
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def argv
  @argv
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def base_path
  @base_path
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def config
  @config
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def env
  @env
end

#env_nameObject (readonly)

Returns the value of attribute env_name.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def env_name
  @env_name
end

#envsObject (readonly)

Returns the value of attribute envs.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def envs
  @envs
end

#hieraObject (readonly)

Returns the value of attribute hiera.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def hiera
  @hiera
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def options
  @options
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



6
7
8
# File 'lib/minfra/cli/cli_starter.rb', line 6

def plugins
  @plugins
end

Instance Method Details

#installObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/minfra/cli/cli_starter.rb', line 69

def install
  cli = self
  Kernel.define_method(:minfra_cli) do
    cli
  end
  Kernel.define_method(:l) do |key, default = nil|
    minfra_cli.hiera.l(key, default)
  end
  Kernel.define_method(:l!) do |key, default = nil|
    minfra_cli.hiera.l!(key, default)
  end
end

#minfrarc_loaded?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/minfra/cli/cli_starter.rb', line 8

def minfrarc_loaded?
  @minfrarc_loaded
end

#minfrarc_me_loaded?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/minfra/cli/cli_starter.rb', line 12

def minfrarc_me_loaded?
  @minfrarc_me_loaded
end

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/minfra/cli/cli_starter.rb', line 46

def run
  exit_code = 0
  if @options[:argv_file]
    CSV.foreach(@options[:argv_file]) do |row|
      args = @argv + row
      @logger.debug("Running (#{env_name}): #{args.join(' ')} ")
      begin
        Minfra::Cli::Main.start(args)
      rescue StandardError # esp. Minfra::Cli::Errors::ExitError !
        exit_code = 1
      end
    end
    @logger.debug('Done argv_file loop')
  else
    begin
      Minfra::Cli::Main.start(@argv)
    rescue Minfra::Cli::Errors::ExitError
      exit_code = 1
    end
  end
  exit_code
end