Class: Utcp::Providers::CliProvider

Inherits:
BaseProvider show all
Defined in:
lib/utcp/providers/cli_provider.rb

Overview

CLI provider for executing local commands (use with caution) tool.provider: { “provider_type”:“cli”, “command”:[“echo”,“hello $BaseProvider#name”] }

Instance Attribute Summary

Attributes inherited from BaseProvider

#auth, #name, #type

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ CliProvider

Returns a new instance of CliProvider.



12
13
14
# File 'lib/utcp/providers/cli_provider.rb', line 12

def initialize(name:)
  super(name: name, provider_type: "cli", auth: nil)
end

Instance Method Details

#call_tool(tool, arguments = {}, &block) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/utcp/providers/cli_provider.rb', line 20

def call_tool(tool, arguments = {}, &block)
  p = tool.provider
  cmd = p["command"]
  raise ConfigError, "cli provider requires 'command' array" unless cmd.is_a?(Array) && !cmd.empty?
  args = Utils::Subst.apply(arguments || {})

  expanded = cmd.map do |part|
    part.to_s.gsub(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/) { |m| args[$1] || ENV[$1] || m }
  end

  stdout, stderr, status = Open3.capture3(*expanded)
  {
    "ok" => status.success?,
    "exit_code" => status.exitstatus,
    "stdout" => stdout,
    "stderr" => stderr
  }
end

#discover_tools!Object

Raises:



16
17
18
# File 'lib/utcp/providers/cli_provider.rb', line 16

def discover_tools!
  raise ProviderError, "CLI is an execution provider only"
end