Module: OllamaChat::ConfigHandling
Overview
Provides functionality for handling configuration files and settings for OllamaChat. It loads configuration from YAML files, supports environment variable overrides, and offers methods to read and write configuration data. The module also ensures default values are set and validates configuration structure.
Instance Method Summary collapse
-
#config ⇒ ComplexConfig::Settings
The config method returns the configuration object associated with the class.
-
#config=(config) ⇒ Object
private
The config= method assigns a new configuration object to the class.
-
#diff_config ⇒ Boolean?
private
Opens a diff tool to compare the current config file with the default config, allowing the user to visually inspect differences.
-
#display_config ⇒ Object
private
The display_config method renders the configuration and displays it using a pager.
-
#edit_config ⇒ Object
private
Edit the current configuration file in the editor defined by the environment variable
EDITOR. -
#fix_config(exception) ⇒ Object
private
The fix_config method handles configuration file errors by informing the user about the exception and prompting them to fix it.
-
#fix_session(argv) ⇒ Array<String>
private
Adjusts the command-line argument array to ensure the current session is explicitly reloaded by injecting the
-l <session_id>flag. -
#reload_config ⇒ Object
private
Reloads the current configuration by restarting the process.
-
#run_syntax_check(checker, path) ⇒ Hash?
Runs the syntax checker on the given file and returns a result hash.
-
#syntax_checker_for(path) ⇒ ComplexConfig::Settings?
Returns the first enabled syntax checker whose suffixes match the given file's extension, or nil if no match.
Instance Method Details
#config ⇒ ComplexConfig::Settings
The config method returns the configuration object associated with the class.
21 22 23 |
# File 'lib/ollama_chat/config_handling.rb', line 21 def config self.class.config end |
#config=(config) ⇒ Object (private)
The config= method assigns a new configuration object to the class.
60 61 62 |
# File 'lib/ollama_chat/config_handling.rb', line 60 def config=(config) self.class.config = config end |
#diff_config ⇒ Boolean? (private)
Opens a diff tool to compare the current config file with the default config, allowing the user to visually inspect differences.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ollama_chat/config_handling.rb', line 84 def diff_config diff_tool = OC::DIFF_TOOL? or return STDERR.puts 'No diff tool configured (OC::DIFF_TOOL).' cmd = [ diff_tool, @ollama_chat_config.filename, @ollama_chat_config.default_config_path, ].map(&:to_s) system(*cmd) end |
#display_config ⇒ Object (private)
The display_config method renders the configuration and displays it using a pager. It determines an appropriate pager command based on environment variables and available system commands, then uses Kramdown::ANSI::Pager to show the formatted configuration output.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ollama_chat/config_handling.rb', line 68 def display_config command = OC::PAGER? rendered = config.to_s Kramdown::ANSI::Pager.pager( lines: rendered.count(?\n), command: ) do |output| output.puts rendered end end |
#edit_config ⇒ Object (private)
Edit the current configuration file in the editor defined by the
environment variable EDITOR.
- Looks up the editor command via
OC::EDITOR. If the value isnilor empty, it prints an error message to STDERR and returns immediately. - Invokes the editor with the path to the active configuration
file (
@ollama_chat_config.filename). The editor is launched viasystemso that the process inherits the current terminal, allowing inโplace editing. - If editing was successful, prompts the user to restart
ollama_chatif desired.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/ollama_chat/config_handling.rb', line 166 def edit_config if result = edit_file(@ollama_chat_config.filename) if confirm?( prompt: "๐ Do you want to restart #{progname}? (y/n) ", yes: /\Ay/i ) then session_close exec($0, *fix_session(ARGV)) else STDOUT.puts "Skipped reloading the config." end else STDERR.puts "Editor returned a non-zero status!" end end |
#fix_config(exception) ⇒ Object (private)
The fix_config method handles configuration file errors by informing the user about the exception and prompting them to fix it. It then executes a diff tool to compare the current config file with the default one. This method exits the program after handling the configuration error.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ollama_chat/config_handling.rb', line 102 def fix_config(exception) STDOUT.puts "When reading the config file, a #{exception.class} " \ "exception was caught: #{exception..inspect}" unless OC::DIFF_TOOL? exit 1 end if confirm?( prompt: '๐ Do you want to fix the config? (y/n) ', yes: /\Ay/i ) then diff_config exit 0 else exit 1 end end |
#fix_session(argv) ⇒ Array<String> (private)
Adjusts the command-line argument array to ensure the current session is
explicitly reloaded by injecting the -l <session_id> flag.
This method handles the following argument scenarios to prevent breaking existing flags:
- Missing Flag: If
-lis not present, it appends-land the current session ID to the end of the array. - Empty Flag: If
-lis present but the next element is another flag (starts with-), it inserts the session ID immediately after-l. (This case should actually never happen.) - Existing Flag: If
-lis present and followed by a value, it replaces that value with the current session ID to ensure continuity.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ollama_chat/config_handling.rb', line 139 def fix_session(argv) argv = argv.dup if i = argv.index('-l') j = i + 1 unless argv[j]&.start_with?(?-) argv[j] = session.id.to_s else argv.insert(j, session.id.to_s) end else argv << '-l' << session.id.to_s end argv end |
#reload_config ⇒ Object (private)
Reloads the current configuration by restarting the process.
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/ollama_chat/config_handling.rb', line 187 def reload_config if confirm?( prompt: "๐ Do you want to restart #{progname}? (y/n) ", yes: /\Ay/i ) then session_close exec($0, *fix_session(ARGV)) else STDOUT.puts "Skipped reloading the config." end end |
#run_syntax_check(checker, path) ⇒ Hash?
Runs the syntax checker on the given file and returns a result hash.
45 46 47 48 49 50 51 52 53 |
# File 'lib/ollama_chat/config_handling.rb', line 45 def run_syntax_check(checker, path) _stdout, stderr, status = Open3.capture3(*checker.cmd, path.to_s) { status: status.success? ? 'pass' : 'fail', output: stderr.strip } rescue Errno::ENOENT nil end |
#syntax_checker_for(path) ⇒ ComplexConfig::Settings?
Returns the first enabled syntax checker whose suffixes match the given file's extension, or nil if no match.
30 31 32 33 34 35 36 37 38 |
# File 'lib/ollama_chat/config_handling.rb', line 30 def syntax_checker_for(path) path = Pathname.new(path) ext = path.extname.delete_prefix(?.).full? or return config.syntax_checkers.attribute_values.each do |checker| next unless checker.enabled return checker if checker.suffixes.include?(ext) end nil end |