-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsql
More file actions
executable file
·125 lines (111 loc) · 2.92 KB
/
csql
File metadata and controls
executable file
·125 lines (111 loc) · 2.92 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env ruby
require 'json'
require 'open3'
require 'time'
BASE_PATH = "https://console.cloud.google.com/sql/instances/"
CACHE_FILE = "#{ENV["HOME"]}/.csql_cache.json"
TTL_OFFSET = 300 # seconds
CLOUD_SQL_PATH = ENV["CLOUD_SQL_PATH"]
PROJECT = ENV["PROJECT"]
class Cache
attr_reader :cache
attr_reader :file_name
def initialize(file_name)
@file_name = file_name
begin
if !File.exists?(file_name) || File.zero?(file_name)
file = File.new(file_name, 'w')
@cache = {
"deadline" => Time.now
}
file.write(@cache.to_json)
else
file = File.open(file_name, 'r')
@cache = JSON.parse(file.read)
end
rescue Exception => e
puts e.message
exit(-1)
ensure
file.close unless file.nil?
end
end
def instances
@cache.has_key?("instances") ? @cache["instances"] : {}
end
def expired
# explicit to_s in case reading from new file
Time.parse(@cache["deadline"].to_s) < Time.now
end
def update_cache
begin
sqlClient = CloudSQL.new
instances = sqlClient.getSQLs
@cache = {
"deadline" => Time.now + TTL_OFFSET,
"instances" => instances
}
file = File.open(@file_name, 'w')
file.write(@cache.to_json)
rescue Exception => e
puts e.message
exit(-1)
ensure
file.close unless file.nil?
end
end
def length
return @cache["instances"].length || 0
end
end
class CloudSQL
def getSQLs
out, stat = Open3.capture2(CLOUD_SQL_PATH, "-project=#{PROJECT}")
raise Exception unless stat.success?
instances = out.split("\n").map { |inst| inst.split(' ') }
results = instances.map do |inst|
{
"instance" => inst[0],
"type" => inst[1]
}
end
results
end
end
case ARGV[0]
when '--search'
begin
cache = Cache.new(CACHE_FILE)
cache.update_cache if cache.expired
results = cache.instances
results = results.select {|arg| arg["instance"] =~ /#{(ARGV[1])}/} # Alfred matching won't match queries embedded with a larger string
alfred_results = results.map do |inst|
{
"uid" => inst["instance"],
"title" => inst["instance"],
"subtitle" => inst["type"],
"arg" => BASE_PATH + inst["instance"],
"autocomplete" => inst["instance"],
"quicklookurl" => BASE_PATH+inst["instance"]
}
end
alfred_results = { "items" => alfred_results }.to_json
puts alfred_results
rescue Exception => e
alfred_results = [{
"title" => "Encountered an error!",
"subtitle" => e.message
}]
alfred_results = { "items" => alfred_results }.to_json
exit(-1)
end
when '--refresh'
begin
cache = Cache.new(CACHE_FILE)
cache.update_cache
puts "Successfully updated cache (#{cache.length} records found)"
rescue Exception => e
puts e.message
exit(-1)
end
end