summaryrefslogtreecommitdiff
path: root/scripts/changed-feature-flags
blob: 3a4f18bd78f838b39a9e149bb207235ea59f4b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'yaml'
require 'optparse'
require_relative 'api/default_options'

# This script returns the desired feature flag state as a comma-separated string for the feature flags in the specified files.
# Each desired feature flag state is specified as 'feature-flag=state'.
#
# For example, if the specified files included `config/feature_flags/development/ci_yaml_limit_size.yml` and the desired
# state as specified by the second argument was enabled, the value returned would be `ci_yaml_limit_size=enabled`

class GetFeatureFlagsFromFiles
  def initialize(options)
    @files = options.delete(:files)
    @state = options.delete(:state)
  end

  def extracted_flags
    files.each_with_object([]) do |file_path, all|
      next unless file_path =~ %r{/feature_flags/(development|ops)/.*\.yml}
      next unless File.exist?(file_path)

      ff_yaml = YAML.safe_load(File.read(file_path))
      ff_to_add = "#{ff_yaml['name']}"
      ff_to_add += "=#{state}" unless state.to_s.empty?

      all << ff_to_add
    end.join(',')
  end

  private

  attr_reader :files, :state
end

if $0 == __FILE__
  options = API::DEFAULT_OPTIONS.dup

  OptionParser.new do |opts|
    opts.on("-f", "--files FILES", Array, "Comma-separated list of feature flag config files") do |value|
      options[:files] = value
    end

    opts.on("-s", "--state STATE", String,
      "The desired state of the feature flags (enabled or disabled). If not specified the output will only list the feature flags."
    ) do |value|
      options[:state] = value
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end.parse!

  puts GetFeatureFlagsFromFiles.new(options).extracted_flags
end