Class: Flycal::Cli::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/flycal/cli/config.rb

Constant Summary collapse

CONFIG_DIR =
File.expand_path("~/.flycal")
CONFIG_FILE =
File.join(CONFIG_DIR, "config.yml")
CONFIG_BACKUP_FILE =
File.join(CONFIG_DIR, "config.backup.yml")
CREDENTIALS_FILE =
File.join(CONFIG_DIR, "credentials.json")
TOKENS_FILE =
File.join(CONFIG_DIR, "tokens.yml")
DEFAULTS_FILE =
File.expand_path("../../../config/defaults.yml", __dir__)

Class Method Summary collapse

Class Method Details

.auth_clientObject



57
58
59
60
61
62
# File 'lib/flycal/cli/config.rb', line 57

def auth_client
  value = load["auth_client"].to_s.strip.downcase
  return value if %w[flycal json].include?(value)

  nil
end

.auth_client=(value) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/flycal/cli/config.rb', line 64

def auth_client=(value)
  data = load
  if value.to_s.strip.empty?
    data.delete("auth_client")
  else
    data["auth_client"] = value.to_s.strip.downcase
  end
  save(data)
end

.backup_and_remove_config!Object



170
171
172
173
174
175
176
177
178
# File 'lib/flycal/cli/config.rb', line 170

def backup_and_remove_config!
  source = config_file
  return false unless File.file?(source)

  FileUtils.mkdir_p(config_dir)
  FileUtils.mv(source, config_backup_file, force: true)
  FileUtils.rm_f(source)
  true
end

.calendar_defaultObject



36
37
38
39
40
41
# File 'lib/flycal/cli/config.rb', line 36

def calendar_default
  value = load["calendar_default"]
  return nil if value.nil? || value.to_s.strip.empty? || value.to_s == "~"

  value.to_s
end

.calendar_default=(calendar_id) ⇒ Object



43
44
45
46
47
# File 'lib/flycal/cli/config.rb', line 43

def calendar_default=(calendar_id)
  data = load
  data["calendar_default"] = calendar_id
  save(data)
end

.clear_allObject



82
83
84
# File 'lib/flycal/cli/config.rb', line 82

def clear_all
  FileUtils.rm_rf(CONFIG_DIR)
end

.config_backup_fileObject



166
167
168
# File 'lib/flycal/cli/config.rb', line 166

def config_backup_file
  CONFIG_BACKUP_FILE
end

.config_dirObject



78
79
80
# File 'lib/flycal/cli/config.rb', line 78

def config_dir
  CONFIG_DIR
end

.config_fileObject



162
163
164
# File 'lib/flycal/cli/config.rb', line 162

def config_file
  CONFIG_FILE
end

.credentials_exist?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/flycal/cli/config.rb', line 53

def credentials_exist?
  File.exist?(CREDENTIALS_FILE)
end

.credentials_pathObject



49
50
51
# File 'lib/flycal/cli/config.rb', line 49

def credentials_path
  CREDENTIALS_FILE
end

.deep_dup(value) ⇒ Object



230
231
232
233
234
235
236
237
238
239
# File 'lib/flycal/cli/config.rb', line 230

def deep_dup(value)
  case value
  when Hash
    value.transform_values { |v| deep_dup(v) }
  when Array
    value.map { |v| deep_dup(v) }
  else
    value
  end
end

.default_valuesObject



203
204
205
206
207
208
209
210
# File 'lib/flycal/cli/config.rb', line 203

def default_values
  @default_values ||= begin
    raise "Defaults file not found: #{DEFAULTS_FILE}" unless File.exist?(DEFAULTS_FILE)

    file = YAML.load_file(DEFAULTS_FILE) || {}
    file.merge("slots" => Flycal::DefaultSlots.config_fragment)
  end
end

.exclude_calendarsObject



90
91
92
# File 'lib/flycal/cli/config.rb', line 90

def exclude_calendars
  Array(slots_config["exclude_calendars"]).map(&:to_s).reject(&:empty?)
end

.exclude_calendars=(calendar_ids) ⇒ Object



94
95
96
97
98
99
# File 'lib/flycal/cli/config.rb', line 94

def exclude_calendars=(calendar_ids)
  data = load
  data["slots"] ||= {}
  data["slots"]["exclude_calendars"] = Array(calendar_ids).map(&:to_s)
  save(data)
end

.loadObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flycal/cli/config.rb', line 17

def load
  user_data = if File.exist?(CONFIG_FILE)
                YAML.load_file(CONFIG_FILE) || {}
              else
                {}
              end

  merged, changed = merge_missing_defaults(user_data, default_values)
  merged, migrated = normalize_slots_keys(merged)
  save(merged) if changed || migrated || !File.exist?(CONFIG_FILE)

  merged
end

.localeObject



188
189
190
# File 'lib/flycal/cli/config.rb', line 188

def locale
  load["locale"].to_s
end

.locale=(value) ⇒ Object



197
198
199
200
201
# File 'lib/flycal/cli/config.rb', line 197

def locale=(value)
  data = load
  data["locale"] = value.to_s
  save(data)
end

.merge_missing_defaults(target, defaults) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/flycal/cli/config.rb', line 212

def merge_missing_defaults(target, defaults)
  merged = target.dup
  changed = false

  defaults.each do |key, default_value|
    if !merged.key?(key)
      merged[key] = deep_dup(default_value)
      changed = true
    elsif default_value.is_a?(Hash) && merged[key].is_a?(Hash)
      nested, nested_changed = merge_missing_defaults(merged[key], default_value)
      merged[key] = nested
      changed ||= nested_changed
    end
  end

  [merged, changed]
end

.normalize_slots_keys(data) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/flycal/cli/config.rb', line 101

def normalize_slots_keys(data)
  slots = data["slots"]
  return [data, false] unless slots.is_a?(Hash)

  changed = false
  normalized = slots.dup

  if normalized.key?("exclude-calendars") && !normalized.key?("exclude_calendars")
    normalized["exclude_calendars"] = normalized.delete("exclude-calendars")
    changed = true
  end

  if normalized.key?("weekdays-only") && !normalized.key?("weekdays_only")
    normalized["weekdays_only"] = normalized.delete("weekdays-only")
    changed = true
  end

  if normalized.key?("workhours") && !normalized.key?("hours")
    normalized["hours"] = normalized.delete("workhours")
    changed = true
  end

  if normalized.key?("tempaltes") && !normalized.key?("templates")
    normalized["templates"] = normalized.delete("tempaltes")
    changed = true
  end

  templates = normalized["templates"]
  templates_empty = !templates.is_a?(Hash) || templates.empty?

  if templates_empty && normalized["hours"]
    days =
      if normalized.key?("weekdays_only") && normalized["weekdays_only"] == false
        [1, 2, 3, 4, 5, 6, 7]
      else
        [1, 2, 3, 4, 5]
      end

    normalized["templates"] = {
      "work" => {
        "days" => days,
        "hours" => deep_dup(normalized["hours"])
      }
    }
    changed = true
  end

  %w[hours weekdays_only weekdays-only workhours tempaltes].each do |legacy_key|
    next unless normalized.key?(legacy_key)

    normalized.delete(legacy_key)
    changed = true
  end

  return [data, false] unless changed

  data = data.dup
  data["slots"] = normalized
  [data, true]
end

.remove_tokens!Object



180
181
182
183
184
185
186
# File 'lib/flycal/cli/config.rb', line 180

def remove_tokens!
  path = tokens_path
  return false unless File.file?(path)

  FileUtils.rm_f(path)
  true
end

.save(data) ⇒ Object



31
32
33
34
# File 'lib/flycal/cli/config.rb', line 31

def save(data)
  FileUtils.mkdir_p(CONFIG_DIR)
  File.write(CONFIG_FILE, data.to_yaml)
end

.slots_configObject



86
87
88
# File 'lib/flycal/cli/config.rb', line 86

def slots_config
  load.fetch("slots", {})
end

.timezoneObject



192
193
194
195
# File 'lib/flycal/cli/config.rb', line 192

def timezone
  value = load["timezone"].to_s.strip
  value.empty? ? Flycal::TimeFormat::DEFAULT_ZONE : value
end

.tokens_pathObject



74
75
76
# File 'lib/flycal/cli/config.rb', line 74

def tokens_path
  TOKENS_FILE
end