Class: Ruborg::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/ruborg/cli.rb

Overview

Command-line interface for ruborg

Defined Under Namespace

Classes: BackupConfig

Constant Summary collapse

DEFAULT_LOCK_WAIT =
300

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruborg/cli.rb', line 14

def initialize(*args)
  super
  # Priority: CLI option > config file > default
  log_path = options[:log]
  unless log_path
    # Try to load config to get log_file setting
    config_path = options[:config] || "ruborg.yml"
    if File.exist?(config_path)
      config_data = begin
        YAML.safe_load_file(config_path, permitted_classes: [Symbol], aliases: true)
      rescue StandardError
        {}
      end
      log_path = config_data["log_file"]
    end
  end

  # Validate log path if provided
  log_path = validate_log_path(log_path) if log_path

  @logger = RuborgLogger.new(log_file: log_path)
end

Instance Method Details

#backupObject



56
57
58
59
60
61
62
63
# File 'lib/ruborg/cli.rb', line 56

def backup
  @logger.info("Starting backup operation with config: #{options[:config]}")
  config = Config.new(options[:config])
  backup_repositories(config)
rescue Error => e
  @logger.error("Backup failed: #{e.message}")
  raise
end

#catalogObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/ruborg/cli.rb', line 351

def catalog
  config = Config.new(options[:config])

  raise ConfigError, "Please specify --repository" unless options[:repository]

  repo_config = config.get_repository(options[:repository])
  raise ConfigError, "Repository '#{options[:repository]}' not found" unless repo_config

  global_settings = config.global_settings
  merged_config = global_settings.merge(repo_config)
  cat = Catalog.new(repo_config["path"])

  if options[:stats]
    print_catalog_stats(cat.stats, options[:json])
  elsif options[:search]
    results = cat.search(options[:search])
    print_catalog_entries(results, options[:json])
  else
    print_catalog_entries(cat.list, options[:json])
  end

  @logger.info("Catalog query on repository '#{merged_config["name"]}'")
rescue Error => e
  @logger.error("Catalog failed: #{e.message}")
  raise
end

#checkObject



451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/ruborg/cli.rb', line 451

def check
  puts "\n⚠️  DEPRECATED COMMAND"
  puts "══════════════════════════════════════════════════════════════════\n\n"
  puts "The 'ruborg check' command has been renamed for consistency.\n"
  puts "Please use: ruborg validate repo\n\n"
  puts "Examples:"
  puts "  ruborg validate repo --repository documents"
  puts "  ruborg validate repo --all"
  puts "  ruborg validate repo --repository documents --verify-data\n\n"
  puts "══════════════════════════════════════════════════════════════════\n"

  @logger.warn("Deprecated command 'check' was called. User should use 'validate repo' instead.")
  exit 1
end

#infoObject



142
143
144
145
146
147
148
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
# File 'lib/ruborg/cli.rb', line 142

def info
  @logger.info("Retrieving repository information")
  config = Config.new(options[:config])

  # If no repository specified, show summary of all repositories
  unless options[:repository]
    show_repositories_summary(config)
    return
  end

  repo_config = config.get_repository(options[:repository])
  raise ConfigError, "Repository '#{options[:repository]}' not found" unless repo_config

  global_settings = config.global_settings
  merged_config = global_settings.merge(repo_config)
  passphrase = fetch_passphrase_for_repo(merged_config)
  repo = build_repo(repo_config["path"], merged_config, passphrase)

  # Auto-initialize repository if configured
  # Use strict boolean checking: only true enables, everything else disables
  auto_init = merged_config["auto_init"]
  auto_init = false unless auto_init == true
  if auto_init && !repo.exists?
    @logger.info("Auto-initializing repository at #{repo_config["path"]}")
    repo.create
    puts "Repository auto-initialized at #{repo_config["path"]}"
  end

  repo.info
  @logger.info("Successfully retrieved repository information")
rescue Error => e
  @logger.error("Failed to get repository info: #{e.message}")
  raise
end

#init(repository_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruborg/cli.rb', line 40

def init(repository_path)
  @logger.info("Initializing repository at #{repository_path}")
  passphrase = get_passphrase(options[:passphrase], options[:passbolt_id])
  repo = Repository.new(repository_path, passphrase: passphrase, logger: @logger)
  repo.create
  @logger.info("Repository successfully initialized at #{repository_path}")
  puts "Repository initialized at #{repository_path}"
rescue Error => e
  @logger.error("Failed to initialize repository: #{e.message}")
  raise
end

#listObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ruborg/cli.rb', line 67

def list
  config = Config.new(options[:config])

  raise ConfigError, "Please specify --repository" unless options[:repository]

  repo_config = config.get_repository(options[:repository])
  raise ConfigError, "Repository '#{options[:repository]}' not found" unless repo_config

  global_settings = config.global_settings
  merged_config = global_settings.merge(repo_config)
  validate_hostname(merged_config)
  passphrase = fetch_passphrase_for_repo(merged_config)
  repo = build_repo(repo_config["path"], merged_config, passphrase)

  # Auto-initialize repository if configured
  # Use strict boolean checking: only true enables, everything else disables
  auto_init = merged_config["auto_init"]
  auto_init = false unless auto_init == true
  if auto_init && !repo.exists?
    @logger.info("Auto-initializing repository at #{repo_config["path"]}")
    repo.create
    puts "Repository auto-initialized at #{repo_config["path"]}"
  end

  if options[:archive]
    @logger.info("Listing files in archive: #{options[:archive]}")
    repo.list_archive(options[:archive])
    @logger.info("Successfully listed files in archive")
  else
    @logger.info("Listing archives in repository")
    repo.list
    @logger.info("Successfully listed archives")
  end
rescue Error => e
  @logger.error("Failed to list: #{e.message}")
  raise
end

#lockObject



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/ruborg/cli.rb', line 384

def lock
  config = Config.new(options[:config])

  raise ConfigError, "Please specify --repository" unless options[:repository]
  raise ConfigError, "Use --break or --force, not both" if options[:break] && options[:force]

  repo_config = config.get_repository(options[:repository])
  raise ConfigError, "Repository '#{options[:repository]}' not found" unless repo_config

  global_settings = config.global_settings
  merged_config = global_settings.merge(repo_config)
  passphrase = fetch_passphrase_for_repo(merged_config)
  repo = build_repo(repo_config["path"], merged_config, passphrase)

  unless repo.locked?
    puts "No lock found for repository '#{repo_config["name"]}'"
    @logger.info("Lock check: no lock found for '#{repo_config["name"]}'")
    return
  end

  warn "Lock detected on repository '#{repo_config["name"]}' (#{repo_config["path"]})"
  @logger.warn("Lock detected on repository '#{repo_config["name"]}'")

  unless options[:break] || options[:force]
    warn "  Run with --break --yes (via borg) or --force --yes (direct removal)."
    exit 1
  end

  unless options[:yes]
    warn "  Add --yes to confirm."
    exit 1
  end

  if options[:force]
    removed = repo.force_break_lock
    puts "Force-removed lock files for '#{repo_config["name"]}': #{removed.join(", ")}"
    @logger.info("Force-removed lock files for '#{repo_config["name"]}'")
  else
    repo.break_lock
    puts "Lock broken for repository '#{repo_config["name"]}'"
    @logger.info("Lock broken for repository '#{repo_config["name"]}'")
  end
rescue Error => e
  @logger.error("Lock command failed: #{e.message}")
  raise
end

#metadata(archive_name) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/ruborg/cli.rb', line 468

def (archive_name)
  @logger.info("Getting metadata for archive: #{archive_name}")
  config = Config.new(options[:config])

  raise ConfigError, "Please specify --repository" unless options[:repository]

  repo_config = config.get_repository(options[:repository])
  raise ConfigError, "Repository '#{options[:repository]}' not found" unless repo_config

  global_settings = config.global_settings
  merged_config = global_settings.merge(repo_config)
  validate_hostname(merged_config)
  passphrase = fetch_passphrase_for_repo(merged_config)
  repo = build_repo(repo_config["path"], merged_config, passphrase)

  raise BorgError, "Repository does not exist at #{repo_config["path"]}" unless repo.exists?

  # Get file metadata
   = repo.(archive_name, file_path: options[:file])

  # Display metadata
  puts "\n═══════════════════════════════════════════════════════════════"
  puts "  FILE METADATA"
  puts "═══════════════════════════════════════════════════════════════\n\n"
  puts "Archive: #{archive_name}"
  puts "File: #{["path"]}"
  puts "Size: #{format_size(["size"])}"
  puts "Modified: #{["mtime"]}"
  puts "Mode: #{["mode"]}"
  puts "User: #{["user"]}"
  puts "Group: #{["group"]}"
  puts "Type: #{["type"]}"
  puts ""

  @logger.info("Successfully retrieved metadata for #{["path"]}")
rescue Error => e
  @logger.error("Failed to get metadata: #{e.message}")
  raise
end

#restore(archive_name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ruborg/cli.rb', line 108

def restore(archive_name)
  restore_target = options[:path] ? "#{options[:path]} from #{archive_name}" : archive_name
  @logger.info("Restoring #{restore_target} to #{options[:destination]}")
  config = Config.new(options[:config])

  raise ConfigError, "Please specify --repository" unless options[:repository]

  repo_config = config.get_repository(options[:repository])
  raise ConfigError, "Repository '#{options[:repository]}' not found" unless repo_config

  global_settings = config.global_settings
  merged_config = global_settings.merge(repo_config)
  validate_hostname(merged_config)
  passphrase = fetch_passphrase_for_repo(merged_config)
  repo = build_repo(repo_config["path"], merged_config, passphrase)

  # Create backup config wrapper for compatibility
  backup_config = BackupConfig.new(repo_config, merged_config)
  backup = Backup.new(repo, config: backup_config, logger: @logger)

  backup.extract(archive_name, destination: options[:destination], path: options[:path])
  @logger.info("Successfully restored #{restore_target} to #{options[:destination]}")

  if options[:path]
    puts "Restored #{options[:path]} from #{archive_name} to #{options[:destination]}"
  else
    puts "Archive restored to #{options[:destination]}"
  end
rescue Error => e
  @logger.error("Failed to restore archive: #{e.message}")
  raise
end

#validate(type) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/ruborg/cli.rb', line 180

def validate(type)
  case type
  when "config"
    validate_config_implementation
  when "repo"
    validate_repo_implementation
  else
    raise ConfigError, "Invalid validation type: #{type}. Use 'config' or 'repo'"
  end
end

#versionObject



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/ruborg/cli.rb', line 432

def version
  require_relative "version"
  puts "ruborg #{Ruborg::VERSION}"
  @logger.info("Version checked: #{Ruborg::VERSION}")

  begin
    borg_version = Repository.borg_version
    borg_path = Repository.borg_path
    puts "borg #{borg_version} (#{borg_path})"
    @logger.info("Borg version: #{borg_version}, path: #{borg_path}")
  rescue BorgError => e
    puts "borg: not found or not executable"
    @logger.warn("Could not determine Borg version: #{e.message}")
  end
end