flycal-cli 1.4 → 1.5.1

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: d5920907ee209e0ea1204364f39bff454f52eb5214d4a2f72011b037cb6f6e58
4
+ data.tar.gz: e140b8f76c717c338d33b85f53b08defb9fde1aecfbdcf6e416ad33b2a87ed94
5
5
  SHA512:
6
- metadata.gz: 13cc3c1b5c9aaa8e4b630920e6752a61a0bdd90de2a6d302473bbe75c548566b6ab5438f5c9d86180c4a11d424a902069aa3f8e1b6e0f948e0bacb1292a495ac
7
- data.tar.gz: 3d0a56c6403f673b7b8fcf4e06797970e936b3448e2496a22ee2072dc07efed566ece48ad98e42132371f8bae6b8d2143a6915ec281b9e9d0fe0f647db47e7ea
6
+ metadata.gz: 737242d48ae220aefad6560bf0a205ee83862b78ebedb479a2dc825da432a31516382fbea88681eede9bc66e61775b2e774f0508d2a6cb0b0e65469ebadbfd3f
7
+ data.tar.gz: b9aab68b6f14e0abb7d97ce3a975ee5be64ba3bc96a4263f8d7b01a1c24c121f4f46fce3913651a7cfeb83e7b7544698c1001724716abc8a86d1a7dc102bed70
@@ -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,19 @@ 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)
176
+ true
177
+ end
178
+
175
179
  def locale
176
180
  load["locale"].to_s
177
181
  end
@@ -8,45 +8,50 @@ 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?
28
-
29
- current = Config.calendar_default.to_s.strip
30
- calendars = Flycal.connect(credentials: Auth.credentials).list_calendars
18
+ def after_uninstall
19
+ return if skip_post_uninstall?
20
+ return unless interactive?
21
+ return unless last_flycal_cli_install?
22
+ return unless File.file?(Config.config_file)
31
23
 
32
- if current.empty?
33
- selected = calendars.find { |calendar| calendar.respond_to?(:primary) && calendar.primary } || calendars.first
34
- return unless selected
24
+ print "#{Locale.t('hooks.after_uninstall.prompt')} "
25
+ return unless accept_default_no?
35
26
 
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)
27
+ if Config.backup_and_remove_config!
28
+ puts Locale.t("hooks.after_uninstall.saved", { backup: Config.config_backup_file })
42
29
  end
43
- rescue StandardError => e
44
- fallback = Config.calendar_default.to_s.strip
45
- if fallback.empty?
30
+ end
31
+
32
+ def after_login
33
+ creds = Auth.credentials
34
+ return unless creds
35
+
36
+ selected = nil
37
+ begin
38
+ calendars = Flycal.connect(credentials: creds).list_calendars
39
+ current = Config.calendar_default.to_s.strip
40
+ if current.empty?
41
+ selected = calendars.find { |calendar| calendar.respond_to?(:primary) && calendar.primary } || calendars.first
42
+ Config.calendar_default = selected.id if selected
43
+ else
44
+ selected = calendars.find { |calendar| calendar.id.to_s == current } ||
45
+ calendars.find { |calendar| calendar.summary.to_s == current }
46
+ end
47
+ rescue StandardError => e
46
48
  warn "flycal-cli after_login: #{e.message}"
47
- else
48
- announce_default_calendar(fallback)
49
49
  end
50
+
51
+ name = display_name(selected) || Config.calendar_default
52
+ return if name.to_s.strip.empty?
53
+
54
+ announce_default_calendar(name)
50
55
  end
51
56
 
52
57
  def launch_login
@@ -62,6 +67,16 @@ module Cli
62
67
  ENV["FLYCAL_SKIP_POST_INSTALL"].to_s == "1"
63
68
  end
64
69
 
70
+ def skip_post_uninstall?
71
+ ENV["FLYCAL_SKIP_POST_UNINSTALL"].to_s == "1"
72
+ end
73
+
74
+ def last_flycal_cli_install?
75
+ Gem::Specification.find_all_by_name("flycal-cli").size <= 1
76
+ rescue StandardError
77
+ true
78
+ end
79
+
65
80
  def skip_cli_after_install?(argv)
66
81
  argv = Array(argv)
67
82
  return true if argv.intersect?(%w[--version -v --help -h])
@@ -84,6 +99,16 @@ module Cli
84
99
  %w[y yes].include?(answer)
85
100
  end
86
101
 
102
+ def accept_default_no?(input = $stdin)
103
+ line = input.gets
104
+ return false if line.nil?
105
+
106
+ answer = line.strip.downcase
107
+ return false if answer.empty?
108
+
109
+ %w[y yes].include?(answer)
110
+ end
111
+
87
112
  def bold(text)
88
113
  return text.to_s unless $stdout.tty?
89
114
 
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.1"
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,10 @@
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.yml? It will be saved as config.backup.yml (y/N):",
67
+ "saved": "Saved as %{backup}"
64
68
  }
65
69
  },
66
70
  "errors": {
data/locales/it.json CHANGED
@@ -61,6 +61,10 @@
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 ~/.flycal/config.yml? Verrà salvato come config.backup.yml (y/N):",
67
+ "saved": "Salvato in %{backup}"
64
68
  }
65
69
  },
66
70
  "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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - flycal-cli