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.
-
#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.
Instance Method Details
#config ⇒ ComplexConfig::Settings
The config method returns the configuration object associated with the class.
19 20 21 |
# File 'lib/ollama_chat/config_handling.rb', line 19 def config self.class.config end |
#config=(config) ⇒ Object (private)
The config= method assigns a new configuration object to the class.
28 29 30 |
# File 'lib/ollama_chat/config_handling.rb', line 28 def config=(config) self.class.config = config 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.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ollama_chat/config_handling.rb', line 36 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.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ollama_chat/config_handling.rb', line 122 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.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ollama_chat/config_handling.rb', line 54 def fix_config(exception) STDOUT.puts "When reading the config file, a #{exception.class} "\ "exception was caught: #{exception..inspect}" unless diff_tool = OC::DIFF_TOOL? exit 1 end if confirm?( prompt: '๐ Do you want to fix the config? (y/n) ', yes: /\Ay/i ) then cmd = [ diff_tool, @ollama_chat_config.filename, @ollama_chat_config.default_config_path, ].map(&:to_s) system(*cmd) 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ollama_chat/config_handling.rb', line 95 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.
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/ollama_chat/config_handling.rb', line 143 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 |