class Geordi::Settings

Constants

ALLOWED_GLOBAL_SETTINGS
ALLOWED_LOCAL_SETTINGS
GLOBAL_SETTINGS_FILE_NAME
LOCAL_SETTINGS_FILE_NAME
SETTINGS_WARNED

Public Class Methods

new() click to toggle source
# File lib/geordi/settings.rb, line 23
def initialize
  read_settings
end

Public Instance Methods

auto_update_chromedriver() click to toggle source
# File lib/geordi/settings.rb, line 48
def auto_update_chromedriver
  @global_settings["auto_update_chromedriver"] || false
end
auto_update_chromedriver=(value) click to toggle source
# File lib/geordi/settings.rb, line 52
def auto_update_chromedriver=(value)
  @global_settings['auto_update_chromedriver'] = value
  save_global_settings
end
hint_probability() click to toggle source
# File lib/geordi/settings.rb, line 44
def hint_probability
  @global_settings['hint_probability']
end
irb_flags() click to toggle source

Global settings

# File lib/geordi/settings.rb, line 28
def irb_flags
  @global_settings['irb_flags']
end
linear_api_key() click to toggle source
# File lib/geordi/settings.rb, line 32
def linear_api_key
  @global_settings['linear_api_key'] || begin
    Interaction.warn 'Linear API key not found'
    inquire_linear_api_key
  end
end
linear_api_key=(value) click to toggle source
# File lib/geordi/settings.rb, line 39
def linear_api_key=(value)
  @global_settings['linear_api_key'] = value
  save_global_settings
end
linear_team_ids() click to toggle source
# File lib/geordi/settings.rb, line 57
def linear_team_ids
  local_team_ids = normalize_team_ids(@local_settings['linear_team_ids'])
  global_team_ids = normalize_team_ids(@global_settings['linear_team_ids'])

  team_ids = local_team_ids | global_team_ids

  if team_ids.empty?
    Geordi::Interaction.warn 'No team id found.'
    Interaction.note 'Please open a team in Linear, open the command menu with CTRL + K and choose'
    Interaction.note "\"Copy model UUID\". Store that team id in #{LOCAL_SETTINGS_FILE_NAME}:"
    puts 'linear_team_ids: abc-123-123-abc, def-456-456-def'
    exit 1
  end

  team_ids
end

Private Instance Methods

check_for_invalid_keys(settings, allowed_keys, file) click to toggle source
# File lib/geordi/settings.rb, line 100
def check_for_invalid_keys(settings, allowed_keys, file)
  return if settings.nil?

  invalid_keys = settings.keys - allowed_keys
  unless invalid_keys.empty?
    Interaction.warn "Unknown settings in #{file}: #{invalid_keys.join(", ")}"
    puts "Supported settings in #{file} are: #{allowed_keys.join(", ")}"
  end
end
inquire_linear_api_key() click to toggle source
# File lib/geordi/settings.rb, line 119
def inquire_linear_api_key
  Geordi::Interaction.note 'Create an API key here: https://linear.app/makandra/settings/api'
  token = Geordi::Interaction.prompt("Please enter the API key:")
  self.linear_api_key = token
  Interaction.note("API key stored in #{GLOBAL_SETTINGS_FILE_NAME}.")
  puts

  token
end
normalize_team_ids(team_ids) click to toggle source
# File lib/geordi/settings.rb, line 129
def normalize_team_ids(team_ids)
  case team_ids
  when Array
    team_ids
  when String
    team_ids.split(/[\s,;]+/)
  when Integer
    [team_ids]
  else
    []
  end
end
read_settings() click to toggle source
# File lib/geordi/settings.rb, line 76
def read_settings
  global_path = GLOBAL_SETTINGS_FILE_NAME
  local_path = LOCAL_SETTINGS_FILE_NAME

  global_settings = if File.exist?(global_path)
    YAML.safe_load(File.read(global_path))
  end
  local_settings = if File.exist?(local_path)
    YAML.safe_load(File.read(local_path))
  end

  # Prevent duplicate warnings caused by another instance of Settings
  unless ENV[SETTINGS_WARNED]
    check_for_invalid_keys(global_settings, ALLOWED_GLOBAL_SETTINGS, global_path)
    check_for_invalid_keys(local_settings, ALLOWED_LOCAL_SETTINGS, local_path)
    Interaction.warn "Unsupported config file \".firefox-version\". Please remove it." if File.exist?('.firefox-version')

    ENV[SETTINGS_WARNED] = 'true'
  end

  @global_settings = global_settings || {}
  @local_settings = local_settings || {}
end
save_global_settings() click to toggle source
# File lib/geordi/settings.rb, line 110
def save_global_settings
  global_path = GLOBAL_SETTINGS_FILE_NAME
  global_directory = File.dirname(global_path)
  FileUtils.mkdir_p(global_directory) unless File.directory? global_directory
  File.open(global_path, 'w') do |file|
    file.write @global_settings.to_yaml
  end
end