Skip to content

Instantly share code, notes, and snippets.

/zbm

Created February 1, 2016 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1991b321b46f227dfd7d to your computer and use it in GitHub Desktop.
Save anonymous/1991b321b46f227dfd7d to your computer and use it in GitHub Desktop.
My first rough draft attempt at a Ruby bookmarks app.
#!/usr/bin/env ruby
require 'gli'
begin # XXX: Remove this begin/rescue before distributing your app
require 'zbm'
rescue LoadError
STDERR.puts "In development, you need to use `bundle exec bin/zbm` to run your app"
STDERR.puts "At install-time, RubyGems will make sure lib, etc. are in the load path"
STDERR.puts "Feel free to remove this message from bin/zbm now"
exit 64
end
include GLI::App
class Bookmark
attr_accessor :url
attr_accessor :description
attr_accessor :tags
def initialize(url, description, tags)
@url = url
@description = description
@tags = tags.split(/\s+/)
end
end
program_desc 'A simple command line bookmarks app.'
version Zbm::VERSION
subcommand_option_handling :normal
arguments :strict
#desc 'Describe some switch here'
#switch [:s,:switch]
#desc 'Describe some flag here'
#default_value 'the default'
#arg_name 'The name of the argument'
#flag [:f,:flagname]
desc 'Specify an input file.'
default_value File.expand_path('~/bookmarks.txt')
arg_name 'file'
flag [:f,:file]
desc 'Format of the output.'
default_value 'pretty'
arg_name 'data|pretty'
flag [:format]
desc 'Add a bookmark.'
arg_name 'Takes three quoted arguments: a URL, a description, and a list of space delimited #hash-tags.'
command :add do |c|
c.action do |global_options,options,args|
raise 'No arguments given' if args.empty?
args.each { |arg| raise 'Empty argument given.' if arg == '' }
url = args.shift
description = args.shift
tags = args.shift
bookmarks = []
File.open(global_options[:f], 'r') do |file|
file.each_line do |line|
line_url, line_description, line_tags = line.split(/\|/)
bookmarks << Bookmark.new(line_url, line_description, line_tags)
end
end
bookmarks.each do |bookmark|
raise "Duplicate bookmark. Not adding." if bookmark.url == url
end
bookmarks << Bookmark.new(url, description, tags)
File.open(global_options[:f], 'w') do |file|
bookmarks.each do |bookmark|
file.print "#{bookmark.url}\|#{bookmark.description}\|"
bookmark.tags.each { |tag| file.print "#{tag} " }
file.puts ''
end
end
puts 'Bookmark added.'
end
end
desc 'Describe rm here'
arg_name 'Describe arguments to rm here'
command :rm do |c|
c.action do |global_options,options,args|
bookmarks = []
File.open(global_options[:f]) do |file|
file.each_line do |line|
url, description, tags = line.split(/\|/)
bookmarks << Bookmark.new(url, description, tags)
end
end
puts "rm command ran"
end
end
desc 'Display all bookmarks.'
arg_name 'This command takes no arguments.'
command :display do |c|
c.action do |global_options,options,args|
bookmarks = []
File.open(global_options[:f]) do |file|
file.each_line do |line|
url, description, tags = line.split(/\|/)
bookmarks << Bookmark.new(url, description, tags)
end
end
if global_options[:format] == 'pretty' then
counter = 1
bookmarks.each do |bookmark|
puts counter
puts bookmark.url
puts bookmark.description
bookmark.tags.each do |tag|
print "#{tag} "
end
puts ''
puts ''
counter += 1
end
else
bookmarks.each do |bookmark|
print "#{bookmark.url}\|#{bookmark.description}\|"
bookmark.tags.each do |tag|
print "#{tag} "
end
puts ''
end
end
end
end
desc 'Describe update here'
arg_name 'Describe arguments to update here'
command :update do |c|
c.action do |global_options,options,args|
bookmarks = []
File.open(global_options[:f]) do |file|
file.each_line do |line|
url, description, tags = line.split(/\|/)
bookmarks << Bookmark.new(url, description, tags)
end
end
puts "update command ran"
end
end
desc 'Search for bookmarks via tags'
arg_name 'Takes a single argument: taglist'
command :search do |c|
c.action do |global_options,options,args|
bookmarks = []
File.open(global_options[:f]) do |file|
file.each_line do |line|
url, description, tags = line.split(/\|/)
bookmarks << Bookmark.new(url, description, tags)
end
end
puts "search command ran"
end
end
pre do |global,command,options,args|
# Pre logic here
# Return true to proceed; false to abort and not call the
# chosen command
# Use skips_pre before a command to skip this block
# on that command only
true
end
post do |global,command,options,args|
# Post logic here
# Use skips_post before a command to skip this
# block on that command only
end
on_error do |exception|
# Error logic here
# return false to skip default error handling
true
end
exit run(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment