Sha256: 9c3e1d0e4b6c92890253de335f704a9ad86ff841be0b9084efebb2c1a66ca335

Contents?: true

Size: 1.66 KB

Versions: 14

Compression:

Stored size: 1.66 KB

Contents

require "pathname"

module Rbnotes::Commands

  ##
  # Writes out a given note into a specified file.  The file will be
  # created in the current working directory unless an absolute path
  # is specified as a filename.
  #
  # When no argument was passed, would try to read a timestamp string
  # from the standard input.

  class Export < Command

    def description             # :nodoc:
      "Write out a note into a file"
    end

    #
    # :call-seq:
    #     execute([a String as timestring], Rbnotes::Conf or Hash) -> nil

    def execute(args, conf)
      stamp = Rbnotes.utils.read_timestamp(args)

      repo = Textrepo.init(conf)
      begin
        content = repo.read(stamp)
      rescue Textrepo::MissingTimestampError => _
        raise MissingTimestampError, stamp
      end

      pathname = Pathname.new(args.shift || "#{stamp}.md")
      pathname.parent.mkpath
      pathname.open("w"){ |f| f.puts content }
      puts "Export a note [%s] into a file [%s]" % [stamp, pathname]
    end

    def help                    # :nodoc:
      puts <<HELP
usage:
    #{Rbnotes::NAME} export [TIMESTAMP [FILENAME]]

Write out a given note into a specified file.  TIMESTAMP must be a
fully qualified one, such "20201108141600", or "20201108141600_012" if
it has a suffix.  FILENAME is optional.  When it omitted, the filename
would be a timestamp string with ".md" as its extension, such
"20201108141600.md"

The file will be created into the current working directory unless an
absolute path is specified as FILENAME.

When no argument was passed, it would try to read a timestamp string
from the standard input.  Then, FILENAME would be regarded as omitted.
HELP
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rbnotes-0.4.19 lib/rbnotes/commands/export.rb
rbnotes-0.4.18 lib/rbnotes/commands/export.rb
rbnotes-0.4.17 lib/rbnotes/commands/export.rb
rbnotes-0.4.16 lib/rbnotes/commands/export.rb
rbnotes-0.4.15 lib/rbnotes/commands/export.rb
rbnotes-0.4.14 lib/rbnotes/commands/export.rb
rbnotes-0.4.13 lib/rbnotes/commands/export.rb
rbnotes-0.4.12 lib/rbnotes/commands/export.rb
rbnotes-0.4.11 lib/rbnotes/commands/export.rb
rbnotes-0.4.10 lib/rbnotes/commands/export.rb
rbnotes-0.4.9 lib/rbnotes/commands/export.rb
rbnotes-0.4.8 lib/rbnotes/commands/export.rb
rbnotes-0.4.7 lib/rbnotes/commands/export.rb
rbnotes-0.4.6 lib/rbnotes/commands/export.rb