Class: YleTfPlugins::CommandHelp::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/yle_tf_plugins/commands/help/command.rb

Instance Method Summary collapse

Instance Method Details

#device(env) ⇒ Object



41
42
43
# File 'lib/yle_tf_plugins/commands/help/command.rb', line 41

def device(env)
  error?(env) ? STDERR : STDOUT
end

#error?(env) ⇒ Boolean

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

Returns:

  • (Boolean)


37
38
39
# File 'lib/yle_tf_plugins/commands/help/command.rb', line 37

def error?(env)
  env[:tf_env] == 'error'
end

#execute(env) ⇒ Object



9
10
11
12
# File 'lib/yle_tf_plugins/commands/help/command.rb', line 9

def execute(env)
  device(env).puts(opts.help)
  exit 1 if error?(env)
end

#optsObject

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yle_tf_plugins/commands/help/command.rb', line 15

def opts
  OptionParser.new do |o|
    o.summary_width = 18
    o.banner = 'Usage: tf <environment> <command> [<args>]'
    o.separator ''
    o.separator 'YleTf options:'
    o.on('-h', '--help', 'Prints this help')
    o.on('-v', '--version', 'Prints the version information')
    o.on('--debug', 'Print debug information')
    o.on('--no-color', 'Do not output with colors')
    o.on('--no-hooks', 'Do not run any hooks')
    o.on('--only-hooks', 'Only run the hooks')
    o.separator ''
    o.separator 'Special tf commands:'
    o.separator tf_command_help
    o.separator ''
    o.separator 'Terraform commands:'
    o.separator terraform_help
  end
end

#terraform_helpObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/yle_tf_plugins/commands/help/command.rb', line 51

def terraform_help
  on_error = proc do |exit_code|
    # exit_code is nil if the command was not found
    # Ignore other exit codes, as older Terraform versions return
    # non-zero exit codes for the help.
    return '    [Terraform not found]' if exit_code.nil?
  end
  help = YleTf::System.read_cmd('terraform', '--help', error_handler: on_error)
  help.lines.grep(/^    /)
end

#tf_command_helpObject



45
46
47
48
49
# File 'lib/yle_tf_plugins/commands/help/command.rb', line 45

def tf_command_help
  YleTf::Plugin.manager.commands.sort.map do |command, data|
    "    #{command.ljust(18)} #{data[:synopsis]}"
  end
end