From 1f7c177eeaca5dcfa5d210972e3de0d6f8f6e670 Mon Sep 17 00:00:00 2001 From: nishiki Date: Tue, 16 Jul 2013 22:36:41 +0200 Subject: [PATCH] generate a config file --- cli.rb | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mpw | 10 +++----- 2 files changed, 85 insertions(+), 6 deletions(-) create mode 100755 cli.rb diff --git a/cli.rb b/cli.rb new file mode 100755 index 0000000..15ba10a --- /dev/null +++ b/cli.rb @@ -0,0 +1,81 @@ +#!/usr/bin/ruby +# author: nishiki +# mail: nishiki@yaegashi.fr +# info: a simple script who manage your passwords + +require 'rubygems' +require 'highline/import' +require 'yaml' + +class Cli + + attr_accessor :key + attr_accessor :file_gpg + attr_accessor :file_pwd + attr_accessor :timeout_pwd + + def initialize() + @file_config = "#{Dir.home()}/.mpw.cfg" + + if !File.exist?(@file_config) || !self.checkconfig() + self.setup() + if not self.checkconfig() + puts "Error during the checkconfig post setup!" + exit 2 + end + end + end + + # Create a new config file + def setup() + key = ask("Enter the GPG key: ") + file_gpg = ask("Enter the path to encrypt file [default=#{Dir.home()}/.mpw.gpg]: ") + file_pwd = ask("Enter te path to password file [default=#{Dir.home()}/.mpw.pwd]: ") + timeout_pwd = ask("Enter the timeout (in seconde) to GPG password [default=300]: ") + + if not key =~ /[a-zA-Z0-9.-_]*\@[a-zA-Z0-9]*\.[a-zA-Z]*/ + puts "GPG key is invalid!" + exit 2 + end + + if file_gpg.empty? + file_gpg = "#{Dir.home()}/.mpw.gpg" + end + + if file_pwd.empty? + file_pwd = "#{Dir.home()}/.mpw.pwd" + end + + timeout_pwd.empty? ? (timeout_pwd = 300) : (timeout_pwd = timeout_pwd.to_i) + + config = {'config' => {'key' => key, + 'file_gpg' => file_gpg, + 'timeout_pwd' => timeout_pwd, + 'file_pwd' => file_pwd}} + + File.open(@file_config, 'w') do |file| + file << config.to_yaml + end + end + + # Check the config file + # @rtrn: true if the config file is correct + def checkconfig() + begin + config = YAML::load_file(@file_config) + @key = config['config']['key'] + @file_gpg = config['config']['file_gpg'] + @file_pwd = config['config']['file_pwd'] + @timeout_pwd = config['config']['timeout_pwd'].to_i + + if @key.empty? || @file_gpg.empty? || @file_pwd.empty? + return false + end + + rescue + return false + end + + return true + end +end diff --git a/mpw b/mpw index 07e4c7c..f171e01 100755 --- a/mpw +++ b/mpw @@ -6,11 +6,7 @@ require 'rubygems' require 'optparse' require './manage-password.rb' - -FILE_GPG = './pass.gpg' -KEY = 'a.waksberg@yaegashi.fr' -FILE_PWD = '/tmp/.password-manager.pwd' -TIMEOUT_PWD = 300 +require './cli.rb' options = {} OptionParser.new do |opts| @@ -47,7 +43,9 @@ OptionParser.new do |opts| end end.parse! -manage = ManagePasswd.new(KEY, FILE_GPG, FILE_PWD) +cli = Cli.new + +manage = ManagePasswd.new(cli.key, cli.file_gpg, cli.file_pwd) # Display the item's informations if not options[:display].nil?