Skip to content

Instantly share code, notes, and snippets.

@synth
Created February 14, 2024 06:33
Show Gist options
  • Save synth/8c9eee23aa9df535aa42a30f7cff9ba9 to your computer and use it in GitHub Desktop.
Save synth/8c9eee23aa9df535aa42a30f7cff9ba9 to your computer and use it in GitHub Desktop.
Feature flag code detection
module Flipper
module FlagRegistration
# These functions are all memoized because they should be static for the
# lifetime of a deployment (albeit they are really static to a Ruby process)
def self.registered_flags
@registered_flags ||= YAML.load_file("config/feature_flags.yml")
end
def self.flags_in_code
@flags_in_code ||= begin
search_regex = 'Flipper.enabled\?\(.*\)'
command = "grep -rohE --exclude-dir='.//.*' --include='*rb' '#{search_regex}' ./"
results = `#{command}`
lines = results.split("\n")
lines.map do |line|
ast = Parser::CurrentRuby.parse(line)
ast.children[2].children[0]
end
end
end
def self.registered_flags_not_found
@registered_flags_not_found ||= flags_in_code - registered_flags.keys.map(&:to_sym)
end
def self.flags_not_registered
@flags_not_registered ||= flags_in_code - registered_flags.keys.map(&:to_sym)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment