Module: Lich::Common::CLI::CLIOrchestration

Defined in:
documented/common/cli/cli_orchestration.rb

Overview

Provides orchestration for CLI commands in the Lich application

Examples:

Executing CLI commands

Lich::Common::CLI::CLIOrchestration.execute

Class Method Summary collapse

Class Method Details

.check_conversion_needed_for_loginvoid

This method returns an undefined value.

Checks if conversion is required before login.

Examples:

Checking conversion before login

Lich::Common::CLI::CLIOrchestration.

Raises:

  • (SystemExit)

    if conversion is needed



50
51
52
53
54
55
56
# File 'documented/common/cli/cli_orchestration.rb', line 50

def self.
  # Check if conversion is required
  if Lich::Common::CLI::CLIConversion.conversion_needed?(DATA_DIR)
    Lich::Common::CLI::CLIConversion.print_conversion_help_message
    exit 1
  end
end

.executevoid

This method returns an undefined value.

Executes the CLI commands based on the provided arguments.

Examples:

Executing commands

Lich::Common::CLI::CLIOrchestration.execute


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'documented/common/cli/cli_orchestration.rb', line 20

def self.execute
  ARGV.each do |arg|
    case arg
    when /^--change-account-password$/, /^-cap$/
      
    when /^--add-account$/, /^-aa$/
      
    when /^--change-master-password$/, /^-cmp$/
      handle_change_master_password
    when /^--recover-master-password$/, /^-rmp$/
      handle_recover_master_password
    when /^--convert-entries$/
      handle_convert_entries
    when /^--change-encryption-mode$/, /^-cem$/
      handle_change_encryption_mode
    end
  end

  # Check for conversion needed before login attempt
  # This is not an early-exit operation - it detects a precondition for login
  if ARGV.include?('--login')
    
  end
end

.handle_add_accountInteger

Handles the addition of a new account.

Examples:

Adding a new account

Lich::Common::CLI::CLIOrchestration.

Parameters:

  • account (String)

    The account to be added

  • password (String)

    The password for the new account

  • frontend (String, nil)

    The optional frontend for the account

Returns:

  • (Integer)

    The exit status of the operation

Raises:

  • (SystemExit)

    if required arguments are missing



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'documented/common/cli/cli_orchestration.rb', line 89

def self.
  idx = ARGV.index { |a| a =~ /^--add-account$|^-aa$/ }
   = ARGV[idx + 1]
  password = ARGV[idx + 2]

  if .nil? || password.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing required arguments'
    $stdout.puts "Usage: ruby #{lich_script} --add-account ACCOUNT PASSWORD [--frontend FRONTEND]"
    $stdout.puts "   or: ruby #{lich_script} -aa ACCOUNT PASSWORD [--frontend FRONTEND]"
    exit 1
  end

  frontend = ARGV[ARGV.index('--frontend') + 1] if ARGV.include?('--frontend')
  exit Lich::Common::CLI::PasswordManager.(, password, frontend)
end

.handle_change_account_passwordInteger

Handles the change of an account password.

Examples:

Changing an account password

Lich::Common::CLI::CLIOrchestration.

Parameters:

  • account (String)

    The account whose password is to be changed

  • new_password (String)

    The new password for the account

Returns:

  • (Integer)

    The exit status of the operation

Raises:

  • (SystemExit)

    if required arguments are missing



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'documented/common/cli/cli_orchestration.rb', line 65

def self.
  idx = ARGV.index { |a| a =~ /^--change-account-password$|^-cap$/ }
   = ARGV[idx + 1]
  new_password = ARGV[idx + 2]

  if .nil? || new_password.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing required arguments'
    $stdout.puts "Usage: ruby #{lich_script} --change-account-password ACCOUNT NEWPASSWORD"
    $stdout.puts "   or: ruby #{lich_script} -cap ACCOUNT NEWPASSWORD"
    exit 1
  end

  exit Lich::Common::CLI::PasswordManager.(, new_password)
end

.handle_change_encryption_modeInteger

Handles the change of the encryption mode.

Examples:

Changing the encryption mode

Lich::Common::CLI::CLIOrchestration.handle_change_encryption_mode

Parameters:

  • mode_arg (String)

    The new encryption mode to set

  • master_password (String, nil)

    The optional master password for enhanced mode

Returns:

  • (Integer)

    The exit status of the operation

Raises:

  • (SystemExit)

    if required arguments are missing



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'documented/common/cli/cli_orchestration.rb', line 206

def self.handle_change_encryption_mode
  idx = ARGV.index { |a| a =~ /^--change-encryption-mode$|^-cem$/ }
  mode_arg = ARGV[idx + 1]

  if mode_arg.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing encryption mode'
    $stdout.puts "Usage: ruby #{lich_script} --change-encryption-mode MODE [--master-password PASSWORD]"
    $stdout.puts "       ruby #{lich_script} -cem MODE [-mp PASSWORD]"
    $stdout.puts 'Modes: plaintext, standard, enhanced'
    exit 1
  end

  new_mode = mode_arg.to_sym

  # Check for optional master password (for Enhanced mode, if automating)
  mp_index = ARGV.index('--master-password') || ARGV.index('-mp')
  master_password = ARGV[mp_index + 1] if mp_index

  exit Lich::Common::CLI::EncryptionModeChange.change_mode(new_mode, master_password)
end

.handle_change_master_passwordInteger

Handles the change of the master password.

Examples:

Changing the master password

Lich::Common::CLI::CLIOrchestration.handle_change_master_password

Parameters:

  • old_password (String)

    The current master password

  • new_password (String, nil)

    The new master password (optional)

Returns:

  • (Integer)

    The exit status of the operation

Raises:

  • (SystemExit)

    if required arguments are missing



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'documented/common/cli/cli_orchestration.rb', line 113

def self.handle_change_master_password
  idx = ARGV.index { |a| a =~ /^--change-master-password$|^-cmp$/ }
  old_password = ARGV[idx + 1]
  new_password = ARGV[idx + 2]

  if old_password.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing required arguments'
    $stdout.puts "Usage: ruby #{lich_script} --change-master-password OLDPASSWORD [NEWPASSWORD]"
    $stdout.puts "   or: ruby #{lich_script} -cmp OLDPASSWORD [NEWPASSWORD]"
    $stdout.puts 'Note: If NEWPASSWORD is not provided, you will be prompted for confirmation'
    exit 1
  end

  exit Lich::Common::CLI::PasswordManager.change_master_password(old_password, new_password)
end

.handle_convert_entriesInteger

Handles the conversion of entries to a specified encryption mode.

Examples:

Converting entries

Lich::Common::CLI::CLIOrchestration.handle_convert_entries

Parameters:

  • encryption_mode_str (String)

    The encryption mode to convert to

Returns:

  • (Integer)

    The exit status of the operation

Raises:

  • (SystemExit)

    if required arguments are missing or invalid



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'documented/common/cli/cli_orchestration.rb', line 149

def self.handle_convert_entries
  idx = ARGV.index('--convert-entries')
  encryption_mode_str = ARGV[idx + 1]

  if encryption_mode_str.nil?
    lich_script = File.join(LICH_DIR, 'lich.rbw')
    $stdout.puts 'error: Missing required argument'
    $stdout.puts "Usage: ruby #{lich_script} --convert-entries [plaintext|standard|enhanced]"
    exit 1
  end

  unless %w[plaintext standard enhanced].include?(encryption_mode_str)
    $stdout.puts "error: Invalid encryption mode: #{encryption_mode_str}"
    $stdout.puts 'Valid modes: plaintext, standard, enhanced'
    exit 1
  end

  # For enhanced mode, prompt for master password and store in keychain before conversion
  # This way migrate_from_legacy will find it in keychain and not try to show GUI dialog
  if encryption_mode_str == 'enhanced'
    master_password = Lich::Common::CLI::PasswordManager.prompt_and_confirm_password('Enter new master password for enhanced encryption')
    if master_password.nil?
      puts 'error: Master password creation cancelled'
      exit 1
    end

    # Store password in keychain so ensure_master_password_exists finds it
    require_relative '../gui/master_password_manager'
    stored = Lich::Common::GUI::MasterPasswordManager.store_master_password(master_password)
    unless stored
      puts 'error: Failed to store master password in keychain'
      exit 1
    end
  end

  # Perform conversion
  success = Lich::Common::CLI::CLIConversion.convert(
    DATA_DIR,
    encryption_mode_str
  )

  if success
    $stdout.puts 'Conversion completed successfully!'
    exit 0
  else
    $stdout.puts 'Conversion failed. Please check the logs for details.'
    exit 1
  end
end

.handle_recover_master_passwordInteger

Handles the recovery of the master password.

Examples:

Recovering the master password

Lich::Common::CLI::CLIOrchestration.handle_recover_master_password

Parameters:

  • new_password (String, nil)

    The new master password (optional)

Returns:

  • (Integer)

    The exit status of the operation



135
136
137
138
139
140
141
# File 'documented/common/cli/cli_orchestration.rb', line 135

def self.handle_recover_master_password
  idx = ARGV.index { |a| a =~ /^--recover-master-password$|^-rmp$/ }
  new_password = ARGV[idx + 1]

  # new_password is optional - if not provided, user will be prompted interactively
  exit Lich::Common::CLI::PasswordManager.recover_master_password(new_password)
end