# frozen_string_literal: true require_relative '../../command' require_relative '../../utils/table' require_relative '../../utils/feature_flag_consts' require_relative '../../feature_flag_report' module Dri module Commands class Fetch class FeatureFlags < Dri::Command include Dri::Utils::Table include Dri::Utils::FeatureFlagConsts def initialize(options) @options = options @today_iso_format = Time.now.strftime('%Y-%m-%dT00:00:00Z') end def execute(input: $stdin, output: $stdout) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity verify_config_exists summary = add_color('Summary', :bright_yellow) changed_on = add_color('Changed(UTC)', :bright_yellow) url = add_color('URL', :bright_yellow) report = Dri::FeatureFlagReport.new headers = [summary, changed_on, url] logger.info "Fetching today's feature flag changes..." spinner.run do response = api_client.fetch_feature_flag_logs(@today_iso_format) if response.empty? logger.info 'It\'s been quiet...no feature flag changes for today 👀' break end response.each do |feature_flag| next unless TITLE_SUBSTRINGS.any? { |substr| feature_flag.title.include?(substr) } report.add_change(feature_flag) end end print_results('Production', headers, report.prod, output) unless report.prod.empty? print_results('Staging', headers, report.staging, output) unless report.staging.empty? print_results('Staging Ref', headers, report.staging_ref, output) unless report.staging_ref.empty? print_results('Preprod', headers, report.preprod, output) unless report.preprod.empty? end private def print_results(env, headers, rows, output_src) puts "\n#{add_color(env, :bright_blue)}" print_table(headers, rows) output_src.puts "\nFound: #{rows.count} feature flag change(s) set to true or false on #{env}." end end end end end