Skip to content

Commit a278488

Browse files
committed
Add --database flag to scope and psql
These changes add the ability to specify a database name to connect when using the `scope` and `psql` command. The flag is setup to be optional. If not provided then it will default to `postgres`.
1 parent cafeb41 commit a278488

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/cb/psql.cr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ require "./action"
22

33
class CB::Psql < CB::Action
44
property cluster_id : String?
5+
property database : String?
56

67
def run
78
c = client.get_cluster cluster_id
89
uri = client.get_cluster_default_role(cluster_id).uri
910

11+
database.tap { |db| uri.path = db if db }
12+
1013
output << "connecting to "
1114
team_name = print_team_slash_cluster c, output
1215

@@ -32,6 +35,10 @@ class CB::Psql < CB::Action
3235
@cluster_id = str
3336
end
3437

38+
def database=(str : String)
39+
@database = str
40+
end
41+
3542
private def ensure_cert(team_id) : String
3643
cert_dir = CB::Creds::CONFIG / "certs"
3744
path = cert_dir / "#{team_id}.pem"

src/cb/scope.cr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ require "./scope_checks/*"
44
class CB::Scope < CB::Action
55
property cluster_id : String?
66
property checks : Array(::Scope::Check.class) = [] of ::Scope::Check.class
7+
property database : String?
78
property suite : String?
89

910
def run
1011
check_required_args { |missing| missing << "cluster" unless cluster_id }
1112

1213
uri = client.get_cluster_default_role(cluster_id).uri
1314

15+
if database.presence
16+
uri.path = database.to_s
17+
end
18+
1419
self.suite = "quick" if checks.empty? && suite.nil?
1520

1621
case suite
@@ -32,3 +37,8 @@ class CB::Scope < CB::Action
3237
end
3338
end
3439
end
40+
41+
def database=(str : String)
42+
str = str.downcase
43+
@database = str
44+
end

src/cli.cr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ op = OptionParser.new do |parser|
7777
end
7878

7979
parser.on("psql", "Connect to the database using `psql`") do
80-
parser.banner = "Usage: cb psql <cluster id> [-- [args for psql such as -c or -f]]"
80+
parser.banner = "Usage: cb psql <cluster id> [--database] [-- [args for psql such as -c or -f]]"
8181
action = psql = CB::Psql.new(PROG.client)
8282

83+
parser.on("--database NAME", "Database name (default: postgres)") { |arg| psql.database = arg }
84+
8385
parser.unknown_args do |args|
8486
psql.cluster_id = get_id_arg.call(args)
8587
end
@@ -129,6 +131,7 @@ op = OptionParser.new do |parser|
129131
action = scope = CB::Scope.new PROG.client
130132
parser.on("--cluster ID", "Choose cluster") { |arg| scope.cluster_id = arg }
131133
parser.on("--suite <all|quick>", "Run suite of scopes (default: quick)") { |arg| scope.suite = arg }
134+
parser.on("--database NAME", "Database name (default: postgres)") { |arg| scope.database = arg }
132135
Scope::Check.all.each do |ck|
133136
parser.on(ck.flag, ck.desc) { scope.checks << ck.type }
134137
end

0 commit comments

Comments
 (0)