Module: KamalBackup::CLI::Helpers
- Included in:
- KamalBackup::CLI, CommandBase
- Defined in:
- lib/kamal_backup/cli.rb
Instance Method Summary collapse
- #accessory_name ⇒ Object
- #bridge ⇒ Object
- #command_env ⇒ Object
- #confirm!(message) ⇒ Object
- #deploy_snippet ⇒ Object
- #deployment_mode? ⇒ Boolean
- #direct_app ⇒ Object
- #exec_remote(argv) ⇒ Object
- #init_config_root ⇒ Object
- #local_app ⇒ Object
- #local_command_config ⇒ Object
- #local_preferences ⇒ Object
- #prompt_required(label) ⇒ Object
- #redactor ⇒ Object
- #shared_config_path ⇒ Object
- #shared_config_template ⇒ Object
- #write_init_file(path, contents) ⇒ Object
Instance Method Details
#accessory_name ⇒ Object
53 54 55 |
# File 'lib/kamal_backup/cli.rb', line 53 def accessory_name @accessory_name ||= bridge.accessory_name(preferred: local_preferences.accessory_name) end |
#bridge ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/kamal_backup/cli.rb', line 41 def bridge @bridge ||= KamalBridge.new( redactor: redactor, config_file: [:config_file], destination: [:destination] ) end |
#command_env ⇒ Object
14 15 16 |
# File 'lib/kamal_backup/cli.rb', line 14 def command_env CLI.command_env || ENV end |
#confirm!(message) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kamal_backup/cli.rb', line 66 def confirm!() return if [:yes] unless $stdin.tty? raise ConfigurationError, "confirmation required; rerun with --yes" end unless yes?("#{} [y/N]") raise ConfigurationError, "aborted" end end |
#deploy_snippet ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/kamal_backup/cli.rb', line 118 def deploy_snippet <<~YAML accessories: backup: image: ghcr.io/crmne/kamal-backup:#{VERSION} host: your-server.example.com env: clear: APP_NAME: your-app DATABASE_ADAPTER: postgres DATABASE_URL: postgres://your-app@your-db:5432/your_app_production BACKUP_PATHS: /data/storage RESTIC_REPOSITORY: s3:https://s3.example.com/your-app-backups RESTIC_INIT_IF_MISSING: "true" BACKUP_SCHEDULE_SECONDS: "86400" secret: - PGPASSWORD - RESTIC_PASSWORD - AWS_ACCESS_KEY_ID - AWS_SECRET_ACCESS_KEY volumes: - "your_app_storage:/data/storage:ro" YAML end |
#deployment_mode? ⇒ Boolean
49 50 51 |
# File 'lib/kamal_backup/cli.rb', line 49 def deployment_mode? ![:destination].to_s.strip.empty? || ![:config_file].to_s.strip.empty? end |
#direct_app ⇒ Object
22 23 24 |
# File 'lib/kamal_backup/cli.rb', line 22 def direct_app @direct_app ||= App.new(config: Config.new(env: command_env), redactor: redactor) end |
#exec_remote(argv) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/kamal_backup/cli.rb', line 57 def exec_remote(argv) result = bridge.execute_on_accessory( accessory_name: accessory_name, command: Shellwords.join(argv) ) print(result.stdout) result end |
#init_config_root ⇒ Object
91 92 93 94 |
# File 'lib/kamal_backup/cli.rb', line 91 def init_config_root config_file = [:config_file] || KamalBridge::DEFAULT_CONFIG_FILE File.dirname(File.(config_file)) end |
#local_app ⇒ Object
26 27 28 |
# File 'lib/kamal_backup/cli.rb', line 26 def local_app @local_app ||= App.new(config: local_command_config, redactor: redactor) end |
#local_command_config ⇒ Object
34 35 36 37 38 39 |
# File 'lib/kamal_backup/cli.rb', line 34 def local_command_config @local_command_config ||= begin defaults = deployment_mode? ? bridge.local_restore_defaults(accessory_name: accessory_name) : {} Config.new(env: command_env, defaults: defaults) end end |
#local_preferences ⇒ Object
30 31 32 |
# File 'lib/kamal_backup/cli.rb', line 30 def local_preferences @local_preferences ||= Config.new(env: command_env) end |
#prompt_required(label) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kamal_backup/cli.rb', line 78 def prompt_required(label) unless $stdin.tty? raise ConfigurationError, "#{label.downcase} is required; pass it on the command line" end value = ask("#{label}:").to_s.strip if value.empty? raise ConfigurationError, "#{label.downcase} is required" else value end end |
#redactor ⇒ Object
18 19 20 |
# File 'lib/kamal_backup/cli.rb', line 18 def redactor @redactor ||= Redactor.new(env: command_env) end |
#shared_config_path ⇒ Object
96 97 98 |
# File 'lib/kamal_backup/cli.rb', line 96 def shared_config_path File.join(init_config_root, "kamal-backup.yml") end |
#shared_config_template ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/kamal_backup/cli.rb', line 110 def shared_config_template <<~YAML # Shared defaults for kamal-backup in this app. # Set this when your accessory is not named "backup". accessory: backup YAML end |
#write_init_file(path, contents) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/kamal_backup/cli.rb', line 100 def write_init_file(path, contents) if File.exist?(path) say "Exists: #{path}", :yellow else FileUtils.mkdir_p(File.dirname(path)) File.write(path, contents) say "Created: #{path}", :green end end |