for user to: - add - delete - re-map - search email mapping from stdin or file
Usage: email_mapping -n name -e email_address email_mapping -n name -e email_address -r email_mapping -n name -d email_mapping -s name email_mapping -f mapping_file email_mapping -f mapping_file -r|-d
options: -n, --name name|email|logo appoint a name|email|logo 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
the name can be either a name, an email or something like logo the file content consists of names and emails, like:
name1: email_address1 name2: email_address2
you can map multi name to a single email address
Signed-off-by: Luan Shengde shdluan@163.com --- sbin/email-mapping.rb | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 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..09338c8 --- /dev/null +++ b/sbin/email-mapping.rb @@ -0,0 +1,90 @@ +#!/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 'redis' +require 'json' +require 'yaml' +require 'optparse' + +require "#{ENV['CCI_SRC']}/lib/email_mapping_client" + +r_name = nil +d_email = nil +email_mapping = nil +re_mapping = false +del_map = false +search_map = false + +options = OptionParser.new do |opts| + opts.banner = "Usage: email_mapping -n name -e email_address\n" + opts.banner += " email_mapping -n name -e email_address -r\n" + opts.banner += " email_mapping -n name -d\n" + opts.banner += " email_mapping -s name\n" + opts.banner += " email_mapping -f mapping_file\n" + opts.banner += " email_mapping -f mapping_file -r|-d\n" + + opts.separator '' + opts.separator 'options:' + + opts.on('-n name|email|logo', '--name name|email|logo', 'appoint a name|email|logo 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) || nil + email_mapping = mapping_content + end + + opts.on('-r', '--re-map', 'do re-mappings') do + re_mapping = true + end + + opts.on('-d', '--delete', 'delete email mappings') do + del_map = true + end + + opts.on('-s', '--search', 'search email mapping') do + search_map = true + end + + opts.on_tail('-h', '--help', 'show this message') do + puts opts + exit + end +end + +if ARGV.empty? + ARGV << '-h' +elsif (['-n', '-f'] - ARGV).eql? ['-n', '-f'] + ARGV.clear + ARGV << '-h' +end + +options.parse!(ARGV) + +def email_mapping(r_name, d_email, re_mapping, del_map, search_map) + email_mapping = AddEmailMapping.new(r_name, d_email) + if re_mapping + email_mapping.re_map_email + elsif del_map + email_mapping.delete_email_mapping + elsif search_map + puts email_mapping.search_map + else + email_mapping.add_email_mapping + end +end + +if email_mapping.nil? + email_mapping(r_name, d_email, re_mapping, del_map, search_map) +else + email_mapping.each do |name, email| + email_mapping(name, email, re_mapping, del_map, search_map) + end +end