Class: Flycal::Cli::Hooks
- Inherits:
-
Object
- Object
- Flycal::Cli::Hooks
- Defined in:
- lib/flycal/cli/hooks.rb
Overview
Post-install / post-login onboarding.
Class Method Summary collapse
- .accept_default_no?(input = $stdin) ⇒ Boolean
- .accept_default_yes?(input = $stdin) ⇒ Boolean
- .after_install ⇒ Object
- .after_login ⇒ Object
- .after_uninstall ⇒ Object
- .announce_default_calendar(name) ⇒ Object
- .bold(text) ⇒ Object
- .display_name(calendar) ⇒ Object
- .interactive? ⇒ Boolean
- .last_flycal_cli_install? ⇒ Boolean
- .launch_login ⇒ Object
- .skip_cli_after_install?(argv) ⇒ Boolean
- .skip_post_install? ⇒ Boolean
- .skip_post_uninstall? ⇒ Boolean
- .uninstall_cleanup_needed? ⇒ Boolean
-
.uninstall_text(key, vars = {}) ⇒ Object
Avoid Cli::Locale (it reads Config and would recreate config.yml after the backup).
Class Method Details
.accept_default_no?(input = $stdin) ⇒ Boolean
117 118 119 120 121 122 123 124 125 |
# File 'lib/flycal/cli/hooks.rb', line 117 def accept_default_no?(input = $stdin) line = input.gets return false if line.nil? answer = line.strip.downcase return false if answer.empty? %w[y yes].include?(answer) end |
.accept_default_yes?(input = $stdin) ⇒ Boolean
107 108 109 110 111 112 113 114 115 |
# File 'lib/flycal/cli/hooks.rb', line 107 def accept_default_yes?(input = $stdin) line = input.gets return true if line.nil? answer = line.strip.downcase return true if answer.empty? %w[y yes].include?(answer) end |
.after_install ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/flycal/cli/hooks.rb', line 8 def after_install return if skip_post_install? return unless interactive? return if Auth.logged_in? puts "#{bold('flycal-cli')} #{Locale.t('hooks.after_install.ready')}" print "#{Locale.t('hooks.after_install.prompt')} " launch_login if accept_default_yes? end |
.after_login ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/flycal/cli/hooks.rb', line 34 def after_login creds = Auth.credentials return unless creds selected = nil begin calendars = Flycal.connect(credentials: creds).list_calendars current = Config.calendar_default.to_s.strip if current.empty? selected = calendars.find { |calendar| calendar.respond_to?(:primary) && calendar.primary } || calendars.first Config.calendar_default = selected.id if selected else selected = calendars.find { |calendar| calendar.id.to_s == current } || calendars.find { |calendar| calendar.summary.to_s == current } end rescue StandardError => e warn "flycal-cli after_login: #{e.}" end name = display_name(selected) || Config.calendar_default return if name.to_s.strip.empty? announce_default_calendar(name) end |
.after_uninstall ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/flycal/cli/hooks.rb', line 18 def after_uninstall return if skip_post_uninstall? return unless interactive? return unless last_flycal_cli_install? return unless uninstall_cleanup_needed? print "#{uninstall_text('hooks.after_uninstall.prompt')} " return unless accept_default_no? backed_up = Config.backup_and_remove_config! Config.remove_tokens! puts uninstall_text("hooks.after_uninstall.saved", { backup: Config.config_backup_file }) if backed_up puts uninstall_text("hooks.after_uninstall.tokens_removed") end |
.announce_default_calendar(name) ⇒ Object
133 134 135 136 137 |
# File 'lib/flycal/cli/hooks.rb', line 133 def announce_default_calendar(name) puts "#{bold('flycal-cli')} #{Locale.t('hooks.after_login.using', { calendar: bold(name) })}" puts Locale.t("hooks.after_login.change") puts Locale.t("hooks.after_login.goodbye") end |
.bold(text) ⇒ Object
127 128 129 130 131 |
# File 'lib/flycal/cli/hooks.rb', line 127 def bold(text) return text.to_s unless $stdout.tty? "\e[1m#{text}\e[0m" end |
.display_name(calendar) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/flycal/cli/hooks.rb', line 139 def display_name(calendar) return nil unless calendar name = calendar.summary.to_s.strip name.empty? ? calendar.id.to_s : name end |
.interactive? ⇒ Boolean
103 104 105 |
# File 'lib/flycal/cli/hooks.rb', line 103 def interactive? $stdin.tty? && $stdout.tty? end |
.last_flycal_cli_install? ⇒ Boolean
76 77 78 79 80 |
# File 'lib/flycal/cli/hooks.rb', line 76 def last_flycal_cli_install? Gem::Specification.find_all_by_name("flycal-cli").size <= 1 rescue StandardError true end |
.launch_login ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/flycal/cli/hooks.rb', line 59 def launch_login bin = begin Gem.bin_path("flycal-cli", "flycal") rescue Gem::Exception "flycal" end system(bin, "login") end |
.skip_cli_after_install?(argv) ⇒ Boolean
95 96 97 98 99 100 101 |
# File 'lib/flycal/cli/hooks.rb', line 95 def skip_cli_after_install?(argv) argv = Array(argv) return true if argv.intersect?(%w[--version -v --help -h]) command = argv.find { |arg| !arg.start_with?("-") } %w[login help].include?(command.to_s) end |
.skip_post_install? ⇒ Boolean
68 69 70 |
# File 'lib/flycal/cli/hooks.rb', line 68 def skip_post_install? ENV["FLYCAL_SKIP_POST_INSTALL"].to_s == "1" end |
.skip_post_uninstall? ⇒ Boolean
72 73 74 |
# File 'lib/flycal/cli/hooks.rb', line 72 def skip_post_uninstall? ENV["FLYCAL_SKIP_POST_UNINSTALL"].to_s == "1" end |
.uninstall_cleanup_needed? ⇒ Boolean
82 83 84 |
# File 'lib/flycal/cli/hooks.rb', line 82 def uninstall_cleanup_needed? File.file?(Config.config_file) || File.file?(Config.tokens_path) end |
.uninstall_text(key, vars = {}) ⇒ Object
Avoid Cli::Locale (it reads Config and would recreate config.yml after the backup).
87 88 89 90 91 92 93 |
# File 'lib/flycal/cli/hooks.rb', line 87 def uninstall_text(key, vars = {}) previous = Thread.current[:flycal_locale_default_provider] Thread.current[:flycal_locale_default_provider] = nil Flycal::Locale.t(key, vars) ensure Thread.current[:flycal_locale_default_provider] = previous end |