Skip to content

Instantly share code, notes, and snippets.

@bf4
Forked from synth/flag_registration.rb
Created April 19, 2024 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/5d978227543ce04236ef2ae6bacc5393 to your computer and use it in GitHub Desktop.
Save bf4/5d978227543ce04236ef2ae6bacc5393 to your computer and use it in GitHub Desktop.
Feature flag code detection
# https://gist.github.com/synth/8c9eee23aa9df535aa42a30f7cff9ba9
require "parser/current"
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.registered_flags=(flags)
File.write("config/feature_flags.yml", flags.to_yaml)
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.filter_map { |line|
ast = Parser::CurrentRuby.parse(line)
ast.children[2].children[0]&.to_s
}.sort.uniq
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