Sha256: bcb8228e0a965e3b333c55a46ab8af6369c9e59adadec94348b55533445df220

Contents?: true

Size: 1.72 KB

Versions: 15

Compression:

Stored size: 1.72 KB

Contents

require "json"

module Imap::Backup
  module Configuration; end

  class Configuration::Store
    CONFIGURATION_DIRECTORY = File.expand_path("~/.imap-backup")

    attr_reader :pathname

    def self.default_pathname
      File.join(CONFIGURATION_DIRECTORY, "config.json")
    end

    def self.exist?(pathname = default_pathname)
      File.exist?(pathname)
    end

    def initialize(pathname = self.class.default_pathname)
      @pathname = pathname
    end

    def path
      File.dirname(pathname)
    end

    def save
      mkdir_private path
      remove_modified_flags
      remove_deleted_accounts
      File.open(pathname, "w") { |f| f.write(JSON.pretty_generate(data)) }
      FileUtils.chmod 0o600, pathname
    end

    def accounts
      data[:accounts]
    end

    def modified?
      accounts.any? { |a| a[:modified] || a[:delete] }
    end

    def debug?
      data[:debug]
    end

    def debug=(value)
      data[:debug] = [true, false].include?(value) ? value : false
    end

    private

    def data
      @data ||=
        begin
          if File.exist?(pathname)
            Utils.check_permissions pathname, 0o600
            contents = File.read(pathname)
            data = JSON.parse(contents, symbolize_names: true)
          else
            data = {accounts: []}
          end
          data[:debug] = data.key?(:debug) ? data[:debug] == true : false
          data
        end
    end

    def remove_modified_flags
      accounts.each { |a| a.delete(:modified) }
    end

    def remove_deleted_accounts
      accounts.reject! { |a| a[:delete] }
    end

    def mkdir_private(path)
      FileUtils.mkdir(path) if !File.directory?(path)
      FileUtils.chmod(0o700, path) if Utils.mode(path) != 0o700
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
imap-backup-4.0.2 lib/imap/backup/configuration/store.rb
imap-backup-4.0.1 lib/imap/backup/configuration/store.rb
imap-backup-4.0.0 lib/imap/backup/configuration/store.rb
imap-backup-4.0.0.rc6 lib/imap/backup/configuration/store.rb
imap-backup-4.0.0.rc5 lib/imap/backup/configuration/store.rb
imap-backup-4.0.0.rc4 lib/imap/backup/configuration/store.rb
imap-backup-4.0.0.rc3 lib/imap/backup/configuration/store.rb
imap-backup-4.0.0.rc2 lib/imap/backup/configuration/store.rb
imap-backup-4.0.0.rc1 lib/imap/backup/configuration/store.rb
imap-backup-3.4.1 lib/imap/backup/configuration/store.rb
imap-backup-3.4.0 lib/imap/backup/configuration/store.rb
imap-backup-3.3.1 lib/imap/backup/configuration/store.rb
imap-backup-3.3.0 lib/imap/backup/configuration/store.rb
imap-backup-3.2.1 lib/imap/backup/configuration/store.rb
imap-backup-3.2.0 lib/imap/backup/configuration/store.rb