Class: Prdigest::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/prdigest/cli.rb

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

EXIT_CODES =
{
  "config" => 2, "cli" => 2, "refusal" => 2,
  "github" => 3,
  "telegram" => 4, "telegram_refused" => 4, "telegram_permanent" => 4,
  "telegram_ambiguous" => 4, "delivery_checkpoint" => 4,
  "delivery_checkpoint_permanent" => 4,
  "state" => 5,
  "provider" => 7, "provider_ambiguous" => 7
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/prdigest/cli.rb', line 117

def exit_on_failure?
  true
end

.invoke(argv = ARGV, out: $stdout, err: $stderr, env: ENV, system_path: "/etc/prdigest/config.yml", facts_runner_factory: nil, prose_runner_factory: nil) ⇒ Object Also known as: start



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/prdigest/cli.rb', line 31

def invoke(argv = ARGV, out: $stdout, err: $stderr, env: ENV,
           system_path: "/etc/prdigest/config.yml", facts_runner_factory: nil,
           prose_runner_factory: nil)
  intent = command_intent(argv)
  facts_intent = intent == "facts"
  prose_intent = intent == "prose"
  parsed = parse(argv)
  validate_command_options!(parsed, intent: intent)
  return print_version(out) if parsed[:command] == "version"
  raise ParseError, "--help is not valid for facts" if facts_intent && parsed[:command] == "help"
  return print_help(out) if %w[help --help -h].include?(parsed[:command])

  raise ParseError, "unknown command" unless %w[facts prose].include?(parsed[:command])

  path = Config.resolve_path(explicit: parsed[:config], env: env, system_path: system_path)
  capability = config_capability(parsed)
  config = Config.load(path, capability: capability)

  validate_date!(parsed[:date]) if parsed[:date]
  repositories = if parsed[:repos].empty?
                   nil
                 else
                   Config.normalize_repos(parsed[:repos], label: "--repo")
                 end
  if parsed[:command] == "facts"
    raise ConfigError, "GitHub token is missing" if config.github_token(env).empty?

    factory = facts_runner_factory || ->(**options) { FactsRunner.new(**options) }
    document = factory.call(
      config: config,
      date: parsed[:date],
      repositories: repositories,
      env: env
    ).call
    out.puts JSON.generate(document)
    return 0
  end

  if parsed[:command] == "prose"
    if parsed[:deliver]
      raise ConfigError, "Telegram bot token is missing" if config.telegram_token(env).empty?
    else
      raise ConfigError, "GitHub token is missing" if config.github_token(env).empty?
      if config.prose_api_key(env).empty?
        raise ConfigError, "prose API key environment variable is unset"
      end
    end

    factory = prose_runner_factory || ->(**options) { ProseRunner.new(**options) }
    outcome = factory.call(
      config: config,
      date: parsed[:date],
      deliver: parsed[:deliver],
      repositories: repositories,
      env: env
    ).call
    present_prose(outcome, out: out)
    return 0
  end
rescue ParseError => e
  return present_facts_failure(out, "cli", e.message) if facts_intent
  return present_prose_failure(err, "cli", e.message) if prose_intent

  present_cli_failure(err, "cli", e.message)
rescue ConfigError => e
  return present_facts_failure(out, "config", e.message) if facts_intent
  return present_prose_failure(err, "config", safe_prose_message(e, config: config, env: env)) if prose_intent

  present_cli_failure(err, "config", e.message)
rescue StandardError => e
  if facts_intent
    kind = e.is_a?(FetchError) ? e.kind : "internal"
    message = e.is_a?(FetchError) ? e.message : "unexpected facts failure (#{e.class})"
    return present_facts_failure(out, kind, message)
  end
  if prose_intent
    kind = prose_error_kind(e)
    message = safe_prose_message(e, config: config, env: env)
    return present_prose_failure(err, kind, message)
  end

  present_cli_failure(err, "internal", "unexpected CLI failure (#{e.class})")
end

Instance Method Details

#factsObject



22
# File 'lib/prdigest/cli.rb', line 22

def facts; end

#proseObject



25
# File 'lib/prdigest/cli.rb', line 25

def prose; end

#versionObject



28
# File 'lib/prdigest/cli.rb', line 28

def version; end