Module: EasyCreds::Views::SettingsMenu
- Defined in:
- lib/easy_creds/views/settings_menu.rb
Class Method Summary collapse
- .prompt_template(prompt) ⇒ Object
- .prompt_vault(prompt) ⇒ Object
- .prune_projects(prompt) ⇒ Object
- .run(prompt) ⇒ Object
- .run_op_test ⇒ Object
- .show_paths(cfg) ⇒ Object
- .update_setting(attr, value) ⇒ Object
Class Method Details
.prompt_template(prompt) ⇒ Object
63 64 65 66 67 |
# File 'lib/easy_creds/views/settings_menu.rb', line 63 def self.prompt_template(prompt) registry = Templates::Registry.new(global_dir: EasyCreds.config.global_dir) names = registry.list.map(&:to_s) prompt.select('Select default template:', names, cycle: true)&.to_sym end |
.prompt_vault(prompt) ⇒ Object
59 60 61 |
# File 'lib/easy_creds/views/settings_menu.rb', line 59 def self.prompt_vault(prompt) VaultPicker.pick(prompt) end |
.prune_projects(prompt) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/easy_creds/views/settings_menu.rb', line 94 def self.prune_projects(prompt) registry = Projects::Registry.load gone = registry.prune_missing! if gone.empty? Theme.notice('No missing projects found.') else gone.each { |pr| Theme.success("Removed #{pr.name} (#{pr.path})") } end end |
.run(prompt) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/easy_creds/views/settings_menu.rb', line 8 def self.run(prompt) loop do cfg = EasyCreds.config p = Theme.pastel choice = prompt.select( "\nGlobal settings:", [ { name: " #{p.bold('default vault')} #{p.dim('─')} #{cfg.default_vault || p.dim('(not set)')}", value: :vault }, { name: " #{p.bold('default template')} #{p.dim('─')} #{cfg.default_template}", value: :template }, { name: " #{p.bold('default provider')} #{p.dim('─')} #{cfg.default_provider}", value: :provider }, { name: " #{p.bold('editor')} #{p.dim('─')} #{cfg.editor || p.dim('($VISUAL/$EDITOR)')}", value: :editor }, { name: " #{p.bold('test 1Password')} #{p.dim('─')} verify op CLI sign-in", value: :op_test }, { name: " #{p.bold('show paths')} #{p.dim('─')} display config / template / project locations", value: :paths }, { name: " #{p.bold('prune missing')} #{p.dim('─')} remove registry entries for deleted paths", value: :prune }, { name: " #{p.dim('back')}", value: :back } ], cycle: true, per_page: 10 ) case choice when :back then return when :vault then update_setting(:default_vault, prompt_vault(prompt)) when :template then update_setting(:default_template, prompt_template(prompt)) when :provider then update_setting(:default_provider, prompt.select('Provider:', [{ name: '1Password', value: :onepassword }])) when :editor then update_setting(:editor, prompt.ask('Editor command:', default: cfg.editor)) when :op_test then run_op_test when :paths then show_paths(cfg) when :prune then prune_projects(prompt) end end end |
.run_op_test ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/easy_creds/views/settings_menu.rb', line 69 def self.run_op_test if system('op whoami > /dev/null 2>&1') email = `op whoami --format=json 2>/dev/null`.then do |raw| JSON.parse(raw)['email'] rescue StandardError '(unknown)' end Theme.success("Signed in to 1Password as #{email}") else Theme.failure('Not signed in to 1Password. Run: op signin') end end |
.show_paths(cfg) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/easy_creds/views/settings_menu.rb', line 82 def self.show_paths(cfg) p = Theme.pastel puts '' puts " #{p.dim('global dir')} #{cfg.global_dir}" puts " #{p.dim('config.yml')} #{File.join(cfg.global_dir, 'config.yml')}" puts " #{p.dim('templates')} #{cfg.resolved_templates_dir}" puts " #{p.dim('projects.yml')} #{Projects::Registry.registry_path}" puts '' puts ' Press enter to continue...' $stdin.gets end |
.update_setting(attr, value) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/easy_creds/views/settings_menu.rb', line 50 def self.update_setting(attr, value) return unless value EasyCreds.configure { |c| c.public_send(:"#{attr}=", value) } path = File.join(EasyCreds.config.global_dir, 'config.yml') Configuration.write_file!(EasyCreds.config, path) Theme.success("#{attr} → #{value}") end |