Module: Kreuzberg::CLIProxy

Defined in:
lib/kreuzberg/cli_proxy.rb

Defined Under Namespace

Classes: CLIExecutionError, Error, MissingBinaryError

Class Method Summary collapse

Class Method Details

.call(argv) ⇒ String

Execute the Kreuzberg CLI with given arguments

Examples:

Extract a file

output = Kreuzberg::CLIProxy.call(['extract', 'document.pdf'])

Detect file type

output = Kreuzberg::CLIProxy.call(['detect', 'document.pdf'])

Parameters:

  • argv (Array<String>)

    Command-line arguments

Returns:

  • (String)

    Standard output from the CLI

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kreuzberg/cli_proxy.rb', line 37

def call(argv)
  binary = find_cli_binary
  args = Array(argv).map(&:to_s)
  stdout, stderr, status = Open3.capture3(binary.to_s, *args)
  return stdout if status.success?

  raise CLIExecutionError.new(
    "kreuzberg CLI exited with status #{status.exitstatus}",
    stderr: stderr,
    status: status.exitstatus
  )
end

.find_cli_binaryPathname

Find the kreuzberg CLI binary

Searches in multiple locations:

  • crates/kreuzberg-cli/target/release/

  • packages/ruby/lib/bin/

  • workspace root target/release/

Returns:

  • (Pathname)

    Path to the CLI binary

Raises:



60
61
62
63
64
65
66
# File 'lib/kreuzberg/cli_proxy.rb', line 60

def find_cli_binary
  binary_name = Gem.win_platform? ? 'kreuzberg.exe' : 'kreuzberg'
  found = search_paths(binary_name).find(&:file?)
  return found if found

  raise MissingBinaryError, missing_binary_message
end

.lib_pathPathname

Get the lib path

Returns:

  • (Pathname)

    Lib path



80
81
82
# File 'lib/kreuzberg/cli_proxy.rb', line 80

def lib_path
  @lib_path ||= Pathname(__dir__ || '.').join('..').expand_path
end

.missing_binary_messageString

Error message when binary is missing

Returns:

  • (String)

    Error message



109
110
111
112
113
114
115
116
# File 'lib/kreuzberg/cli_proxy.rb', line 109

def missing_binary_message
  <<~MSG.strip
    kreuzberg CLI binary not found. Build it with:
    `cargo build --release --package kreuzberg-cli`

    Or install the gem with pre-built binaries.
  MSG
end

.root_pathPathname

Get the root path of the Ruby package

Returns:

  • (Pathname)

    Root path



72
73
74
# File 'lib/kreuzberg/cli_proxy.rb', line 72

def root_path
  @root_path ||= Pathname(__dir__ || '.').join('../..').expand_path
end

.search_paths(binary_name) ⇒ Array<Pathname>

Search paths for the CLI binary

Parameters:

  • binary_name (String)

    Name of the binary

Returns:

  • (Array<Pathname>)

    List of paths to search



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kreuzberg/cli_proxy.rb', line 89

def search_paths(binary_name)
  paths = [
    lib_path.join('bin', binary_name),
    lib_path.join(binary_name),
    root_path.join('../../crates/kreuzberg-cli/target/release', binary_name),
    root_path.join('../../target/release', binary_name),
    root_path.join('../../target/debug', binary_name)
  ]

  workspace_root = root_path.parent&.parent
  paths << workspace_root.join('target', 'release', binary_name) if workspace_root
  paths << workspace_root.join('target', 'debug', binary_name) if workspace_root

  paths
end