for user to: - add email mapping - delete email mapping - re-map email mapping - search email mapping - search all email mapping from stdin or map file or - delete email mapping queue
Usage: email_mapping -n name [-e email_address] [-d] [-s] [-r] email_mapping -f mapping_file [-r] [-d] email_mapping --delete-queue email_mapping --search-all
options: -n, --name name|email|tag appoint a name|email|tag to add mapping -e, --email email_address appoint a email to be mapped -f, --file filename appoint a mapping file for name/email -r, --re-map do re-mappings -d, --delete delete email mappings -s, --search search email mapping -h, --help show this message --delete-queue delete mapping queue --search-all search all email mappings
the name can be either a name, an email or something like a tag. the file content consists of names and emails, like:
name: email_address_d tag: email_address_d email_address_r: email_address_d
can map multi name|tag|email_address to a single email address
Signed-off-by: Luan Shengde shdluan@163.com --- sbin/email-mapping.rb | 119 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 sbin/email-mapping.rb
diff --git a/sbin/email-mapping.rb b/sbin/email-mapping.rb new file mode 100755 index 0000000..365aabf --- /dev/null +++ b/sbin/email-mapping.rb @@ -0,0 +1,119 @@ +#!/usr/bin/env ruby +# SPDX-License-Identifier: MulanPSL-2.0+ +# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved. +# frozen_string_literal: true + +require 'yaml' +require 'optparse' + +REDIS_HOST = 'localhost' +REDIS_PORT = '6379' + +# require_relative "#{ENV['CCI_SRC']}/lib/redis_client" +require '../lib/redis_client' + +r_name = nil +d_email = nil +file_email_mapping = {} +map_conf = { + 're_mapping' => false, + 'del_map' => false, + 'search_map' => false, + 'delete_queue' => false, + 'search_all_map' => false +} + +options = OptionParser.new do |opts| + opts.banner = "Usage: email_mapping -n name [-e email_address] [-d] [-s] [-r]\n" + opts.banner += " email_mapping -f mapping_file [-r] [-d] [-s]\n" + opts.banner += " email_mapping --delete-queue\n" + opts.banner += " email_mapping --search-all\n" + + opts.separator '' + opts.separator 'options:' + + opts.on('-n name|email|tag', '--name name|email|tag', 'appoint a name|email|tag to add mapping') do |name| + r_name = name + end + + opts.on('-e email_address', '--email email_address', 'appoint a email to be mapped') do |email| + d_email = email + end + + opts.on('-f filename', '--file filename', 'appoint a mapping file for name/email') do |filename| + mapping_content = YAML.load_file(filename) || {} + file_email_mapping.update mapping_content + end + + opts.on('-r', '--re-map', 'do re-mappings') do + map_conf['re_mapping'] = true + end + + opts.on('-d', '--delete', 'delete email mappings') do + map_conf['del_map'] = true + end + + opts.on('--delete-queue', 'delete mapping queue') do + map_conf['delete_queue'] = true + end + + opts.on('-s', '--search', 'search email mapping') do + map_conf['search_map'] = true + end + + opts.on('--search-all', 'search all email mappings') do + map_conf['search_all_map'] = true + end + + opts.on_tail('-h', '--help', 'show this message') do + puts opts + exit + end +end + +required_opts = ['-n', '-f', '--delete-queue', '--search-all'] + +if ARGV.empty? + ARGV << '-h' +elsif (required_opts - ARGV).eql? required_opts + ARGV.clear + ARGV << '-h' +end + +options.parse!(ARGV) + +def email_mapping(r_name, d_email, map_conf) + email_mapping = RedisClient.new('email_mapping') + if map_conf['re_mapping'] + email_mapping.reset_hash_key(r_name, d_email) + elsif map_conf['del_map'] + email_mapping.delete_hash_key(r_name) + elsif map_conf['search_map'] + mapped_email = email_mapping.search_hash_key(r_name) + if mapped_email.nil? || mapped_email.empty? + puts "#{r_name} has not been mapped an email yet." + else + puts mapped_email + end + elsif map_conf['search_all_map'] + all_mappings = email_mapping.search_all_hash_key + + all_mappings.each do |k, v| + puts "#{k}: #{v}\n" + end + elsif map_conf['delete_queue'] + email_mapping.delete_queue + else + return if email_mapping.add_hash_key(r_name, d_email) + + puts "#{r_name} has already mapped an email." + end +end + +if file_email_mapping.empty? + email_mapping(r_name, d_email, map_conf) +else + file_email_mapping.each do |name, email| + email_mapping(name, email, map_conf) + end +end