Class: FunctionsFramework::CLI

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

Overview

Implementation of the functions-framework-ruby executable.

Constant Summary collapse

DEFAULT_LOGGING_LEVEL =

The default logging level, if not given in the environment variable.

Returns:

  • (Integer)
::Logger::Severity::INFO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Create a new CLI, setting arguments to their defaults.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/functions_framework/cli.rb', line 34

def initialize
  @target = ::ENV["FUNCTION_TARGET"] || ::FunctionsFramework::DEFAULT_TARGET
  @source = ::ENV["FUNCTION_SOURCE"] || ::FunctionsFramework::DEFAULT_SOURCE
  @env = nil
  @port = nil
  @bind = nil
  @min_threads = nil
  @max_threads = nil
  @detailed_errors = nil
  @signature_type = ::ENV["FUNCTION_SIGNATURE_TYPE"]
  @logging_level = init_logging_level
  @what_to_do = nil
  @error_message = nil
  @exit_code = 0
end

Instance Attribute Details

#error_messageString? (readonly)

Returns:

  • (String)

    The current error message.

  • (nil)

    if no error has occurred.



68
69
70
# File 'lib/functions_framework/cli.rb', line 68

def error_message
  @error_message
end

#exit_codeInteger (readonly)

Returns The current exit status.

Returns:

  • (Integer)

    The current exit status.



62
63
64
# File 'lib/functions_framework/cli.rb', line 62

def exit_code
  @exit_code
end

Instance Method Details

#completeObject

Finish the CLI, displaying any error status and exiting with the current exit code. Never returns.



174
175
176
177
# File 'lib/functions_framework/cli.rb', line 174

def complete
  warn @error_message if @error_message
  exit @exit_code
end

#error?boolean

Determine if an error has occurred

Returns:

  • (boolean)


55
56
57
# File 'lib/functions_framework/cli.rb', line 55

def error?
  !@error_message.nil?
end

#parse_args(argv) ⇒ self

Parse the given command line arguments. Exits if argument parsing failed.

Parameters:

  • argv (Array<String>)

Returns:

  • (self)


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
# File 'lib/functions_framework/cli.rb', line 77

def parse_args argv # rubocop:disable Metrics/MethodLength
  @option_parser = ::OptionParser.new do |op| # rubocop:disable Metrics/BlockLength
    op.on "-t", "--target TARGET",
          "Set the name of the function to execute (defaults to #{DEFAULT_TARGET})" do |val|
      @target = val
    end
    op.on "-s", "--source SOURCE",
          "Set the source file to load (defaults to #{DEFAULT_SOURCE})" do |val|
      @source = val
    end
    op.on "--signature-type TYPE",
          "Asserts that the function has the given signature type." \
          " Supported values are 'http' and 'cloudevent'." do |val|
      @signature_type = val
    end
    op.on "-p", "--port PORT", "Set the port to listen to (defaults to 8080)" do |val|
      @port = val.to_i
    end
    op.on "-b", "--bind BIND", "Set the address to bind to (defaults to 0.0.0.0)" do |val|
      @bind = val
    end
    op.on "-e", "--environment ENV", "Set the Rack environment" do |val|
      @env = val
    end
    op.on "--min-threads NUM", "Set the minimum threead pool size" do |val|
      @min_threads = val
    end
    op.on "--max-threads NUM", "Set the maximum threead pool size" do |val|
      @max_threads = val
    end
    op.on "--[no-]detailed-errors", "Set whether to show error details" do |val|
      @detailed_errors = val
    end
    op.on "--verify", "Verify the app only, but do not run the server." do
      @what_to_do ||= :verify
    end
    op.on "-v", "--verbose", "Increase log verbosity" do
      @logging_level -= 1
    end
    op.on "-q", "--quiet", "Decrease log verbosity" do
      @logging_level += 1
    end
    op.on "--version", "Display the framework version" do
      @what_to_do ||= :version
    end
    op.on "--help", "Display help" do
      @what_to_do ||= :help
    end
  end
  begin
    @option_parser.parse! argv
    error! "Unrecognized arguments: #{argv}\n#{@option_parser}", 2 unless argv.empty?
  rescue ::OptionParser::ParseError => e
    error! "#{e.message}\n#{@option_parser}", 2
  end
  self
end

#runself

Perform the requested function.

  • If the --version flag was given, display the version.
  • If the --help flag was given, display online help.
  • If the --verify flag was given, load and verify the function, displaying any errors, then exit without starting a server.
  • Otherwise, start the configured server and block until it stops.

Returns:

  • (self)


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/functions_framework/cli.rb', line 146

def run
  return self if error?
  case @what_to_do
  when :version
    puts ::FunctionsFramework::VERSION
  when :help
    puts @option_parser
  when :verify
    begin
      load_function
      puts "OK"
    rescue ::StandardError => e
      error! e.message
    end
  else
    begin
      start_server.wait_until_stopped
    rescue ::StandardError => e
      error! e.message
    end
  end
  self
end