Skip to content

Commit cafe29a

Browse files
committed
add psql command
1 parent cafe08c commit cafe29a

File tree

6 files changed

+66
-9
lines changed

6 files changed

+66
-9
lines changed

dev_setup.fish

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set -gp fish_user_paths bin/
2-
alias scb src/cli.cr
3-
complete --command cb --arguments '(cb --_completion (commandline -cp))' --no-files
4-
complete --command scb --arguments '(scb --_completion (commandline -cp))' --no-files
1+
set -gp fish_user_paths bin/
2+
alias scb src/cli.cr
3+
complete --command cb --arguments '(cb --_completion (commandline -cp))' --no-files
4+
complete --command scb --arguments '(scb --_completion (commandline -cp))' --no-files

src/cb/client.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ class CB::Client
106106
raise Program::Error.new "cluster #{id.colorize.t_id} does not exist, or you do not have access to it"
107107
end
108108

109+
jrecord Role, name : String, password : String, uri : URI
110+
111+
def get_cluster_default_role(id)
112+
resp = get "clusters/#{id}/roles/default"
113+
Role.from_json resp.body
114+
end
115+
109116
def create_cluster(cc)
110117
body = {
111118
ha: cc.ha,

src/cb/completion.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ class CB::Completion
1919
case args.first
2020
when "info"
2121
return info
22+
when "psql"
23+
return info
2224
when "destroy"
2325
return info
2426
when "create"
2527
return create
2628
when "firewall"
2729
return firewall
2830
else
29-
STDERR.puts
30-
STDERR.puts commandline.inspect
31-
STDERR.puts args.inspect
3231
[] of String
3332
end
3433
end
@@ -45,6 +44,7 @@ class CB::Completion
4544
"create\tProvision a new cluster",
4645
"destroy\tDestroy a cluster",
4746
"firewall\tManage firewall rules",
47+
"psql\tInteractive psql console",
4848
]
4949
end
5050

src/cb/program.cr

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,16 @@ class CB::Program
126126
end
127127
end
128128

129-
def info(id)
130-
c = client.get_cluster id
129+
private def print_team_slash_cluster(c)
131130
team_name = team_name_for_cluster c
132131
output << team_name << "/" if team_name
133132
output << c.name.colorize.t_name << "\n"
133+
team_name
134+
end
135+
136+
def info(id)
137+
c = client.get_cluster id
138+
print_team_slash_cluster c
134139

135140
details = {
136141
"state" => c.state,
@@ -158,6 +163,37 @@ class CB::Program
158163
firewall_rules.each { |fr| output << " "*(pad + 4) << fr.rule << "\n" }
159164
end
160165

166+
def psql(id)
167+
c = client.get_cluster id
168+
role = client.get_cluster_default_role id
169+
uri = role.uri
170+
171+
output << "connecting to "
172+
team_name = print_team_slash_cluster c
173+
174+
psqlpromptname = String.build do |s|
175+
s << "%[%033[32m%]#{team_name}%[%033m%]" << "/" if team_name
176+
s << "%[%033[36m%]#{c.name}%[%033m%]"
177+
end
178+
179+
psqlrc = File.tempfile(c.id, "pslrc")
180+
File.copy("~/.psqlrc", psqlrc.path) if File.exists?("~/.psqlrc")
181+
File.open(psqlrc.path, "a") do |f|
182+
f.puts "\\set ON_ERROR_ROLLBACK interactive"
183+
f.puts "\\set x auto"
184+
f.puts "\\set PROMPT1 '#{psqlpromptname}/%[%033[33;1m%]%x%x%x%[%033[0m%]%[%033[1m%]%/%[%033[0m%]%R%# '"
185+
end
186+
187+
Process.exec("psql", env: {
188+
"PGHOST" => uri.hostname,
189+
"PGUSER" => uri.user,
190+
"PGPASSWORD" => uri.password,
191+
"PGDATABASE" => uri.path.lchop('/'),
192+
"PGPORT" => uri.port.to_s,
193+
"PSQLRC" => psqlrc.path.to_s,
194+
})
195+
end
196+
161197
private def team_name_for_cluster(c)
162198
# no way to look up a single team yet
163199
client.get_teams.find { |t| t.id == c.team_id }.try &.name.colorize.t_alt

src/cli.cr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ op = OptionParser.new do |parser|
6565
end
6666
end
6767

68+
parser.on("psql", "connect to the dabase using `psql`") do
69+
parser.banner = "Usage: cb psql <cluster id>"
70+
parser.unknown_args do |args|
71+
id = get_id_arg.call(args)
72+
action = ->{ PROG.psql id }
73+
end
74+
end
75+
6876
parser.on("firewall", "manage firewall rules") do
6977
action = manage = CB::ManageFirewall.new(PROG.client)
7078
parser.banner = "Usage: cb firewall <--cluster> [--add] [--remove]"

src/stdlib_ext.cr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ macro jrecord(name, *properties)
3939
{{yield}}
4040
end
4141
end
42+
43+
class URI
44+
def self.new(pull : JSON::PullParser)
45+
parse pull.read_string
46+
end
47+
end

0 commit comments

Comments
 (0)