File: output.ex

package info (click to toggle)
rabbitmq-server 4.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,972 kB
  • sloc: erlang: 257,835; javascript: 22,466; sh: 3,037; makefile: 2,517; python: 1,966; xml: 646; cs: 335; java: 244; ruby: 212; php: 100; perl: 63; awk: 13
file content (72 lines) | stat: -rw-r--r-- 1,978 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
## This Source Code Form is subject to the terms of the Mozilla Public
## License, v. 2.0. If a copy of the MPL was not distributed with this
## file, You can obtain one at https://mozilla.org/MPL/2.0/.
##
## Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.  All rights reserved.

defmodule RabbitMQ.CLI.Core.Output do
  def format_output(:ok, _, _) do
    :ok
  end

  # the command intends to produce no output
  def format_output({:ok, nil}, _, _) do
    :ok
  end

  def format_output({:ok, :check_passed}, _, _) do
    :ok
  end

  def format_output({:ok, output}, formatter, options) do
    {:ok, formatter.format_output(output, options)}
  end

  def format_output({:stream, stream}, formatter, options) do
    {:stream, formatter.format_stream(stream, options)}
  end

  def print_output(output, printer, options) do
    {:ok, printer_state} = printer.init(options)
    exit_code = print_output_0(output, printer, printer_state)
    printer.finish(printer_state)
    exit_code
  end

  def print_output_0(:ok, printer, printer_state) do
    printer.print_ok(printer_state)
    :ok
  end

  # the command intends to produce no output
  def print_output_0({:ok, nil}, _printer, _printer_state) do
    :ok
  end

  def print_output_0({:ok, :check_passed}, _printer, _printer_state) do
    :ok
  end

  def print_output_0({:ok, single_value}, printer, printer_state) do
    printer.print_output(single_value, printer_state)
    :ok
  end

  def print_output_0({:stream, stream}, printer, printer_state) do
    case print_output_stream(stream, printer, printer_state) do
      :ok -> :ok
      {:error, _} = err -> err
    end
  end

  def print_output_stream(stream, printer, printer_state) do
    Enum.reduce_while(stream, :ok, fn
      {:error, err}, _ ->
        {:halt, {:error, err}}

      val, _ ->
        printer.print_output(val, printer_state)
        {:cont, :ok}
    end)
  end
end