Skip to content

Instantly share code, notes, and snippets.

@maus-
Created June 23, 2014 17:54
Show Gist options
  • Save maus-/6430818a486948422a07 to your computer and use it in GitHub Desktop.
Save maus-/6430818a486948422a07 to your computer and use it in GitHub Desktop.
note.rb
#!/usr/bin/env ruby
# Version Controlled Notes.... Because I'm lazy. - Maus.
# TODO: Get git syncing working. Maybe do a branch that merges to master?
require 'optparse'
require 'fileutils'
require 'digest'
config = {
notes_path: ENV['HOME'] + '/notes',
git_path: ENV['HOME'] + '/notes/.git',
git_remote: '',
editor_hook: 'vim',
banner_font: 'doom'
}
class App
def setup(config)
prereqs = {
homebrew: %x{which brew},
figlet: %x{which figlet},
git: %x{which git}
}
unless prereqs[:homebrew].empty?
if prereqs[:figlet].empty?
system 'brew install figlet'
end
if prereqs[:git].empty?
system 'brew install git'
end
end
unless File.directory?(config[:notes_path]) &&
File.directory?(config[:git_path])
puts 'Inital setup detected'
end
unless File.directory?(config[:notes_path])
puts "Setting up notes directory in #{config[:notes_path]}"
FileUtils.mkdir config[:notes_path]
end
unless File.directory?(config[:git_path])
Dir.chdir(config[:notes_path])
system 'git init'
end
#unless config[:git_remote].empty?
# system "git --git-dir=#{config[:git_path]} \
# --work-tree=#{config[:notes_path]} \
# remote add orgin #{config[:git_remote]}"
#end
# bad implementation of what I want to do ^
end
def main(config)
banner = 'Usage: notes.rb [filename] [optional-banner] -h for more info'
if ARGV.empty?
puts banner
else
options = {}
OptionParser.new do |opts|
opts.banner = banner
opts.on('-l', '--list-notes', 'list notes') do |l|
options[:list] = l
end
opts.on('-s', '--sync', 'sync remote repo') do |s|
options[:sync] = s
end
end.parse!
if options[:list] == true
puts "Listing entries in #{config[:notes_path]}"
Dir.entries(config[:notes_path]).each do |file|
file_path = config[:notes_path] + '/' + file
last_accessed = File.atime(file_path).to_s
printf "%-40s %s\n", file, last_accessed unless file.match(/^\..*/)
end
elsif options[:sync] == true
unless config[:git_remote].empty?
puts 'Forcing Sync'
system "git --git-dir=#{config[:git_path]} \
--work-tree=#{config[:notes_path]} \
pull"
end
puts 'no remote server specified!'
else
unless config[:git_remote].empty?
system "git --git-dir=#{config[:git_path]} \
--work-tree=#{config[:notes_path]} \
pull"
end
file_path = config[:notes_path] + '/' + ARGV[0]
filename = File.basename(file_path)
if File.exist?(file_path)
sha256 = Digest::SHA256.new
hash = {}
hash[:pre] = sha256.digest File.read(file_path)
system "#{config[:editor_hook]} #{file_path}"
hash[:post] = sha256.digest File.read(file_path)
if hash[:pre] != hash[:post]
system "git --git-dir=#{config[:git_path]} \
--work-tree=#{config[:notes_path]} \
commit -am 'Updated #{filename}'"
else
puts 'No changes were made, nothing to commit.'
end
else
File.new(file_path, 'w')
unless ARGV[1].nil?
banner_text = ARGV[1]
system "figlet -f #{config[:banner_font]} \
#{banner_text} >> #{file_path}"
end
system "#{config[:editor_hook]} #{file_path}"
system "git --git-dir=#{config[:git_path]} \
--work-tree=#{config[:notes_path]} \
add #{filename}"
system "git --git-dir=#{config[:git_path]} \
--work-tree=#{config[:notes_path]} \
commit -am 'Added #{filename}'"
unless config[:git_remote].empty?
system "git --git-dir=#{config[:git_path]} \
--work-tree=#{config[:notes_path]} \
push"
end
end
end
end
end
end
run = App.new
run.setup(config)
run.main(config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment