Skip to content

Commit

Permalink
sync syntax and output of user agent string updater
Browse files Browse the repository at this point in the history
  • Loading branch information
h00die committed Dec 30, 2024
1 parent 1d97e7e commit 81a88b8
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions tools/dev/update_user_agent_strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,58 @@
end
optparse.parse!

# colors and puts templates from msftidy.rb

class String
def red
"\e[1;31;40m#{self}\e[0m"
end

def yellow
"\e[1;33;40m#{self}\e[0m"
end

def green
"\e[1;32;40m#{self}\e[0m"
end

def cyan
"\e[1;36;40m#{self}\e[0m"
end
end

#
# Display an error message, given some text
#
def error(txt)
puts "[#{'ERROR'.red}] #{cleanup_text(txt)}"
end

#
# Display a warning message, given some text
#
def warning(txt)
puts "[#{'WARNING'.yellow}] #{cleanup_text(txt)}"
end

#
# Display a info message, given some text
#
def info(txt)
puts "[#{'INFO'.cyan}] #{cleanup_text(txt)}"
end

def cleanup_text(txt)
# remove line breaks
txt = txt.gsub(/[\r\n]/, ' ')
# replace multiple spaces by one space
txt.gsub(/\s{2,}/, ' ')
end

def replace_agent_string(lines, replace_marker, url, regex)
valid_chars = 'a-zA-Z0-9\(\);:\.,/_ '
regex = regex.gsub('{VALID_CHARS}', valid_chars)
puts "Updating #{replace_marker}"
info "Checking: #{replace_marker}"

index = lines.index { |line| line.include?(replace_marker) }
raise "Couldn't find marker #{replace_marker}" if index.nil?
Expand All @@ -32,16 +79,16 @@ def replace_agent_string(lines, replace_marker, url, regex)
raise "Couldn't match regex #{regex}" if match.nil?

new_string = match[1]
puts " New value is: #{new_string}"

old_line = lines[index]
if old_line.include?("'#{new_string}'")
puts '(This is unchanged from the previous value)'
puts " (Unchanged): #{new_string}"
else
new_line = old_line.gsub(/'(.*)'/, "'#{new_string}'")
if old_line == new_line
raise "Line didn't change: #{old_line}"
raise " Line didn't change: #{old_line}"
end

puts " New value is: #{new_string}"
lines[index] = new_line
end
end
Expand Down

0 comments on commit 81a88b8

Please sign in to comment.