Class: Opdotenv::Exporter
- Inherits:
-
Object
- Object
- Opdotenv::Exporter
- Defined in:
- lib/opdotenv/exporter.rb
Class Method Summary collapse
- .escape_env(value) ⇒ Object
-
.export(path:, data:, field_type: nil, client: nil, env: ENV) ⇒ Object
Export data to 1Password Paths like "op://Vault/.env.development" or "op://Vault/config.json" create Secure Notes (format inferred from item name extension) Paths like "op://Vault/App" create/update item fields Format is inferred from item name extension: .env.*, *.json, *.yaml, *.yml.
- .infer_format_from_item(item) ⇒ Object
- .serialize_by_format(data, format) ⇒ Object
Class Method Details
.escape_env(value) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/opdotenv/exporter.rb', line 49 def self.escape_env(value) s = value.to_s return '""' if s.empty? if s.match?(/\s|["'#]/) '"' + s.gsub('"', '\\"') + '"' else s end end |
.export(path:, data:, field_type: nil, client: nil, env: ENV) ⇒ Object
Export data to 1Password Paths like "op://Vault/.env.development" or "op://Vault/config.json" create Secure Notes (format inferred from item name extension) Paths like "op://Vault/App" create/update item fields Format is inferred from item name extension: .env.*, *.json, *.yaml, *.yml
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/opdotenv/exporter.rb', line 10 def self.export(path:, data:, field_type: nil, client: nil, env: ENV) client ||= ClientFactory.create(env: env) vault, item = Loader.parse_op_path(path) # Extract item name and potential field name # Handle paths like "op://Vault/Item" or "op://Vault/Item Name/field" item_parts = item.split("/") item_name = item_parts.first # Check if path matches format patterns (Secure Note) or regular item (fields) if FormatInferrer.matches_format_pattern?(item_name) # Create Secure Note field_type ||= FormatInferrer.infer_from_name(item_name) || :dotenv content = serialize_by_format(data, field_type) client.item_create_note(vault: vault, title: item_name, notes: content) else # Create/update item with fields flat = data.transform_values(&:to_s) client.item_create_or_update_fields(vault: vault, item: item_name, fields: flat) end end |
.infer_format_from_item(item) ⇒ Object
32 33 34 |
# File 'lib/opdotenv/exporter.rb', line 32 def self.infer_format_from_item(item) FormatInferrer.infer_from_name(item) || :dotenv end |
.serialize_by_format(data, format) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/opdotenv/exporter.rb', line 36 def self.serialize_by_format(data, format) case format when :dotenv data.map { |k, v| "#{k}=#{escape_env(v)}" }.join("\n") + "\n" when :json JSON.pretty_generate(data) when :yaml, :yml YAML.dump(data) else raise ArgumentError, "Unsupported format: #{format}. Supported: :dotenv, :json, :yaml" end end |