Class: FunctionsFramework::CLI
- Inherits:
-
Object
- Object
- FunctionsFramework::CLI
- 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.
::Logger::Severity::INFO
Instance Attribute Summary collapse
- #error_message ⇒ String? readonly
-
#exit_code ⇒ Integer
readonly
The current exit status.
- #pidfile ⇒ String? readonly
Instance Method Summary collapse
-
#complete ⇒ Object
Finish the CLI, displaying any error status and exiting with the current exit code.
-
#error? ⇒ boolean
Determine if an error has occurred.
-
#initialize ⇒ CLI
constructor
Create a new CLI, setting arguments to their defaults.
-
#parse_args(argv) ⇒ self
Parse the given command line arguments.
-
#run ⇒ self
Perform the requested function.
Constructor Details
#initialize ⇒ CLI
Create a new CLI, setting arguments to their defaults.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 @pidfile = 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_message ⇒ String? (readonly)
69 70 71 |
# File 'lib/functions_framework/cli.rb', line 69 def @error_message end |
#exit_code ⇒ Integer (readonly)
Returns The current exit status.
63 64 65 |
# File 'lib/functions_framework/cli.rb', line 63 def exit_code @exit_code end |
#pidfile ⇒ String? (readonly)
75 76 77 |
# File 'lib/functions_framework/cli.rb', line 75 def pidfile @pidfile end |
Instance Method Details
#complete ⇒ Object
Finish the CLI, displaying any error status and exiting with the current exit code. Never returns.
184 185 186 187 |
# File 'lib/functions_framework/cli.rb', line 184 def complete warn @error_message if @error_message exit @exit_code end |
#error? ⇒ boolean
Determine if an error has occurred
56 57 58 |
# File 'lib/functions_framework/cli.rb', line 56 def error? !@error_message.nil? end |
#parse_args(argv) ⇒ self
Parse the given command line arguments. Exits if argument parsing failed.
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/functions_framework/cli.rb', line 84 def parse_args argv # rubocop:disable Metrics/MethodLength,Metrics/AbcSize @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", "--pidfile PIDFILE", "Set the pidfile for the server (defaults to puma.pid)" do |val| @pidfile = 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 thread pool size" do |val| @min_threads = val end op.on "--max-threads NUM", "Set the maximum thread 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.}\n#{@option_parser}", 2 end self end |
#run ⇒ self
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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/functions_framework/cli.rb', line 156 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. end else begin start_server.wait_until_stopped rescue ::StandardError => e error! e. end end self end |