Class: Wikiwiki::CLI::Commands::Attachment::Get

Inherits:
Base
  • Object
show all
Defined in:
lib/wikiwiki/cli/commands/attachment/get.rb

Overview

Download attachment from a page

Instance Method Summary collapse

Instance Method Details

#call(page_name:, file_name:, directory: nil, force: false, out: $stdout, err: $stderr) ⇒ void

This method returns an undefined value.

Execute the get command

Parameters:

  • page_name (String)

    name of the page

  • file_name (String)

    name of the attachment file

  • directory (String, nil) (defaults to: nil)

    optional download directory

  • force (Boolean) (defaults to: false)

    whether to overwrite existing file

  • out (IO) (defaults to: $stdout)

    output stream

  • err (IO) (defaults to: $stderr)

    error stream



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wikiwiki/cli/commands/attachment/get.rb', line 25

def call(page_name:, file_name:, directory: nil, force: false, out: $stdout, err: $stderr, **)
  wiki = create_wiki(out:, err:, **)

  output_path = directory ? File.join(directory, file_name) : file_name

  if File.exist?(output_path) && !force
    raise ArgumentError, "File '#{output_path}' already exists. Use --force to overwrite."
  end

  attachment = wiki.attachment(page_name:, attachment_name: file_name)
  File.binwrite(output_path, attachment.content)

  say("Attachment '#{file_name}' (#{attachment.content.bytesize} bytes) saved to #{output_path}", out:, **)
end