Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for options with spaces to riemann-wrapper #280

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions bin/riemann-wrapper
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'strscan'

Process.setproctitle($PROGRAM_NAME)

def camelize(subject)
Expand All @@ -15,6 +17,42 @@ def constantize(subject)
Object.const_get(subject)
end

# Split on blanks unless quoted or escaped:
# "--foo --bar=bar --baz='baz baz'\ baz" #=> ["--foo", "--bar=bar", "--baz=baz baz baz"]
def split_options(options)
res = []

return res unless options

current = ''
s = StringScanner.new(options)
until s.eos?
if s.scan(/\s+/)
res << current
current = ''
elsif s.scan(/\\./)
current += s.matched[1]
elsif s.scan(/['"]/)
match = s.matched
loop do
if s.scan(match)
break
elsif s.scan(/\\./)
current += s.matched[1]
else
current += s.getch
end
end
else
current += s.getch
end
end

res << current unless current.empty?

res
end

def read_flags(argv)
res = []

Expand Down Expand Up @@ -80,10 +118,14 @@ if ARGV.size == 1
require 'yaml'
config = YAML.safe_load(File.read(ARGV[0]))

commandline = config['options']
config['tools'].each { |tool| commandline << " -- #{tool['name']} #{tool['options']}" }
arguments = split_options(config['options'])
config['tools'].each do |tool|
arguments << '--'
arguments << tool['name']
arguments += split_options(tool['options'])
end

ARGV.replace(commandline.split)
ARGV.replace(arguments)
end

argv = ARGV.dup
Expand Down