Class: Prdigest::CLI

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

Defined Under Namespace

Classes: ParseError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/prdigest/cli.rb', line 147

def exit_on_failure?
  true
end

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



28
29
30
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/prdigest/cli.rb', line 28

def invoke(argv = ARGV, out: $stdout, err: $stderr, env: ENV,
           system_path: "/etc/prdigest/config.yml", runner_factory: nil,
           facts_runner_factory: nil, prose_runner_factory: nil)
  json_intent = Array(argv).any? { |arg| arg == "--json" || arg.start_with?("--json=") }
  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])

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

  path = Config.resolve_path(explicit: parsed[:config], env: env, system_path: system_path)
  capability = config_capability(parsed)
  config = Config.load(path, capability: capability)
  if parsed[:command] == "serve"
    err.puts "prdigest serve: not implemented — use systemd timer + `prdigest run`"
    return 0
  end

  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

  raise ConfigError, "GitHub token is missing" if config.github_token(env).empty?
  if !parsed[:dry_run] && config.telegram_token(env).empty?
    raise ConfigError, "Telegram bot token is missing"
  end

  factory = runner_factory || ->(**options) { Runner.new(**options) }
  result = factory.call(
    config: config,
    date: parsed[:date],
    dry_run: parsed[:dry_run],
    repositories: repositories,
    env: env
  ).call
  present(result, json: parsed[:json], out: out, err: err)
  result.exit_code
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

  result = Result.failure(mode: "scheduled", error_kind: "cli", message: e.message)
  present(result, json: json_intent, out: out, err: err)
  result.exit_code
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

  result = Result.failure(mode: parsed && parsed[:date] ? "explicit_date_replay" : "scheduled", error_kind: "config", message: e.message)
  present(result, json: parsed ? parsed[:json] : json_intent, out: out, err: err)
  result.exit_code
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

  result = Result.failure(
    mode: parsed && parsed[:date] ? "explicit_date_replay" : "scheduled",
    error_kind: "internal",
    message: "unexpected CLI failure (#{e.class})"
  )
  present(result, json: parsed ? parsed[:json] : json_intent, out: out, err: err)
  result.exit_code
end

Instance Method Details

#factsObject



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

def facts; end

#proseObject



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

def prose; end

#run_cmdObject



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

def run_cmd; end

#serveObject



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

def serve; end

#versionObject



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

def version; end