Sha256: ef26f21a15750c5a03c4cfb97686591f641cc7e7be480162d685fa4d3911c1e5

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

# encoding: utf-8
require 'rubygems' if RUBY_VERSION < '1.9'
require 'highline'

module Imap::Backup
  module Configuration; end

  class Configuration::Setup
    class << self
      attr_accessor :highline
    end
    self.highline = HighLine.new

    def run
      setup_logging
      loop do
        system('clear')
        self.class.highline.choose do |menu|
          menu.header = 'Choose an action'
          config.data[:accounts].each do |account|
            menu.choice("#{account[:username]}") do
              edit_account account[:username]
            end
          end
          menu.choice('add account') do
            username = Configuration::Asker.email
            edit_account username
          end
          menu.choice('save and exit') do
            config.save
            return
          end
          menu.choice(:quit) do
            return
          end
        end
      end
    end

    private

    def config
      @config ||= Configuration::Store.new
    end

    def setup_logging
      Imap::Backup.logger.level =
        if config.data[:debug]
          ::Logger::Severity::DEBUG
        else
          ::Logger::Severity::ERROR
        end
    end

    def add_account(username)
      account = {
        :username   => username,
        :password   => '',
        :local_path => File.join(config.path, username.gsub('@', '_')),
        :folders    => []
      }
      config.data[:accounts] << account
      account
    end

    def edit_account(username)
      account = config.data[:accounts].find { |a| a[:username] == username }
      if account.nil?
        account = add_account(username)
      end
      Configuration::Account.new(config, account, Configuration::Setup.highline).run
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
imap-backup-1.0.10 lib/imap/backup/configuration/setup.rb