module Magellan::Cli::FileAccess

Constants

DEFAULT_SELECTION_FILENAME

Public Instance Methods

ensure_config_dir() click to toggle source

check if ~/.config/magellan directory exists.

# File lib/magellan/cli/file_access.rb, line 28
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
load_selection(obj) click to toggle source

@param [Class|String] obj Resource class or resource name

# File lib/magellan/cli/file_access.rb, line 39
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
load_selections() click to toggle source
# File lib/magellan/cli/file_access.rb, line 23
def load_selections
  File.readable?(selection_filename) ? YAML.load_file(selection_filename) : {}
end
remove_selection_file() click to toggle source
# File lib/magellan/cli/file_access.rb, line 19
def remove_selection_file
  File.exist?(selection_filename) && File.delete(selection_filename)
end
selection_filename() click to toggle source
# File lib/magellan/cli/file_access.rb, line 15
def selection_filename
  ENV["MAGELLAN_CLI_CONFIG_FILE"] || DEFAULT_SELECTION_FILENAME
end
update_selections(hash = nil) { |sel| ... } click to toggle source
# File lib/magellan/cli/file_access.rb, line 52
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