Class: Textus::CLI::Verb::Put

Inherits:
Textus::CLI::Verb show all
Defined in:
lib/textus/cli/verb/put.rb

Instance Attribute Summary

Attributes inherited from Textus::CLI::Verb

#positional

Instance Method Summary collapse

Methods inherited from Textus::CLI::Verb

command_name, #context_for, descendants, #emit, inherited, #initialize, needs_store?, option, options, parent_group, #parse, #resolved_role, #session_for

Constructor Details

This class inherits a constructor from Textus::CLI::Verb

Instance Method Details

#call(store) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/textus/cli/verb/put.rb', line 11

def call(store)
  key = positional.shift or raise UsageError.new("put requires a key")
  raise UsageError.new("put requires --stdin in v1") unless use_stdin

  role = resolved_role(store)

  raw = @stdin.read
  payload =
    if fetch_name
      result =
        begin
          Timeout.timeout(Textus::Application::Write::RefreshWorker::FETCH_TIMEOUT_SECONDS) do
            store.rpc.invoke(:resolve_intake, fetch_name,
                             caps: nil,
                             config: { "bytes" => raw },
                             args: {})
          end
        rescue Timeout::Error
          raise UsageError.new(
            "fetch '#{fetch_name}' exceeded #{Textus::Application::Write::RefreshWorker::FETCH_TIMEOUT_SECONDS}s timeout",
          )
        end
      basename = key.split(".").last
      {
        "_meta" => {
          "name" => basename,
          "last_refreshed_at" => Time.now.utc.iso8601,
          "fetched_with" => fetch_name,
        }.merge(result[:_meta] || result["_meta"] || {}),
        "body" => result[:body] || result["body"] || "",
      }
    else
      JSON.parse(raw)
    end

  meta = payload["_meta"] || {}
  body = payload["body"] || ""
  if_etag = payload["if_etag"]
  result = store.session(role: role).put(key, meta: meta, body: body, if_etag: if_etag)
  emit(result.to_h_for_wire)
end