Class: Rbnotes::Commands::Import
- Defined in:
- lib/rbnotes/commands/import.rb
Overview
Imports a existing file which specified by the argument as a note.
A timestamp is generated referring to the birthtime of the given file. If birthtime is not available on the system, uses mtime (modification time).
When the option, “-m” (or “–use-mtime”) is specified, uses mtime instead of birthtime.
Occasionally, there is another note which has the same timestmap in the repository. Then, tries to create a new timestamp with a suffix. Unluckily, when such timestamp with a suffix already exists, tries to create a new one with increasing suffix. Suffix will be “001”, “002”, …, or “999”. In worst case, all suffix might have been already used. Then, abandons to import.
Instance Method Summary collapse
-
#description ⇒ Object
:nodoc:.
-
#execute(args, conf) ⇒ Object
:call-seq: execute(, Rbnotes::Conf or Hash) -> nil.
-
#help ⇒ Object
:nodoc:.
Instance Method Details
#description ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/rbnotes/commands/import.rb', line 22 def description # :nodoc: "Import a file as a note" end |
#execute(args, conf) ⇒ Object
:call-seq:
execute([PATHNAME], Rbnotes::Conf or Hash) -> nil
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rbnotes/commands/import.rb', line 30 def execute(args, conf) @opts = {} parse_opts(args) file = args.shift unless file.nil? st = File::Stat.new(file) time = nil if @opts[:use_mtime] time = st.mtime else time = st.respond_to?(:birthtime) ? st.birthtime : st.mtime end stamp = Textrepo::Timestamp.new(time) puts "Import [%s] (timestamp [%s]) ..." % [file, stamp] repo = Textrepo.init(conf) content = nil File.open(file, "r") {|f| content = f.readlines(chomp: true)} count = 0 while count <= 999 begin repo.create(stamp, content) break # success to create a note rescue Textrepo::DuplicateTimestampError => _ puts "A text with the timestamp [%s] has been already exists" \ " in the repository." % stamp repo_text = repo.read(stamp) if content == repo_text # if the content is the same to the target file, # the specified file has been already imported. # Then, there is nothing to do. Just exit. puts "The note [%s] in the repository exactly matches" \ " the specified file." % stamp puts "It seems there is no need to import the file [%s]." % file break else puts "The text in the repository does not match the" \ " specified file." count += 1 stamp = Textrepo::Timestamp.new(stamp.time, count) puts "Try to create a note again with a new " \ "timestamp [%s]." % stamp end rescue Textrepo::EmptyTextError => _ puts "... aborted." puts "The specified file is empty." break end end if count > 999 puts "Cannot create a text into the repository with the" \ " specified file [%s]." % file puts "For, the birthtime [%s] is identical to some notes" \ " already exists in the reopsitory." % time puts "Change the birthtime of the target file, then retry." else puts "... Done." end else puts "not supecified FILE" super end end |
#help ⇒ Object
:nodoc:
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rbnotes/commands/import.rb', line 98 def help # :nodoc: puts <<HELP usage: #{Rbnotes::NAME} import [-m|--use-mtime] FILE Imports a existing file which specified by the argument as a note. A timestamp is generated referring to the birthtime of the given file. If birthtime is not available on the system, use mtime (modification time). HELP end |