Module: Magellan::Cli::FileAccess
- Included in:
- Command, Http, Messaging::Base, Resources::Base
- Defined in:
- lib/magellan/cli/file_access.rb
Defined Under Namespace
Classes: NotSelected
Constant Summary collapse
- DEFAULT_SELECTION_FILENAME =
File.("~/.config/magellan/magellan-cli")
Class Method Summary collapse
-
.ensure_config_dir ⇒ Object
check if ~/.config/magellan directory exists.
- .load_selection(obj) ⇒ Object
- .load_selections ⇒ Object
- .remove_selection_file ⇒ Object
- .selection_filename ⇒ Object
- .update_selections(hash = nil) {|sel| ... } ⇒ Object
Class Method Details
.ensure_config_dir ⇒ Object
check if ~/.config/magellan directory exists.
29 30 31 32 33 34 35 36 37 |
# File 'lib/magellan/cli/file_access.rb', line 29 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) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/magellan/cli/file_access.rb', line 40 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 ⇒ Object
24 25 26 |
# File 'lib/magellan/cli/file_access.rb', line 24 def load_selections File.readable?(selection_filename) ? YAML.load_file(selection_filename) : {} end |
.remove_selection_file ⇒ Object
20 21 22 |
# File 'lib/magellan/cli/file_access.rb', line 20 def remove_selection_file File.exist?(selection_filename) && File.delete(selection_filename) end |
.selection_filename ⇒ Object
16 17 18 |
# File 'lib/magellan/cli/file_access.rb', line 16 def selection_filename ENV["MAGELLAN_CLI_CONFIG_FILE"] || DEFAULT_SELECTION_FILENAME end |
.update_selections(hash = nil) {|sel| ... } ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/magellan/cli/file_access.rb', line 53 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 |