Module: Ollama::Utils::FileArgument

Defined in:
lib/ollama/utils/file_argument.rb

Class Method Summary collapse

Class Method Details

.get_file_argument(path_or_content, default: nil) ⇒ String

Returns the contents of a file or string, or a default value if neither is provided.

Examples:

Get the contents of a file

get_file_argument('path/to/file')

Use a string as content

get_file_argument('string content')

Return a default value if no valid input is given

get_file_argument(nil, default: 'default content')

Parameters:

  • path_or_content (String)

    The path to a file or a string containing the content.

  • default (String) (defaults to: nil)

    The default value to return if no valid input is given. Defaults to nil.

Returns:

  • (String)

    The contents of the file, the string, or the default value.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ollama/utils/file_argument.rb', line 22

def get_file_argument(path_or_content, default: nil)
  if path_or_content.present? && path_or_content.size < 2 ** 15 &&
      File.basename(path_or_content).size < 2 ** 8 &&
      File.exist?(path_or_content)
  then
    File.read(path_or_content)
  elsif path_or_content.present?
    path_or_content
  else
    default
  end
end