Class: Ruborg::CLI
- Inherits:
-
Thor
- Object
- Thor
- Ruborg::CLI
- 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
- #backup ⇒ Object
- #catalog ⇒ Object
- #check ⇒ Object
- #info ⇒ Object
- #init(repository_path) ⇒ Object
-
#initialize(*args) ⇒ CLI
constructor
A new instance of CLI.
- #list ⇒ Object
- #lock ⇒ Object
- #metadata(archive_name) ⇒ Object
- #restore(archive_name) ⇒ Object
- #validate(type) ⇒ Object
- #version ⇒ Object
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 = [:log] unless log_path # Try to load config to get log_file setting config_path = [: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
#backup ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/ruborg/cli.rb', line 56 def backup @logger.info("Starting backup operation with config: #{[:config]}") config = Config.new([:config]) backup_repositories(config) rescue Error => e @logger.error("Backup failed: #{e.}") raise end |
#catalog ⇒ Object
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([:config]) raise ConfigError, "Please specify --repository" unless [:repository] repo_config = config.get_repository([:repository]) raise ConfigError, "Repository '#{[: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 [:stats] print_catalog_stats(cat.stats, [:json]) elsif [:search] results = cat.search([:search]) print_catalog_entries(results, [:json]) else print_catalog_entries(cat.list, [:json]) end @logger.info("Catalog query on repository '#{merged_config["name"]}'") rescue Error => e @logger.error("Catalog failed: #{e.}") raise end |
#check ⇒ Object
456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/ruborg/cli.rb', line 456 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 |
#info ⇒ Object
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([:config]) # If no repository specified, show summary of all repositories unless [:repository] show_repositories_summary(config) return end repo_config = config.get_repository([:repository]) raise ConfigError, "Repository '#{[: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.}") 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([:passphrase], [: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.}") raise end |
#list ⇒ Object
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([:config]) raise ConfigError, "Please specify --repository" unless [:repository] repo_config = config.get_repository([:repository]) raise ConfigError, "Repository '#{[: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 [:archive] @logger.info("Listing files in archive: #{[:archive]}") repo.list_archive([: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.}") raise end |
#lock ⇒ Object
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 430 431 432 433 434 |
# File 'lib/ruborg/cli.rb', line 384 def lock config = Config.new([:config]) raise ConfigError, "Please specify --repository" unless [:repository] raise ConfigError, "Use --break or --force, not both" if [:break] && [:force] repo_config = config.get_repository([:repository]) raise ConfigError, "Repository '#{[: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) repo_locked = repo.locked? cache_locked = repo.cache_locked? unless repo_locked || cache_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"]}):" warn " Repository lock : #{repo_locked ? "LOCKED" : "clear"}" warn " Cache lock : #{cache_locked ? "LOCKED" : "clear"}" @logger.warn("Lock detected on '#{repo_config["name"]}' — repo=#{repo_locked}, cache=#{cache_locked}") unless [:break] || [:force] warn " Run with --break --yes (via borg) or --force --yes (direct removal)." exit 1 end unless [:yes] warn " Add --yes to confirm." exit 1 end if [: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.}") raise end |
#metadata(archive_name) ⇒ Object
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 507 508 509 510 511 |
# File 'lib/ruborg/cli.rb', line 473 def (archive_name) @logger.info("Getting metadata for archive: #{archive_name}") config = Config.new([:config]) raise ConfigError, "Please specify --repository" unless [:repository] repo_config = config.get_repository([:repository]) raise ConfigError, "Repository '#{[: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: [: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.}") 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 = [:path] ? "#{[:path]} from #{archive_name}" : archive_name @logger.info("Restoring #{restore_target} to #{[:destination]}") config = Config.new([:config]) raise ConfigError, "Please specify --repository" unless [:repository] repo_config = config.get_repository([:repository]) raise ConfigError, "Repository '#{[: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: [:destination], path: [:path]) @logger.info("Successfully restored #{restore_target} to #{[:destination]}") if [:path] puts "Restored #{[:path]} from #{archive_name} to #{[:destination]}" else puts "Archive restored to #{[:destination]}" end rescue Error => e @logger.error("Failed to restore archive: #{e.}") 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 |
#version ⇒ Object
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/ruborg/cli.rb', line 437 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.}") end end |