Class: FunctionsFramework::CLI

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

Overview

Implementation of the functions-framework executable.

Instance Method Summary collapse

Constructor Details

#initializeCLI

Create a new CLI, setting arguments to their defaults.



27
28
29
30
31
32
33
34
35
36
# File 'lib/functions_framework/cli.rb', line 27

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
end

Instance Method Details

#parse_args(argv) ⇒ self

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

Parameters:

  • argv (Array<String>)

Returns:

  • (self)


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

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 "-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 "-v", "--verbose", "Increase log verbosity" do
      ::FunctionsFramework.logger.level -= 1
    end
    op.on "-q", "--quiet", "Decrease log verbosity" do
      ::FunctionsFramework.logger.level += 1
    end
    op.on "--help", "Display help" do
      puts op
      exit
    end
  end
  option_parser.parse! argv
  unless argv.empty?
    warn "Unrecognized arguments: #{argv}"
    puts op
    exit 1
  end
  self
end

#runself

Run the configured server, and block until it stops.

Returns:

  • (self)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/functions_framework/cli.rb', line 97

def run
  FunctionsFramework.logger.info \
    "FunctionsFramework: Loading functions from #{@source.inspect}..."
  load @source
  server = ::FunctionsFramework.start @target do |config|
    config.rack_env = @env
    config.port = @port
    config.bind_addr = @bind
    config.show_error_details = @detailed_errors
    config.min_threads = @min_threads
    config.max_threads = @max_threads
  end
  server.wait_until_stopped
  self
end