Class: Booker::Config

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

Overview

configuration

Constant Summary collapse

VALID =
[:searcher, :bookmarks, :browser, :picker].freeze
HOME =
ENV.fetch("HOME", "/usr/local/").freeze
YAMLCONF =
(HOME + "/.booker.yml").freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/booker/config.rb', line 74

def initialize
  # the yaml file if there is one, and only work out the defaults when
  # there is not: discovery walks every chrome profile directory to answer
  # a question the config file has already answered
  @config = read(YAMLCONF) || {
    browser: "open ",
    searcher: "https://google.com/search?q=",
    bookmarks: detect_default_bookmarks
  }

  # prune bad config keys
  @config.each_key do |k|
    if !VALID.include? k.to_sym
      warn "Failure:".red + " Bad key found in config file: #{k}"
      exit 1
    end
  end
end

Class Method Details

.defaultObject

one instance per run. Bookmarks, Picker and the CLI want the same answers, and one Config apiece meant parsing the yaml three times, discovering three times, and warning three times to say it once



68
# File 'lib/booker/config.rb', line 68

def default = @default ||= new

.reset!Object

the memo outlives a single example, so the suite drops it between them



71
# File 'lib/booker/config.rb', line 71

def reset! = @default = nil

Instance Method Details

#bookmarksObject

always an array, so callers never branch on it: configs written before multi-source support hold a single path string



133
# File 'lib/booker/config.rb', line 133

def bookmarks = Array(@config[:bookmarks])

#detect_default_bookmarksObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/booker/config.rb', line 93

def detect_default_bookmarks
  # Return ALL available bookmark sources (Chrome, Firefox, Safari)
  all_sources = discover_all_bookmark_sources

  # If we found sources, return them all; otherwise return a default single path
  if all_sources.empty?
    # Fallback to macOS Chrome default for backward compatibility
    HOME + "/Library/Application Support/Google/Chrome/Profile 1/Bookmarks"
  else
    all_sources
  end
end

#discover_all_bookmark_sourcesObject



106
# File 'lib/booker/config.rb', line 106

def discover_all_bookmark_sources = Sources.discover

#pickerObject

the finder command with its flags, split with Shellwords rather than handed to a shell. nil means "auto detect fzf", which is why #write leaves it out of the defaults: #initialize exits on keys it does not know, so a config naming it is unreadable by a booker predating the key



141
# File 'lib/booker/config.rb', line 141

def picker = @config[:picker]

#read(file) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/booker/config.rb', line 108

def read(file)
  # #write emits symbol keys, so they have to be permitted on the way back
  YAML.safe_load_file(file, permitted_classes: [Symbol])
rescue Errno::ENOENT
  warn "Warning: ".yel +
    "YAML configuration file couldn't be found. Using defaults."
  warn "Suggest: ".grn + "booker --install config"
  false
rescue Psych::Exception
  # every psych failure means the same thing here - an unusable config to
  # fall back from rather than crash on. rescuing SyntaxError alone let
  # anchors through as an AliasesNotEnabled backtrace under psych 4
  warn "Warning: ".red +
    "YAML configuration file could not be read. Using defaults."
  false
end

#searcherObject



135
# File 'lib/booker/config.rb', line 135

def searcher = @config[:searcher]

#write(k = nil, v = nil) ⇒ Object

used for creating and updating the default configuration



126
127
128
129
# File 'lib/booker/config.rb', line 126

def write(k = nil, v = nil)
  @config[k] = v unless k.nil? # update users yaml config file
  File.write(YAMLCONF, @config.to_yaml)
end