Sha256: 1c28d0aaecba1308442cc198ce0686b93fda7f42ea305e1007a3916bba2d348e

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

class Rustic::CommandBuilder
  class UnknownPasswordMethodError < Rustic::Error; end
  class UnknownCommandError < Rustic::Error; end
  class MissingConfigError < Rustic::Error; end
  class MalformedConfigError < Rustic::Error; end

  def initialize(command, config)
    @command = command
    @config = config
  end

  def build
    @args = []
    @env_variables = {}

    add_repository_path!
    add_password!

    add_command!

    [[@config.restic_path, *@args], @env_variables] # TODO: properly handle spaces in paths
  end

  private

  def add_repository_path! = @args += ["-r", @config.repository]

  def add_password!
    return @env_variables.merge!("RESTIC_PASSWORD" => @config.password) if @config.password
    return @env_variables.merge!("RESTIC_PASSWORD_FILE" => @config.password_file) if @config.password_file

    raise UnknownPasswordMethodError
  end

  def add_command!
    command_class = Rustic::CommandBuilders.const_get(@command.capitalize)
    @args.concat(command_class.new(@config).build)
  rescue NameError
    raise UnknownCommandError, "Unknown command #{@command}"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rustic-0.3.0 lib/rustic/command_builder.rb