Class: Dri::Commands::Fetch::Runbooks

Inherits:
Dri::Command show all
Defined in:
lib/dri/commands/fetch/runbooks.rb

Instance Method Summary collapse

Methods inherited from Dri::Command

#add_color, #api_client, #bold, #command, #config, #cursor, #editor, #emoji, #handover_report_path, #logger, #ops_token, #pastel, #profile, #prompt, #spinner, #timezone, #token, #username, #verify_config_exists

Constructor Details

#initialize(options) ⇒ Runbooks

Returns a new instance of Runbooks.



13
14
15
# File 'lib/dri/commands/fetch/runbooks.rb', line 13

def initialize(options)
  @options = options
end

Instance Method Details

#execute(folder: nil, _input: $stdin, output: $stdout) ⇒ Object



17
18
19
20
21
# File 'lib/dri/commands/fetch/runbooks.rb', line 17

def execute(folder: nil, _input: $stdin, output: $stdout)
  return fetch_summary(output: output) unless folder

  fetch_runbook("#{folder}/index.md", output: output)
end

#fetch_runbook(path, output:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dri/commands/fetch/runbooks.rb', line 36

def fetch_runbook(path, output:)
  runbook = nil
  spinner.run do
    runbook = api_client.get_file(
      path,
      ref: 'main',
      project_id: Utils::Constants::ProjectIDs::RUNBOOKS_PROJECT_ID
    )
  rescue Gitlab::Error::NotFound
    logger.error "The runbook #{path} could not be found."
  end

  return unless runbook

  content = Base64.decode64(runbook.content)
  markdown(content, output)
end

#fetch_summary(output:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dri/commands/fetch/runbooks.rb', line 23

def fetch_summary(output:)
  logger.info 'Fetching runbooks'
  prompt = TTY::Prompt.new
  runbooks = api_client.list_runbooks

  return output.puts 'No runbooks could be found' if runbooks.empty?

  choices = runbooks.map(&:path)
  path = prompt.select('Which runbook would you like to look at?', choices)

  fetch_runbook("#{path}/index.md", output: output)
end