From 745d3e82c561b3ff5bd63b5ce01960ab1450daef Mon Sep 17 00:00:00 2001 From: nishiki Date: Mon, 30 Dec 2013 20:15:00 +0100 Subject: [PATCH] add option select config file (--config CONFIG) --- i18n/en.yml | 1 + i18n/fr.yml | 1 + lib/Cli.rb | 4 ++-- lib/MPW.rb | 8 ++++++-- mpw | 7 ++++++- mpw-ssh | 18 +++++++++++++----- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/i18n/en.yml b/i18n/en.yml index d0aacf8..a4f94dd 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -28,6 +28,7 @@ en: remove: "Delete an item" group: "Search the items with specified group" add: "Add an item" + config: "Specify the configuration file to use" setup: "Create a new configuration file" protocol: "Select the items with the specified protocol" export: "Export all items in a CSV file" diff --git a/i18n/fr.yml b/i18n/fr.yml index 197ae80..565022f 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -28,6 +28,7 @@ fr: remove: "Supprime un élément" group: "Recherche les éléments appartenant au groupe spécifié" add: "Ajoute un élément" + config: "Spécifie le fichier de configuration à utiliser" setup: "Création d'un nouveau fichier de configuration" protocol: "Sélectionne les éléments ayant le protocole spécifié" export: "Exporte tous les éléments dans un fichier au format CSV" diff --git a/lib/Cli.rb b/lib/Cli.rb index d9048d1..3954c29 100644 --- a/lib/Cli.rb +++ b/lib/Cli.rb @@ -14,8 +14,8 @@ require "#{APP_ROOT}/lib/MPW.rb" class Cli # Constructor - def initialize(lang) - @m = MPW.new() + def initialize(lang, config_file=nil) + @m = MPW.new(config_file) if not @m.checkconfig() self.setup(lang) diff --git a/lib/MPW.rb b/lib/MPW.rb index 51b3620..c38a7c4 100644 --- a/lib/MPW.rb +++ b/lib/MPW.rb @@ -25,9 +25,13 @@ class MPW attr_accessor :timeout_pwd # Constructor - def initialize() + def initialize(file_config=nil) + @error_msg = nil @file_config = "#{Dir.home()}/.mpw.cfg" - @error_msg = nil + + if !file_config.nil? && !file_config.empty? && File.exist?(file_config) + @file_config = file_config + end end # Create a new config file diff --git a/mpw b/mpw index 002c2ea..53a05f7 100755 --- a/mpw +++ b/mpw @@ -25,6 +25,7 @@ options = {} options[:force] = false options[:format] = false options[:group] = nil +options[:config] = nil OptionParser.new do |opts| opts.banner = "#{I18n.t('cli.option.usage')}: mpw [options]" @@ -54,6 +55,10 @@ OptionParser.new do |opts| options[:add] = true end + opts.on('-c', '--config CONFIG', I18n.t('cli.option.config')) do |config| + options[:config] = config + end + opts.on('-S', '--setup', I18n.t('cli.option.setup')) do |b| options[:setup] = true end @@ -85,7 +90,7 @@ OptionParser.new do |opts| end.parse! -cli = Cli.new(lang) +cli = Cli.new(lang, options[:config]) # Display the item's informations if not options[:setup].nil? diff --git a/mpw-ssh b/mpw-ssh index 64f09cf..dbbe4d5 100755 --- a/mpw-ssh +++ b/mpw-ssh @@ -20,22 +20,25 @@ end I18n.load_path = Dir["#{APP_ROOT}/i18n/#{lang}.yml"] I18n.locale = lang.to_sym -cli = CliSSH.new(lang) options = {} OptionParser.new do |opts| opts.banner = "#{I18n.t('ssh.option.usage')}: mpw-ssh SEARCH [options]" opts.on("-l", "--login LOGIN", I18n.t('ssh.option.login')) do |login| - cli.login = login + options[:login] = login end opts.on("-s", "--server SERVER", I18n.t('ssh.option.server')) do |server| - cli.server = server + options[:server] = server end opts.on("-p", "--port PORT", I18n.t('ssh.option.port')) do |port| - cli.port = port + options[:port] = port + end + + opts.on('-c', '--config CONFIG', I18n.t('cli.option.config')) do |config| + options[:config] = config end opts.on("-h", "--help", I18n.t('ssh.option.help')) do |b| @@ -44,7 +47,12 @@ OptionParser.new do |opts| end end.parse! -search = ARGV[0] +cli = CliSSH.new(lang, options[:config]) +cli.login = options[:login] +cli.server = options[:server] +cli.port = options[:port] + +search = ARGV[0] if ARGV.length < 1 puts "#{I18n.t('ssh.option.usage')}: mpw-ssh SEARCH [options]"