This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
soulmate
executable file
·86 lines (74 loc) · 2.29 KB
/
soulmate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
begin
require 'redis'
rescue LoadError
require 'rubygems'
require 'redis'
end
require 'soulmate'
require 'optparse'
parser = OptionParser.new do |opts|
opts.banner = "Usage: soulmate [options] COMMAND"
opts.separator ""
opts.separator "Options:"
opts.on("-r", "--redis [HOST:PORT]", "Redis connection string") do |host|
Soulmate.redis = host
end
opts.on("-h", "--help", "Show this message") do
puts opts
exit
end
opts.separator ""
opts.separator "Commands:"
opts.separator " load TYPE Replaces collection specified by TYPE with items read from stdin in the JSON lines format."
opts.separator " add TYPE Adds items to collection specified by TYPE read from stdin in the JSON lines format."
opts.separator " remove TYPE Removes items from collection specified by TYPE read from stdin in the JSON lines format. Items only require an 'id', all other fields are ignored."
opts.separator " query TYPE QUERY Queries for items from collection specified by TYPE."
end
def load(type)
puts "Loading items of type #{type}..."
items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) }
loaded = Soulmate::Loader.new(type).load(items)
puts "Loaded a total of #{loaded.size} items"
end
def add(type)
puts "Adding items of type #{type}..."
loader = Soulmate::Loader.new(type)
items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) }
items.each do |item|
loader.add(item)
end
puts "Loaded a total of #{items.size} items"
end
def remove(type)
puts "Removing items of type #{type}..."
loader = Soulmate::Loader.new(type)
items = $stdin.read.split("\n").map { |l| MultiJson.decode(l) }
items.each do |item|
loader.remove(item)
end
puts "Removed a total of #{items.size} items"
end
def query(type, query)
puts "> Querying '#{type}' for '#{query}'"
matcher = Soulmate::Matcher.new(type)
results = matcher.matches_for_term(query, :limit => 0)
results.each do |item|
puts MultiJson.encode(item)
end
puts "> Found #{results.size} matches"
end
parser.parse!
case ARGV[0]
when 'load'
load ARGV[1]
when 'add'
add ARGV[1]
when 'remove'
remove ARGV[1]
when 'query'
query ARGV[1], ARGV[2]
else
puts parser.help
end