Class: MPW::Config
- Inherits:
-
Object
- Object
- MPW::Config
- Defined in:
- lib/mpw/config.rb
Instance Attribute Summary collapse
-
#config_dir ⇒ Object
Returns the value of attribute config_dir.
-
#default_wallet ⇒ Object
Returns the value of attribute default_wallet.
-
#error_msg ⇒ Object
Returns the value of attribute error_msg.
-
#gpg_exe ⇒ Object
Returns the value of attribute gpg_exe.
-
#gpg_key ⇒ Object
Returns the value of attribute gpg_key.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#password ⇒ Object
Returns the value of attribute password.
-
#wallet_dir ⇒ Object
Returns the value of attribute wallet_dir.
Instance Method Summary collapse
-
#check_gpg_key? ⇒ Boolean
Check if private key exist @rtrn: true if the key exist, else false.
-
#initialize(config_file = nil) ⇒ Config
constructor
Constructor @args: config_file -> the specify config file.
-
#load_config ⇒ Object
Load the config file.
-
#setup(options) ⇒ Object
Create a new config file @args: options -> hash with values @rtrn: true if le config file is create.
-
#setup_gpg_key(password, name, length = 4096, expire = 0) ⇒ Object
Setup a new gpg key @args: password -> the GPG key password name -> the name of user length -> length of the GPG key expire -> the time of expire to GPG key @rtrn: true if the GPG key is create, else false.
Constructor Details
#initialize(config_file = nil) ⇒ Config
Constructor @args: config_file -> the specify config file
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mpw/config.rb', line 39 def initialize(config_file=nil) @config_file = config_file if /darwin/ =~ RUBY_PLATFORM @config_dir = "#{Dir.home}/Library/Preferences/mpw" elsif /cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM @config_dir = "#{Dir.home}/AppData/Local/mpw" else @config_dir = "#{Dir.home}/.config/mpw" end @config_file = "#{@config_dir}/mpw.cfg" if @config_file.nil? or @config_file.empty? end |
Instance Attribute Details
#config_dir ⇒ Object
Returns the value of attribute config_dir.
31 32 33 |
# File 'lib/mpw/config.rb', line 31 def config_dir @config_dir end |
#default_wallet ⇒ Object
Returns the value of attribute default_wallet.
32 33 34 |
# File 'lib/mpw/config.rb', line 32 def default_wallet @default_wallet end |
#error_msg ⇒ Object
Returns the value of attribute error_msg.
27 28 29 |
# File 'lib/mpw/config.rb', line 27 def error_msg @error_msg end |
#gpg_exe ⇒ Object
Returns the value of attribute gpg_exe.
34 35 36 |
# File 'lib/mpw/config.rb', line 34 def gpg_exe @gpg_exe end |
#gpg_key ⇒ Object
Returns the value of attribute gpg_key.
29 30 31 |
# File 'lib/mpw/config.rb', line 29 def gpg_key @gpg_key end |
#lang ⇒ Object
Returns the value of attribute lang.
30 31 32 |
# File 'lib/mpw/config.rb', line 30 def lang @lang end |
#password ⇒ Object
Returns the value of attribute password.
35 36 37 |
# File 'lib/mpw/config.rb', line 35 def password @password end |
#wallet_dir ⇒ Object
Returns the value of attribute wallet_dir.
33 34 35 |
# File 'lib/mpw/config.rb', line 33 def wallet_dir @wallet_dir end |
Instance Method Details
#check_gpg_key? ⇒ Boolean
Check if private key exist @rtrn: true if the key exist, else false
150 151 152 153 154 155 156 157 |
# File 'lib/mpw/config.rb', line 150 def check_gpg_key? ctx = GPGME::Ctx.new ctx.each_key(@gpg_key, true) do return true end return false end |
#load_config ⇒ Object
Load the config file
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mpw/config.rb', line 132 def load_config config = YAML::load_file(@config_file) @gpg_key = config['gpg_key'] @lang = config['lang'] @wallet_dir = config['wallet_dir'] @default_wallet = config['default_wallet'] @gpg_exe = config['gpg_exe'] @password = config['password'] || {} raise if @gpg_key.empty? or @wallet_dir.empty? I18n.locale = @lang.to_sym rescue Exception => e raise "#{I18n.t('error.config.load')}\n#{e}" end |
#setup(options) ⇒ Object
Create a new config file @args: options -> hash with values @rtrn: true if le config file is create
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mpw/config.rb', line 56 def setup() gpg_key = [:gpg_key] || @gpg_key lang = [:lang] || @lang wallet_dir = [:wallet_dir] || @wallet_dir default_wallet = [:default_wallet] || @default_wallet gpg_exe = [:gpg_exe] || @gpg_exe password = { numeric: true, alpha: true, special: false, length: 16, } ['numeric', 'special', 'alpha', 'length'].each do |k| if .has_key?("pwd_#{k}".to_sym) password[k.to_sym] = ["pwd_#{k}".to_sym] elsif not @password.nil? and @password.has_key?(k.to_sym) password[k.to_sym] = @password[k.to_sym] end end if not gpg_key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ raise I18n.t('error.config.key_bad_format') end wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty? config = { 'gpg_key' => gpg_key, 'lang' => lang, 'wallet_dir' => wallet_dir, 'default_wallet' => default_wallet, 'gpg_exe' => gpg_exe, 'password' => password, } FileUtils.mkdir_p(@config_dir, mode: 0700) FileUtils.mkdir_p(wallet_dir, mode: 0700) File.open(@config_file, 'w') do |file| file << config.to_yaml end rescue Exception => e raise "#{I18n.t('error.config.write')}\n#{e}" end |
#setup_gpg_key(password, name, length = 4096, expire = 0) ⇒ Object
Setup a new gpg key @args: password -> the GPG key password
name -> the name of user
length -> length of the GPG key
expire -> the time of expire to GPG key
@rtrn: true if the GPG key is create, else false
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/mpw/config.rb', line 105 def setup_gpg_key(password, name, length = 4096, expire = 0) if name.to_s.empty? raise "#{I18n.t('error.config.genkey_gpg.name')}" elsif password.to_s.empty? raise "#{I18n.t('error.config.genkey_gpg.password')}" end param = '' param << '<GnupgKeyParms format="internal">' + "\n" param << "Key-Type: RSA\n" param << "Key-Length: #{length}\n" param << "Subkey-Type: ELG-E\n" param << "Subkey-Length: #{length}\n" param << "Name-Real: #{name}\n" param << "Name-Comment: #{name}\n" param << "Name-Email: #{@gpg_key}\n" param << "Expire-Date: #{expire}\n" param << "Passphrase: #{password}\n" param << "</GnupgKeyParms>\n" ctx = GPGME::Ctx.new ctx.genkey(param, nil, nil) rescue Exception => e raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" end |