From: Lunar Date: Sun, 2 Jun 2013 09:09:12 +0000 (+0200) Subject: Move script logic into module methods X-Git-Url: https://nos-oignons.net/gitweb/gestion-adh.git/commitdiff_plain/3e57bb279787236b1349f6b1da2ecf61ed4211e0 Move script logic into module methods --- diff --git a/bin/list-emails b/bin/list-emails index 6b13304..5315d86 100755 --- a/bin/list-emails +++ b/bin/list-emails @@ -5,10 +5,6 @@ require 'rubygems' require 'bundler' Bundler.setup -require 'nos_oignons/subscriptions' +require 'nos_oignons' -NosOignons::Subscription.all.each do |subscription| - if subscription.up_to_date? - puts subscription.email - end -end +NosOignons.list_emails! diff --git a/bin/pre-commit-hook b/bin/pre-commit-hook index 921c39c..2bd3c77 100755 --- a/bin/pre-commit-hook +++ b/bin/pre-commit-hook @@ -5,25 +5,6 @@ require 'rubygems' require 'bundler' Bundler.setup -require 'nos_oignons/git' -require 'nos_oignons/subscriptions' +require 'nos_oignons' -if system('git rev-parse --quiet --verify HEAD >/dev/null') - against = 'HEAD' -else - # Initial commit: diff against an empty tree object - against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904' -end - -IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io| - NosOignons::Git.handle_modified_files(io) do |file| - next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/") - begin - # Use empty ref to get the index - NosOignons::Subscription.read_from_git('', file) - rescue ArgumentError - $stderr.puts "Désolé : #{file} n'a pas le bon format !" - exit 1 - end - end -end +NosOignons.pre_commit_hook! diff --git a/bin/pre-receive-hook b/bin/pre-receive-hook index c86b325..8c204df 100755 --- a/bin/pre-receive-hook +++ b/bin/pre-receive-hook @@ -5,20 +5,6 @@ require 'rubygems' require 'bundler' Bundler.setup -require 'nos_oignons/git' -require 'nos_oignons/subscriptions' +require 'nos_oignons' -$stdin.readlines.each do |ref_line| - old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3) - IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io| - NosOignons::Git.handle_modified_files(io) do |file| - next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/") - begin - NosOignons::Subscription.read_from_git(new_value, file) - rescue ArgumentError - $stderr.puts "Désolé : #{file} n'a pas le bon format !" - exit 1 - end - end - end -end +NosOignons.pre_receive_hook!($stdin) diff --git a/bin/update-ag-subscribers b/bin/update-ag-subscribers index f10978e..59d5a91 100755 --- a/bin/update-ag-subscribers +++ b/bin/update-ag-subscribers @@ -5,13 +5,6 @@ require 'rubygems' require 'bundler' Bundler.setup -require 'nos_oignons/mailman' -require 'nos_oignons/subscriptions' +require 'nos_oignons' -LIST = 'ag' - -current_emails = NosOignons::Mailman.list_members(LIST) -uptodate_emails = NosOignons::Subscription.all.select(&:up_to_date?).collect(&:email) - -NosOignons::Mailman.add_members(LIST, uptodate_emails - current_emails) -NosOignons::Mailman.remove_members(LIST, current_emails - uptodate_emails) +NosOignons.update_ag_subscribers! diff --git a/lib/nos_oignons.rb b/lib/nos_oignons.rb new file mode 100644 index 0000000..d377775 --- /dev/null +++ b/lib/nos_oignons.rb @@ -0,0 +1,66 @@ +#-*- coding: utf-8 -*- + +require 'nos_oignons/git' +require 'nos_oignons/mailman' +require 'nos_oignons/subscriptions' + +module NosOignons + # The following class methods are all meant to be called as command-line scripts + class << self + def list_emails! + NosOignons::Subscription.all.each do |subscription| + if subscription.up_to_date? + puts subscription.email + end + end + end + + MEMBER_MAILING_LIST = 'ag' + def update_ag_subscribers! + current_emails = NosOignons::Mailman.list_members(MEMBER_MAILING_LIST) + uptodate_emails = NosOignons::Subscription.all.select(&:up_to_date?).collect(&:email) + + NosOignons::Mailman.add_members(MEMBER_MAILING_LIST, uptodate_emails - current_emails) + NosOignons::Mailman.remove_members(MEMBER_MAILING_LIST, current_emails - uptodate_emails) + end + + def pre_commit_hook! + if system('git rev-parse --quiet --verify HEAD >/dev/null') + against = 'HEAD' + else + # Initial commit: diff against an empty tree object + against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904' + end + + IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io| + NosOignons::Git.handle_modified_files(io) do |file| + next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/") + begin + # Use empty ref to get the index + NosOignons::Subscription.read_from_git('', file) + rescue ArgumentError + $stderr.puts "Désolé : #{file} n'a pas le bon format !" + exit 1 + end + end + end + end + + def pre_receive_hook!(stdin) + stdin.readlines.each do |ref_line| + old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3) + IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io| + NosOignons::Git.handle_modified_files(io) do |file| + next unless file.start_with?("#{NosOignons::SUBSCRIPTIONS_ROOT}/") + begin + NosOignons::Subscription.read_from_git(new_value, file) + rescue ArgumentError + $stderr.puts "Désolé : #{file} n'a pas le bon format !" + exit 1 + end + end + end + end + end + end +end