Class: R2::CLI

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

Overview

Command line interface of the project.

Acts as a minimal orchestrator: interprets the user's input, delegates the execution to the responsible components and presents the results.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: nil, storage: nil) ⇒ CLI

Initializes the CLI with its dependencies.

Dependencies are loaded lazily: they are only created when the first command actually uses them. This allows help and command listing to work without a configured environment.

Parameters:

  • configuration (Configuration) (defaults to: nil)

    application configuration

  • storage (Storage) (defaults to: nil)

    storage used in object operations



20
21
22
23
24
25
26
27
28
# File 'lib/r2/cli.rb', line 20

def initialize(
    *,
    configuration: nil,
    storage: nil
)
    super(*)
    @configuration = configuration
    @storage = storage
end

Class Method Details

.exit_on_failure?Boolean

Ensures that Thor exits with a non-zero status code when an operation fails.

Returns:

  • (Boolean)


32
33
34
# File 'lib/r2/cli.rb', line 32

def self.exit_on_failure?
    true
end

.start(*args) ⇒ Object

Starts the CLI and clearly presents domain errors not handled by the commands.

Errors raised before a command runs (such as missing configuration) are caught here, shown on the error output and terminate the CLI with status code 1, avoiding stack traces.



42
43
44
45
46
47
# File 'lib/r2/cli.rb', line 42

def self.start(*args)
    super
rescue Errors::Error => e
    warn "Error: #{e.message}"
    exit 1
end

Instance Method Details

#delete(file) ⇒ Object

Deletes a file from the configured Cloudflare R2 bucket.

The operation is considered successful when the storage completes the request without errors.

Parameters:

  • file (String)

    name of the file to delete



94
95
96
97
98
99
100
# File 'lib/r2/cli.rb', line 94

def delete(file)
    storage.delete(key: file)
    puts "File deleted successfully: #{file}"
rescue Errors::Error => e
    warn "Error: #{e.message}"
    exit 1
end

#listObject

Lists the files stored in the configured Cloudflare R2 bucket.



113
114
115
116
117
118
119
120
121
122
# File 'lib/r2/cli.rb', line 113

def list
    files = storage.list

    files.each do |file|
        puts file
    end
rescue Errors::Error => e
    warn "Error: #{e.message}"
    exit 1
end

#upload(file) ⇒ Object

Uploads an image to the configured Cloudflare R2 bucket.

The object key in the bucket will be the name of the given file.

Parameters:

  • file (String)

    path of the file to upload



67
68
69
70
71
72
73
74
75
76
# File 'lib/r2/cli.rb', line 67

def upload(file)
    body = open_file(file)
    storage.upload(key: File.basename(file), body: body)
    puts "Image uploaded successfully: #{File.basename(file)}"
rescue Errors::Error => e
    warn "Error: #{e.message}"
    exit 1
ensure
    body&.close
end