Skip to content

Commit 1f1f485

Browse files
committed
add script/hooks
1 parent 8b7abf3 commit 1f1f485

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

script/hooks

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Development CLI script for the hooks framework
5+
6+
# Add lib directory to load path so we can require our code
7+
lib_dir = File.expand_path("../lib", __dir__)
8+
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
9+
10+
# Set bundle gemfile
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
require "bundler/setup"
14+
require "optparse"
15+
require "hooks"
16+
require "yaml"
17+
18+
# CLI implementation
19+
class HooksCLI
20+
def initialize
21+
@options = {
22+
config_file: "hooks.yaml",
23+
port: 4567,
24+
host: "0.0.0.0",
25+
environment: "development",
26+
threads: "5:5"
27+
}
28+
end
29+
30+
def run(args = ARGV)
31+
# Handle version and help flags before parsing other options
32+
if args.include?("--version") || args.include?("-v")
33+
puts Hooks::VERSION
34+
exit
35+
end
36+
37+
if args.include?("--help") || args.include?("-h") || args.include?("help")
38+
show_help
39+
exit
40+
end
41+
42+
parse_options(args)
43+
44+
case args.first
45+
when "start", nil
46+
start_server
47+
when "version"
48+
puts Hooks::VERSION
49+
else
50+
puts "Unknown command: #{args.first}"
51+
show_help
52+
exit 1
53+
end
54+
end
55+
56+
private
57+
58+
def parse_options(args)
59+
OptionParser.new do |opts|
60+
opts.banner = "Usage: hooks [command] [options]"
61+
62+
opts.on("-c", "--config FILE", "Configuration file (default: hooks.yaml)") do |file|
63+
@options[:config_file] = file
64+
end
65+
66+
opts.on("-p", "--port PORT", Integer, "Port to listen on (default: 4567)") do |port|
67+
@options[:port] = port
68+
end
69+
70+
opts.on("-H", "--host HOST", "Host to bind to (default: 0.0.0.0)") do |host|
71+
@options[:host] = host
72+
end
73+
74+
opts.on("-e", "--environment ENV", "Environment (default: development)") do |env|
75+
@options[:environment] = env
76+
end
77+
78+
opts.on("-t", "--threads THREADS", "Puma thread pool size (default: 5:5)") do |threads|
79+
@options[:threads] = threads
80+
end
81+
82+
opts.on("-h", "--help", "Show this help message") do
83+
show_help
84+
exit
85+
end
86+
87+
opts.on("-v", "--version", "Show version") do
88+
puts Hooks::VERSION
89+
exit
90+
end
91+
end.parse!(args)
92+
end
93+
94+
def start_server
95+
puts "Starting Hooks webhook server..."
96+
puts "Config file: #{@options[:config_file]}"
97+
puts "Host: #{@options[:host]}"
98+
puts "Port: #{@options[:port]}"
99+
puts "Environment: #{@options[:environment]}"
100+
puts "Threads: #{@options[:threads]}"
101+
puts
102+
103+
# parse the configuration file
104+
if File.exist?(@options[:config_file])
105+
begin
106+
config = YAML.load_file(@options[:config_file])
107+
rescue Psych::SyntaxError => e
108+
puts "Error parsing configuration file: #{e.message}"
109+
exit 1
110+
end
111+
else
112+
puts "Configuration file #{@options[:config_file]} not found. Using defaults."
113+
config = {}
114+
end
115+
116+
# Merge CLI options into config
117+
config.merge!({
118+
"host" => @options[:host],
119+
"port" => @options[:port],
120+
"environment" => @options[:environment],
121+
"threads" => @options[:threads]
122+
})
123+
124+
# Build the application with framework-level config
125+
app = Hooks.build(config:)
126+
127+
# Start the server with CLI options
128+
require "rack"
129+
require "rack/handler/puma"
130+
require "puma"
131+
132+
Rack::Handler::Puma.run(
133+
app,
134+
Host: @options[:host],
135+
Port: @options[:port],
136+
Threads: @options[:threads],
137+
environment: @options[:environment]
138+
)
139+
rescue Interrupt
140+
puts "\nShutting down gracefully..."
141+
exit 0
142+
rescue => e
143+
puts "Error starting server: #{e.message}"
144+
puts e.backtrace if @options[:environment] == "development"
145+
exit 1
146+
end
147+
148+
def show_help
149+
puts <<~HELP
150+
Hooks - A Pluggable Webhook Server Framework
151+
152+
Usage:
153+
hooks [start] Start the webhook server (default)
154+
hooks version Show version information
155+
hooks help Show this help message
156+
157+
Options:
158+
-c, --config FILE Configuration file (default: hooks.yaml)
159+
-p, --port PORT Port to listen on (default: 4567)
160+
-H, --host HOST Host to bind to (default: 0.0.0.0)
161+
-e, --environment ENV Environment (default: development)
162+
-t, --threads THREADS Puma thread pool size (default: 5:5)
163+
-h, --help Show this help message
164+
-v, --version Show version
165+
166+
Examples:
167+
hooks Start server with default settings
168+
hooks start -p 8080 Start server on port 8080
169+
hooks -c custom.yaml -e production Start with custom config in production mode
170+
hooks -t 10:10 Start with 10 threads
171+
hooks version Show version information
172+
173+
For more information, see the README.md file.
174+
HELP
175+
end
176+
end
177+
178+
# Run the CLI if this file is executed directly
179+
if __FILE__ == $0
180+
HooksCLI.new.run
181+
end

0 commit comments

Comments
 (0)