Class: Cli
- Inherits:
-
Object
- Object
- Cli
- Defined in:
- lib/mpw/ui/cli.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#add ⇒ Object
Form to add a new item.
-
#decrypt ⇒ Object
Request the GPG password and decrypt the file.
-
#delete(id, force = false) ⇒ Object
Remove an item @args: id -> the item’s id force -> no resquest a validation.
-
#display(options = {}) ⇒ Object
Display the query’s result @args: search -> the string to search protocol -> search from a particular protocol.
-
#display_item(item) ⇒ Object
Display an item in the default format @args: item -> an array with the item information.
-
#export(file, type = :yaml) ⇒ Object
Export the items in a CSV file @args: file -> the destination file.
-
#import(file, type = :yaml, force = false) ⇒ Object
Import items from a CSV file @args: file -> the import file force -> no resquest a validation.
-
#initialize(config) ⇒ Cli
constructor
Constructor @args: lang -> the operating system language config_file -> a specify config file.
-
#setup(lang) ⇒ Object
Create a new config file @args: lang -> the software language.
-
#setup_gpg_key ⇒ Object
Setup a new GPG key.
-
#sync ⇒ Object
Sync the data with the server @rtnr: true if the synchro is finish.
-
#update(id) ⇒ Object
Update an item @args: id -> the item’s id.
Constructor Details
#initialize(config) ⇒ Cli
Constructor @args: lang -> the operating system language
config_file -> a specify config file
21 22 23 |
# File 'lib/mpw/ui/cli.rb', line 21 def initialize(config) @config = config end |
Instance Method Details
#add ⇒ Object
Form to add a new item
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/mpw/ui/cli.rb', line 189 def add = {} puts I18n.t('form.add.title') puts '--------------------' [:name] = ask(I18n.t('form.add.name')).to_s [:group] = ask(I18n.t('form.add.group')).to_s [:host] = ask(I18n.t('form.add.server')).to_s [:protocol] = ask(I18n.t('form.add.protocol')).to_s [:user] = ask(I18n.t('form.add.login')).to_s [:password] = ask(I18n.t('form.add.password')).to_s [:port] = ask(I18n.t('form.add.port')).to_s [:comment] = ask(I18n.t('form.add.comment')).to_s item = MPW::Item.new() if @mpw.add(item) if @mpw.encrypt sync puts "#{I18n.t('form.add.valid')}".green else puts "#{I18n.t('display.error')} #12: #{@mpw.error_msg}".red end else puts "#{I18n.t('display.error')} #13: #{item.error_msg}".red end end |
#decrypt ⇒ Object
Request the GPG password and decrypt the file
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/mpw/ui/cli.rb', line 121 def decrypt if not defined?(@mpw) @mpw = MPW::MPW.new(@config.file_gpg, @config.key, @config.share_keys) end @password = ask(I18n.t('display.gpg_password')) {|q| q.echo = false} if not @mpw.decrypt(@password) puts "#{I18n.t('display.error')} #11: #{@mpw.error_msg}".red exit 2 end end |
#delete(id, force = false) ⇒ Object
Remove an item @args: id -> the item’s id
force -> no resquest a validation
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/mpw/ui/cli.rb', line 255 def delete(id, force=false) if not force item = @mpw.search_by_id(id) if not item.nil? display_item(item) confirm = ask("#{I18n.t('form.delete.ask', id: id)} (y/N) ").to_s if confirm =~ /^(y|yes|YES|Yes|Y)$/ force = true end else puts I18n.t('display.nothing') end end if force item.delete if @mpw.encrypt sync puts "#{I18n.t('form.delete.valid', id: id)}".green else puts "#{I18n.t('display.error')} #16: #{@mpw.error_msg}".red end end end |
#display(options = {}) ⇒ Object
Display the query’s result @args: search -> the string to search
protocol -> search from a particular protocol
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/mpw/ui/cli.rb', line 136 def display(={}) result = @mpw.list() case result.length when 0 puts I18n.t('display.nothing') when 1 display_item(result.first) else i = 1 result.each do |item| print "#{i}: ".cyan print item.name print " -> #{item.comment}".magenta if not item.comment.to_s.empty? print "\n" i += 1 end choice = ask(I18n.t('form.select')).to_i if choice >= 1 and choice < i display_item(result[choice-1]) else puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow end end end |
#display_item(item) ⇒ Object
Display an item in the default format @args: item -> an array with the item information
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mpw/ui/cli.rb', line 166 def display_item(item) puts '--------------------'.cyan print 'Id: '.cyan puts item.id print "#{I18n.t('display.name')}: ".cyan puts item.name print "#{I18n.t('display.group')}: ".cyan puts item.group print "#{I18n.t('display.server')}: ".cyan puts item.host print "#{I18n.t('display.protocol')}: ".cyan puts item.protocol print "#{I18n.t('display.login')}: ".cyan puts item.user print "#{I18n.t('display.password')}: ".cyan puts item.password print "#{I18n.t('display.port')}: ".cyan puts item.port print "#{I18n.t('display.comment')}: ".cyan puts item.comment end |
#export(file, type = :yaml) ⇒ Object
Export the items in a CSV file @args: file -> the destination file
285 286 287 288 289 290 291 |
# File 'lib/mpw/ui/cli.rb', line 285 def export(file, type=:yaml) if @mpw.export(file, type) puts "#{I18n.t('export.valid', file)}".green else puts "#{I18n.t('display.error')} #17: #{@mpw.error_msg}".red end end |
#import(file, type = :yaml, force = false) ⇒ Object
Import items from a CSV file @args: file -> the import file
force -> no resquest a validation
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/mpw/ui/cli.rb', line 296 def import(file, type=:yaml, force=false) if not force result = @mpw.import_preview(file, type) if result.is_a?(Array) and not result.empty? result.each do |r| display_item(r) end confirm = ask("#{I18n.t('form.import.ask', file: file)} (y/N) ").to_s if confirm =~ /^(y|yes|YES|Yes|Y)$/ force = true end else puts I18n.t('form.import.not_valid') end end if force if @mpw.import(file, type) and @mpw.encrypt sync puts "#{I18n.t('form.import.valid')}".green else puts "#{I18n.t('display.error')} #18: #{@mpw.error_msg}".red end end end |
#setup(lang) ⇒ Object
Create a new config file @args: lang -> the software language
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 |
# File 'lib/mpw/ui/cli.rb', line 41 def setup(lang) puts I18n.t('form.setup.title') puts '--------------------' language = ask(I18n.t('form.setup.lang', lang: lang)).to_s key = ask(I18n.t('form.setup.gpg_key')).to_s share_keys = ask(I18n.t('form.setup.share_gpg_keys')).to_s file_gpg = ask(I18n.t('form.setup.gpg_file', home: @conf.dir_config)).to_s sync_type = ask(I18n.t('form.setup.sync_type')).to_s if ['ssh', 'ftp', 'mpw'].include?(sync_type) sync_host = ask(I18n.t('form.setup.sync_host')).to_s sync_port = ask(I18n.t('form.setup.sync_port')).to_s sync_user = ask(I18n.t('form.setup.sync_user')).to_s sync_pwd = ask(I18n.t('form.setup.sync_pwd')).to_s sync_path = ask(I18n.t('form.setup.sync_path')).to_s end if language.nil? or language.empty? language = lang end I18n.locale = language.to_sym sync_type = sync_type.nil? or sync_type.empty? ? nil : sync_type sync_host = sync_host.nil? or sync_host.empty? ? nil : sync_host sync_port = sync_port.nil? or sync_port.empty? ? nil : sync_port.to_i sync_user = sync_user.nil? or sync_user.empty? ? nil : sync_user sync_pwd = sync_pwd.nil? or sync_pwd.empty? ? nil : sync_pwd sync_path = sync_path.nil? or sync_path.empty? ? nil : sync_path if @config.setup(key, share_keys, language, file_gpg, sync_type, sync_host, sync_port, sync_user, sync_pwd, sync_path) puts "#{I18n.t('form.setup.valid')}".green else puts "#{I18n.t('display.error')} #8: #{@config.error_msg}".red exit 2 end if not @config.checkconfig puts "#{I18n.t('display.error')} #9: #{@config.error_msg}".red exit 2 end end |
#setup_gpg_key ⇒ Object
Setup a new GPG key
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/mpw/ui/cli.rb', line 84 def setup_gpg_key puts I18n.t('form.setup_gpg_key.title') puts '--------------------' ask = ask(I18n.t('form.setup_gpg_key.ask')).to_s if not ['Y', 'y', 'O', 'o'].include?(ask) puts I18n.t('form.setup_gpg_key.no_create') exit 2 end name = ask(I18n.t('form.setup_gpg_key.name')).to_s password = ask(I18n.t('form.setup_gpg_key.password')) {|q| q.echo = false} confirm = ask(I18n.t('form.setup_gpg_key.confirm_password')) {|q| q.echo = false} if password != confirm puts I18n.t('form.setup_gpg_key.error_password') exit 2 end length = ask(I18n.t('form.setup_gpg_key.length')).to_s expire = ask(I18n.t('form.setup_gpg_key.expire')).to_s password = password.to_s length = length.nil? or length.empty? ? 2048 : length.to_i expire = expire.nil? or expire.empty? ? 0 : expire.to_i puts I18n.t('form.setup_gpg_key.wait') if @config.setup_gpg_key(password, name, length, expire) puts "#{I18n.t('form.setup_gpg_key.valid')}".green else puts "#{I18n.t('display.error')} #10: #{@config.error_msg}".red exit 2 end end |
#sync ⇒ Object
Sync the data with the server @rtnr: true if the synchro is finish
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mpw/ui/cli.rb', line 27 def sync @sync = MPW::Sync.new(@config, @mpw, @password) raise(@sync.error_msg) if not @sync.get_remote raise(@sync.error_msg) if not @sync.sync return true rescue Exception => e puts "#{I18n.t('display.error')} #7: #{e}".red return false end |
#update(id) ⇒ Object
Update an item @args: id -> the item’s id
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/mpw/ui/cli.rb', line 218 def update(id) item = @mpw.search_by_id(id) if not item.nil? = {} puts I18n.t('form.update.title') puts '--------------------' [:name] = ask(I18n.t('form.update.name' , name: item.name)).to_s [:group] = ask(I18n.t('form.update.group' , group: item.group)).to_s [:host] = ask(I18n.t('form.update.server' , server: item.host)).to_s [:protocol] = ask(I18n.t('form.update.protocol', protocol: item.protocol)).to_s [:user] = ask(I18n.t('form.update.login' , login: item.user)).to_s [:password] = ask(I18n.t('form.update.password')).to_s [:port] = ask(I18n.t('form.update.port' , port: item.port)).to_s [:comment] = ask(I18n.t('form.update.comment' , comment: item.comment)).to_s .delete_if { |k,v| v.empty? } if item.update() if @mpw.encrypt sync puts "#{I18n.t('form.update.valid')}".green else puts "#{I18n.t('display.error')} #14: #{@mpw.error_msg}".red end else puts "#{I18n.t('display.error')} #15: #{item.error_msg}".red end else puts I18n.t('display.nothing') end end |