Class: Opdotenv::OpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/opdotenv/op_client.rb

Defined Under Namespace

Classes: OpError

Constant Summary collapse

NOTES_PLAIN_FIELD =
"notesPlain"
SECURE_NOTE_CATEGORY =
"secure-note"
LOGIN_CATEGORY =
"LOGIN"

Instance Method Summary collapse

Constructor Details

#initialize(env: ENV, cli_path: nil) ⇒ OpClient

Returns a new instance of OpClient.



12
13
14
15
# File 'lib/opdotenv/op_client.rb', line 12

def initialize(env: ENV, cli_path: nil)
  @env = env
  @cli_path = cli_path || env["OP_CLI_PATH"] || env["OPDOTENV_CLI_PATH"] || "op"
end

Instance Method Details

#item_create_note(vault:, title:, notes:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/opdotenv/op_client.rb', line 29

def item_create_note(vault:, title:, notes:)
  # Create a Secure Note with given title and notesPlain
  # Use shell escaping to prevent injection
  args = [
    @cli_path, "item", "create",
    "--category", SECURE_NOTE_CATEGORY,
    "--title", title,
    "--vault", vault,
    "#{NOTES_PLAIN_FIELD}=#{notes}"
  ]
  capture(args)
end

#item_create_or_update_fields(vault:, item:, fields: {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opdotenv/op_client.rb', line 42

def item_create_or_update_fields(vault:, item:, fields: {})
  exists = item_exists?(item, vault: vault)
  if exists
    fields.each do |k, v|
      # Use shell escaping to prevent injection
      field_arg = "#{k}=#{v}"
      capture([@cli_path, "item", "edit", item, "--vault", vault, "--set", field_arg])
    end
  else
    args = [@cli_path, "item", "create", "--title", item, "--vault", vault]
    fields.each do |k, v|
      args += ["--set", "#{k}=#{v}"]
    end
    capture(args)
  end
end

#item_get(item, vault: nil) ⇒ Object



23
24
25
26
27
# File 'lib/opdotenv/op_client.rb', line 23

def item_get(item, vault: nil)
  args = [@cli_path, "item", "get", item, "--format", "json"]
  args += ["--vault", vault] if vault
  capture(args)
end

#read(path) ⇒ Object



17
18
19
20
21
# File 'lib/opdotenv/op_client.rb', line 17

def read(path)
  validate_path(path)
  out = capture([@cli_path, "read", path])
  out.strip
end