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

#emit, inherited, #initialize, needs_store?, option, options, #parse

Constructor Details

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

Instance Method Details

#call(store) ⇒ Object

rubocop:disable Metrics/AbcSize

Raises:



9
10
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
# File 'lib/textus/cli/verb/put.rb', line 9

def call(store) # rubocop:disable Metrics/AbcSize
  key = positional.shift or raise UsageError.new("put requires a key")
  raise UsageError.new("put requires --stdin in v1") unless use_stdin

  role = Role.resolve(flag: as_flag, env: ENV, root: store.root)

  raw = @stdin.read
  payload =
    if fetch_name
      callable = store.registry.rpc_callable(:fetch, fetch_name)
      result =
        begin
          Timeout.timeout(Textus::Refresh::FETCH_TIMEOUT_SECONDS) do
            callable.call(config: { "bytes" => raw }, store: Textus::Store::View.new(store), args: {})
          end
        rescue Timeout::Error
          raise UsageError.new(
            "fetch '#{fetch_name}' exceeded #{Textus::Refresh::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"] || result[:frontmatter] || result["frontmatter"] || {}),
        "body" => result[:body] || result["body"] || "",
      }
    else
      JSON.parse(raw)
    end

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