flycal-cli 1.4 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7f3ae70fd1c2b1259a400d7bea2ff5b936765ea3c4031e35d11ab59e755bd98
4
- data.tar.gz: e9decf6f9520e826f010e7c5dd8f9fe4f2cd5739c079b0ffa133fdebcd721cf4
3
+ metadata.gz: 406ca19950c40ebb2ab3d0215d16c97154c6d322d90a3e7dce39440b8c272601
4
+ data.tar.gz: 79734bbb5b9a884517be0095351ee6cb43fd52af62d8022592860d884de302fc
5
5
  SHA512:
6
- metadata.gz: 13cc3c1b5c9aaa8e4b630920e6752a61a0bdd90de2a6d302473bbe75c548566b6ab5438f5c9d86180c4a11d424a902069aa3f8e1b6e0f948e0bacb1292a495ac
7
- data.tar.gz: 3d0a56c6403f673b7b8fcf4e06797970e936b3448e2496a22ee2072dc07efed566ece48ad98e42132371f8bae6b8d2143a6915ec281b9e9d0fe0f647db47e7ea
6
+ metadata.gz: ead67f0591807b59784123b7943c6a27975b6dec986327a4888cae463f0929c7c1b770b807ebef18c887f5499eec9b0dcc662e4d64362a8f17211db45ac549b2
7
+ data.tar.gz: 2023d313314d76377b9e287f59f5ff0170864d1b8bf5726d3eed08d594f36477cb08016624b046d8d8153bf47408a3e109c767fd4f5d15532c712517cf5e690c
@@ -30,7 +30,6 @@ module Cli
30
30
  if Auth.logged_in?
31
31
  puts Locale.t("login.already_connected")
32
32
  Hooks.after_login
33
- puts "\n#{Locale.t('login.next_config')}" unless Config.calendar_default
34
33
  return
35
34
  end
36
35
 
@@ -41,7 +40,6 @@ module Cli
41
40
  Auth.login(mode)
42
41
  puts "\n#{Locale.t('login.success')}"
43
42
  Hooks.after_login
44
- puts "\n#{Locale.t('login.next_config')}" unless Config.calendar_default
45
43
  rescue Flycal::Error => e
46
44
  puts "Error: #{e.message}"
47
45
  exit 1
@@ -28,6 +28,8 @@ module Cli
28
28
  end
29
29
 
30
30
  def logged_in?
31
+ return false unless tokens_present?
32
+
31
33
  creds = credentials
32
34
  return false unless creds
33
35
 
@@ -37,6 +39,12 @@ module Cli
37
39
  false
38
40
  end
39
41
 
42
+ # Google session file. config.yml is not a login signal (it survives uninstall).
43
+ def tokens_present?
44
+ path = Config.tokens_path
45
+ File.file?(path) && !File.size(path).to_i.zero?
46
+ end
47
+
40
48
  def login(mode)
41
49
  mode = normalize_mode(mode)
42
50
  if Config.auth_client && Config.auth_client != mode && File.exist?(Config.tokens_path)
@@ -8,6 +8,7 @@ module Cli
8
8
  class Config
9
9
  CONFIG_DIR = File.expand_path("~/.flycal")
10
10
  CONFIG_FILE = File.join(CONFIG_DIR, "config.yml")
11
+ CONFIG_BACKUP_FILE = File.join(CONFIG_DIR, "config.backup.yml")
11
12
  CREDENTIALS_FILE = File.join(CONFIG_DIR, "credentials.json")
12
13
  TOKENS_FILE = File.join(CONFIG_DIR, "tokens.yml")
13
14
  DEFAULTS_FILE = File.expand_path("../../../config/defaults.yml", __dir__)
@@ -45,16 +46,6 @@ module Cli
45
46
  save(data)
46
47
  end
47
48
 
48
- def after_install_done?
49
- load["after_install_done"] == true
50
- end
51
-
52
- def after_install_done=(value)
53
- data = load
54
- data["after_install_done"] = value ? true : false
55
- save(data)
56
- end
57
-
58
49
  def credentials_path
59
50
  CREDENTIALS_FILE
60
51
  end
@@ -172,6 +163,28 @@ module Cli
172
163
  CONFIG_FILE
173
164
  end
174
165
 
166
+ def config_backup_file
167
+ CONFIG_BACKUP_FILE
168
+ end
169
+
170
+ def backup_and_remove_config!
171
+ source = config_file
172
+ return false unless File.file?(source)
173
+
174
+ FileUtils.mkdir_p(config_dir)
175
+ FileUtils.mv(source, config_backup_file, force: true)
176
+ FileUtils.rm_f(source)
177
+ true
178
+ end
179
+
180
+ def remove_tokens!
181
+ path = tokens_path
182
+ return false unless File.file?(path)
183
+
184
+ FileUtils.rm_f(path)
185
+ true
186
+ end
187
+
175
188
  def locale
176
189
  load["locale"].to_s
177
190
  end
@@ -8,45 +8,52 @@ module Cli
8
8
  def after_install
9
9
  return if skip_post_install?
10
10
  return unless interactive?
11
- return if Config.after_install_done?
12
-
13
- if Auth.logged_in?
14
- after_login
15
- Config.after_install_done = true
16
- return
17
- end
11
+ return if Auth.logged_in?
18
12
 
19
13
  puts "#{bold('flycal-cli')} #{Locale.t('hooks.after_install.ready')}"
20
14
  print "#{Locale.t('hooks.after_install.prompt')} "
21
- accepted = accept_default_yes?
22
- Config.after_install_done = true
23
- launch_login if accepted
15
+ launch_login if accept_default_yes?
24
16
  end
25
17
 
26
- def after_login
27
- return unless Auth.logged_in?
18
+ def after_uninstall
19
+ return if skip_post_uninstall?
20
+ return unless interactive?
21
+ return unless last_flycal_cli_install?
22
+ return unless uninstall_cleanup_needed?
28
23
 
29
- current = Config.calendar_default.to_s.strip
30
- calendars = Flycal.connect(credentials: Auth.credentials).list_calendars
24
+ print "#{uninstall_text('hooks.after_uninstall.prompt')} "
25
+ return unless accept_default_no?
31
26
 
32
- if current.empty?
33
- selected = calendars.find { |calendar| calendar.respond_to?(:primary) && calendar.primary } || calendars.first
34
- return unless selected
27
+ backed_up = Config.backup_and_remove_config!
28
+ Config.remove_tokens!
35
29
 
36
- Config.calendar_default = selected.id
37
- announce_default_calendar(display_name(selected))
38
- else
39
- selected = calendars.find { |calendar| calendar.id.to_s == current } ||
40
- calendars.find { |calendar| calendar.summary.to_s == current }
41
- announce_default_calendar(display_name(selected) || current)
42
- end
43
- rescue StandardError => e
44
- fallback = Config.calendar_default.to_s.strip
45
- if fallback.empty?
30
+ puts uninstall_text("hooks.after_uninstall.saved", { backup: Config.config_backup_file }) if backed_up
31
+ puts uninstall_text("hooks.after_uninstall.tokens_removed")
32
+ end
33
+
34
+ def after_login
35
+ creds = Auth.credentials
36
+ return unless creds
37
+
38
+ selected = nil
39
+ begin
40
+ calendars = Flycal.connect(credentials: creds).list_calendars
41
+ current = Config.calendar_default.to_s.strip
42
+ if current.empty?
43
+ selected = calendars.find { |calendar| calendar.respond_to?(:primary) && calendar.primary } || calendars.first
44
+ Config.calendar_default = selected.id if selected
45
+ else
46
+ selected = calendars.find { |calendar| calendar.id.to_s == current } ||
47
+ calendars.find { |calendar| calendar.summary.to_s == current }
48
+ end
49
+ rescue StandardError => e
46
50
  warn "flycal-cli after_login: #{e.message}"
47
- else
48
- announce_default_calendar(fallback)
49
51
  end
52
+
53
+ name = display_name(selected) || Config.calendar_default
54
+ return if name.to_s.strip.empty?
55
+
56
+ announce_default_calendar(name)
50
57
  end
51
58
 
52
59
  def launch_login
@@ -62,6 +69,29 @@ module Cli
62
69
  ENV["FLYCAL_SKIP_POST_INSTALL"].to_s == "1"
63
70
  end
64
71
 
72
+ def skip_post_uninstall?
73
+ ENV["FLYCAL_SKIP_POST_UNINSTALL"].to_s == "1"
74
+ end
75
+
76
+ def last_flycal_cli_install?
77
+ Gem::Specification.find_all_by_name("flycal-cli").size <= 1
78
+ rescue StandardError
79
+ true
80
+ end
81
+
82
+ def uninstall_cleanup_needed?
83
+ File.file?(Config.config_file) || File.file?(Config.tokens_path)
84
+ end
85
+
86
+ # Avoid Cli::Locale (it reads Config and would recreate config.yml after the backup).
87
+ def uninstall_text(key, vars = {})
88
+ previous = Thread.current[:flycal_locale_default_provider]
89
+ Thread.current[:flycal_locale_default_provider] = nil
90
+ Flycal::Locale.t(key, vars)
91
+ ensure
92
+ Thread.current[:flycal_locale_default_provider] = previous
93
+ end
94
+
65
95
  def skip_cli_after_install?(argv)
66
96
  argv = Array(argv)
67
97
  return true if argv.intersect?(%w[--version -v --help -h])
@@ -84,6 +114,16 @@ module Cli
84
114
  %w[y yes].include?(answer)
85
115
  end
86
116
 
117
+ def accept_default_no?(input = $stdin)
118
+ line = input.gets
119
+ return false if line.nil?
120
+
121
+ answer = line.strip.downcase
122
+ return false if answer.empty?
123
+
124
+ %w[y yes].include?(answer)
125
+ end
126
+
87
127
  def bold(text)
88
128
  return text.to_s unless $stdout.tty?
89
129
 
data/lib/flycal.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # Under Rails, Zeitwerk autoloads lib/flycal/*.
5
5
  # The CLI gem entrypoint (flycal_cli/lib/flycal_cli.rb) eager-requires what it needs.
6
6
  module Flycal
7
- VERSION = "1.4"
7
+ VERSION = "1.5.2"
8
8
 
9
9
  def self.connect(credentials:)
10
10
  Client.new(credentials: credentials)
@@ -11,3 +11,14 @@ Gem.post_install do |installer|
11
11
  warn "flycal-cli post-install: #{e.message}"
12
12
  end
13
13
  end
14
+
15
+ Gem.pre_uninstall do |uninstaller|
16
+ next unless uninstaller.spec.name == "flycal-cli"
17
+
18
+ begin
19
+ require "flycal_cli"
20
+ Flycal::Cli::Hooks.after_uninstall
21
+ rescue LoadError, StandardError => e
22
+ warn "flycal-cli pre-uninstall: #{e.message}"
23
+ end
24
+ end
data/locales/en.json CHANGED
@@ -61,6 +61,11 @@
61
61
  "using": "is using %{calendar} by default,",
62
62
  "change": "you can change it using command 'flycal config',",
63
63
  "goodbye": "happy calendling!"
64
+ },
65
+ "after_uninstall": {
66
+ "prompt": "Remove Flycal config and Google session? config.yml will be saved as config.backup.yml, tokens.yml will be deleted (y/N):",
67
+ "saved": "Saved as %{backup}",
68
+ "tokens_removed": "Removed ~/.flycal/tokens.yml"
64
69
  }
65
70
  },
66
71
  "errors": {
data/locales/it.json CHANGED
@@ -61,6 +61,11 @@
61
61
  "using": "sta usando %{calendar} come predefinito,",
62
62
  "change": "puoi cambiarlo con il comando 'flycal config',",
63
63
  "goodbye": "happy calendling!"
64
+ },
65
+ "after_uninstall": {
66
+ "prompt": "Vuoi rimuovere la configurazione e la sessione Google? config.yml verrà salvato come config.backup.yml, tokens.yml verrà cancellato (y/N):",
67
+ "saved": "Salvato in %{backup}",
68
+ "tokens_removed": "Rimosso ~/.flycal/tokens.yml"
64
69
  }
65
70
  },
66
71
  "errors": {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flycal-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.4'
4
+ version: 1.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - flycal-cli