for user to:
- add email mapping
- delete email mapping
- re-map email mapping
- search 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
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
--delete-queue delete mapping queue
-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(a)163.com>
---
sbin/email-mapping.rb | 99 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 99 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..cfc70ec
--- /dev/null
+++ b/sbin/email-mapping.rb
@@ -0,0 +1,99 @@
+#!/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 "#{ENV['CCI_SRC']}/lib/email_mapping_client"
+
+r_name = nil
+d_email = nil
+file_email_mapping = {}
+map_conf = {
+ 're_mapping' => false,
+ 'del_map' => false,
+ 'search_map' => false,
+ 'delete_queue' => 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]\n"
+ opts.banner += " email_mapping --delete-queue\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
+ 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_tail('-h', '--help', 'show this message') do
+ puts opts
+ exit
+ end
+end
+
+if ARGV.empty?
+ ARGV << '-h'
+elsif (['-n', '-f', '--delete-queue'] - ARGV).eql? ['-n', '-f', '--delete-queue']
+ ARGV.clear
+ ARGV << '-h'
+end
+
+options.parse!(ARGV)
+
+def email_mapping(r_name, d_email, map_conf)
+ email_mapping = EmailMapping.new(r_name, d_email)
+ if map_conf['re_mapping']
+ email_mapping.re_map_email
+ elsif map_conf['del_map']
+ email_mapping.delete_email_mapping
+ elsif map_conf['search_map']
+ puts email_mapping.search_map
+ elsif map_conf['delete_queue']
+ email_mapping.delete_mapping_queue
+ else
+ return if email_mapping.add_email_mapping
+
+ 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
--
2.23.0