Sha256: 7deb2b79b34ee3fe74dccee8d01e4634e089e3b60be63e11a857c933be8b6f30

Contents?: true

Size: 1.93 KB

Versions: 36

Compression:

Stored size: 1.93 KB

Contents

# -*- coding: utf-8 -*-

require "fileutils"

module Magellan
  module Cli
    module FileAccess

      class NotSelected < Magellan::Cli::Error
      end

      DEFAULT_SELECTION_FILENAME = File.expand_path("~/.config/magellan/magellan-cli")

      module_function

      def selection_filename
        ENV["MAGELLAN_CLI_CONFIG_FILE"] || DEFAULT_SELECTION_FILENAME
      end

      def remove_selection_file
        File.exist?(selection_filename) && File.delete(selection_filename)
      end

      def load_selections
        File.readable?(selection_filename) ? YAML.load_file(selection_filename) : {}
      end

      # check if ~/.config/magellan directory exists.
      def ensure_config_dir
        return if ENV["MAGELLAN_CLI_CONFIG_FILE"]
        default_dir = File.dirname(DEFAULT_SELECTION_FILENAME)
        unless File.directory?(default_dir)
          # This is notification message to be displayed at the first time magellan-cli invoked.
          puts I18n.t(:config_file, scope: [:base, :notification])
          FileUtils.mkdir_p(default_dir)
        end
      end

      # @param [Class|String] obj Resource class or resource name
      def load_selection(obj)
        if obj.respond_to?(:parameter_name)
          name = obj.parameter_name
          label = obj.name.split(/::/).last.underscore
        else
          name = label = obj
        end
        sel = load_selections
        s = sel[name]
        raise NotSelected, I18n.t(:not_selected, scope: [:file_access, :load_selection], label: label) unless s
        return s
      end

      def update_selections(hash = nil)
        sel = load_selections
        sel.update(hash) if hash
        yield(sel) if block_given?
        filepath = selection_filename
        unless File.exist?(File.dirname(filepath))
          FileUtils.mkdir_p(File.dirname(filepath))
        end
        open(filepath, "w") do |f|
          f.chmod 0600
          YAML.dump(sel, f)
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
magellan-cli-0.11.1 lib/magellan/cli/file_access.rb
magellan-cli-0.11.0 lib/magellan/cli/file_access.rb
magellan-cli-0.10.0 lib/magellan/cli/file_access.rb
magellan-cli-0.9.1 lib/magellan/cli/file_access.rb
magellan-cli-0.9.0 lib/magellan/cli/file_access.rb
magellan-cli-0.8.3 lib/magellan/cli/file_access.rb
magellan-cli-0.8.2 lib/magellan/cli/file_access.rb
magellan-cli-0.8.1 lib/magellan/cli/file_access.rb
magellan-cli-0.8.0 lib/magellan/cli/file_access.rb
magellan-cli-0.7.11 lib/magellan/cli/file_access.rb
magellan-cli-0.7.10 lib/magellan/cli/file_access.rb
magellan-cli-0.7.9 lib/magellan/cli/file_access.rb
magellan-cli-0.7.8 lib/magellan/cli/file_access.rb
magellan-cli-0.7.7 lib/magellan/cli/file_access.rb
magellan-cli-0.7.6 lib/magellan/cli/file_access.rb
magellan-cli-0.7.5 lib/magellan/cli/file_access.rb
magellan-cli-0.7.4 lib/magellan/cli/file_access.rb
magellan-cli-0.7.3 lib/magellan/cli/file_access.rb
magellan-cli-0.7.2 lib/magellan/cli/file_access.rb
magellan-cli-0.7.1 lib/magellan/cli/file_access.rb