Module: Lich::Common::CLI::CLIConversion

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

Class Method Summary collapse

Class Method Details

.conversion_needed?(data_dir) ⇒ Boolean

Determines if conversion is needed based on the presence of entry.dat and absence of entry.yaml.

Parameters:

  • data_dir (String)

    the directory containing the data files

Returns:

  • (Boolean)

    true if conversion is needed, false otherwise



13
14
15
16
17
18
# File 'documented/common/cli/cli_conversion.rb', line 13

def self.conversion_needed?(data_dir)
  dat_file = File.join(data_dir, 'entry.dat')
  yaml_file = Lich::Common::Authentication::EntryStore.yaml_file_path(data_dir)

  File.exist?(dat_file) && !File.exist?(yaml_file)
end

.convert(data_dir, encryption_mode) ⇒ Boolean

Converts the entry.dat file to the new format based on the specified encryption mode.

Parameters:

  • data_dir (String)

    the directory containing the data files

  • encryption_mode (String)

    the mode of encryption to use for the conversion

Returns:

  • (Boolean)

    true if conversion was successful, false otherwise

Raises:

  • StandardError if an error occurs during conversion



26
27
28
29
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
# File 'documented/common/cli/cli_conversion.rb', line 26

def self.convert(data_dir, encryption_mode)
  # Normalize encryption_mode to symbol if string is passed
  mode = encryption_mode.to_sym

  # Validate preconditions
  dat_file = File.join(data_dir, 'entry.dat')
  yaml_file = Lich::Common::Authentication::EntryStore.yaml_file_path(data_dir)

  unless File.exist?(dat_file)
    Lich.log "error: entry.dat not found at #{dat_file}"
    return false
  end

  if File.exist?(yaml_file)
    Lich.log "error: entry.yaml already exists at #{yaml_file}"
    return false
  end

  # Delegate to EntryStore for the actual conversion
  # For enhanced mode, migrate_from_legacy will prompt user to create master password
  result = Lich::Common::Authentication::EntryStore.migrate_from_legacy(data_dir, encryption_mode: mode)

  unless result
    Lich.log "error: EntryStore.migrate_from_legacy returned false"
  end

  result
rescue StandardError => e
  Lich.log "error: Conversion failed: #{e.class}: #{e.message}"
  Lich.log "error: Backtrace: #{e.backtrace.join("\n  ")}"
  false
end

This method returns an undefined value.

Prints a help message to the standard output regarding the conversion of saved entries.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'documented/common/cli/cli_conversion.rb', line 62

def self.print_conversion_help_message
  lich_script = File.join(LICH_DIR, 'lich.rbw')

  $stdout.puts "\n" + '=' * 80
  $stdout.puts "Saved entries conversion required"
  $stdout.puts '=' * 80
  $stdout.puts "\nYour login entries need to be converted to the new format."
  $stdout.puts "\nRun one of these commands:\n\n"

  $stdout.puts "For no encryption (least secure):"
  $stdout.puts "  ruby #{lich_script} --convert-entries plaintext\n\n"

  $stdout.puts "For account-based encryption (standard):"
  $stdout.puts "  ruby #{lich_script} --convert-entries standard\n\n"

  $stdout.puts "For master-password encryption (recommended):"
  $stdout.puts "  ruby #{lich_script} --convert-entries enhanced\n\n"

  $stdout.puts '=' * 80 + "\n"
end