class Rbnotes::Commands::Add

Adds a new note to the repository. If no options, a new timestamp is generated at the execution time, then it is attached to the note. If the timestamp has already existed in the repository, the command fails.

Accepts an option with `-t STAMP_PATTERN` (or `–timestamp`), a timestamp is generated according to `STAMP_PATTERN`.

STAMP_PATTERN could be one of followings:

"20201104172230_078" : full qualified timestamp string
"20201104172230"     : full qualified timestamp string (no suffix)
"202011041722"       : year, date and time (omit second part)
"11041722"           : date and time (omit year and second part)

Also accepts an option with `-f FILENAME` (or `–template-file`) to specify a template file which will be the initial content of a new note.

This command starts the external editor program to prepare text to store. The editor program will be searched in the following order:

1. conf[:editor] (conf is the 1st arg of execute method)
2. ENV["EDITOR"]
3. "nano"
4. "vi"

If none of the above editor is available, the command fails.

TEMPLATE FILE:

This command search a file as its template which will be the initial conent of a new note when an option or a setting in the configuration file (`:template`) is specified a template file.

Or, even if neither an option nor a setting about a template file is specified, it always searches the default directory to read a template file. The directory is:

- $XDG_CONIFG_HOME/rbnotes/templates (if $XDG_CONFIG_HOME is defined)

or

- $HOME/.config/rbnotes/templates

If a file, `default.md` exists in the above directory, it will used as a template.

A template file can be written in ERB syntax. See ERB manual to know about how to mix ruby code and text content in ERB syntax.

Public Instance Methods

execute(args, conf) click to toggle source
# File lib/rbnotes/commands/add.rb, line 61
def execute(args, conf)
  @opts = {}
  parse_opts(args)

  stamp = @opts[:timestamp] || Textrepo::Timestamp.new(Time.now)

  candidates = [conf[:editor], ENV["EDITOR"], "nano", "vi"].compact
  editor = Rbnotes.utils.find_program(candidates)
  raise Rbnotes::NoEditorError, candidates if editor.nil?

  template = read_template(conf)
  tmpfile = Rbnotes.utils.run_with_tmpfile(editor, stamp.to_s, template)

  unless FileTest.exist?(tmpfile)
    puts "Cancel adding, since nothing to store"
    return
  end

  text = File.readlines(tmpfile)

  repo = Textrepo.init(conf)
  begin
    repo.create(stamp, text)
  rescue Textrepo::DuplicateTimestampError => e
    puts e.message
    puts "Just wait a second, then retry."
  rescue Textrepo::EmptyTextError => e
    puts e.message
  rescue StandardError => e
    puts e.message
  else
    puts "Add a note [%s]" % stamp.to_s
  ensure
    # Don't forget to remove the temporary file.
    File.delete(tmpfile)
  end
end