Module: MercuryBanking::CLI::Base

Included in:
Financials::FinancialsCommand, Main
Defined in:
lib/mercury_banking/cli/base.rb

Overview

Base module for CLI functionality

Instance Method Summary collapse

Instance Method Details

#api_keyObject

Get the API key from the configuration file (legacy method)



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mercury_banking/cli/base.rb', line 61

def api_key
  config_path = File.join(Dir.home, '.mercury', 'config')
  key_path = File.join(Dir.home, '.mercury', 'key')

  unless File.exist?(config_path) && File.exist?(key_path)
    puts "Error: Mercury API key not found. Please run 'mercury configure' first."
    exit 1
  end

  File.read(key_path).strip
end

#command_exists?(command) ⇒ Boolean

Check if a command exists in the system

Returns:

  • (Boolean)


85
86
87
# File 'lib/mercury_banking/cli/base.rb', line 85

def command_exists?(command)
  system("which #{command} > /dev/null 2>&1")
end

#get_api_keyObject

Get API key by decrypting the stored encrypted key



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mercury_banking/cli/base.rb', line 30

def get_api_key
  config_dir = File.join(Dir.home, '.mercury-banking')
  key_config_path = File.join(config_dir, 'key_config.json')
  api_key_path = File.join(config_dir, 'api_key.enc')

  unless File.exist?(key_config_path) && File.exist?(api_key_path)
    return nil
  end

  # Load the cipher configuration
  cipher_config = JSON.parse(File.read(key_config_path))
  
  # Recreate the cipher
  cipher = SymmetricEncryption::Cipher.new(
    key: Base64.strict_decode64(cipher_config['key']),
    iv: Base64.strict_decode64(cipher_config['iv']),
    cipher_name: cipher_config['cipher_name']
  )

  # Set the cipher as the primary one
  SymmetricEncryption.cipher = cipher
  
  # Load and decrypt the API key
  encrypted_key = File.read(api_key_path)
  cipher.decrypt(encrypted_key)
rescue StandardError => e
  puts "Error decrypting API key: #{e.message}" unless options[:json]
  nil
end

#log_transfer(from, to, amount, note, status) ⇒ Object

Log transfer details



74
75
76
77
78
79
80
81
82
# File 'lib/mercury_banking/cli/base.rb', line 74

def log_transfer(from, to, amount, note, status)
  log_dir = File.join(Dir.home, '.mercury-banking', 'logs')
  FileUtils.mkdir_p(log_dir)

  log_file = File.join(log_dir, 'transfers.log')
  File.open(log_file, 'a') do |f|
    f.puts "#{Time.now.iso8601},\"#{from}\",\"#{to}\",\"#{amount}\",\"#{note}\",\"#{status}\""
  end
end

#with_api_clientObject

Get API client with error handling



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mercury_banking/cli/base.rb', line 14

def with_api_client
  api_key = get_api_key
  raise "API key not found. Please run 'mercury set_key' first." unless api_key

  client = MercuryBanking::API.new(api_key)
  yield client
rescue StandardError => e
  if options[:json]
    puts JSON.pretty_generate({ 'error' => e.message })
  else
    puts "Error: #{e.message}"
  end
  exit 1
end