require 'safe_yaml'
SafeYAML::OPTIONS[:default_mode] = :safe

SUBSCRIPTIONS_ROOT = 'Membres'

def is_valid_subscription?(content)
  return false if content.length == 0
  return false unless content.start_with?("---\n")
  begin
    data = YAML.load(content)
  rescue ArgumentError
    # Parse error
    return false
  end
  ['name', 'email'].each do |key|
    return false unless data.include?(key)
  end
  if data.include?('membership_fee_paid_on')
    return false unless data.is_a?(Date)
  end
  true
end

def is_valid_subscription_file?(ref, file)
  IO.popen(['git', 'show', "#{ref}:#{file}"]) do |f|
    is_valid_subscription?(f.read)
  end
end
