aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-04-30 00:23:22 +0200
committerTeddy Wing2017-04-30 00:23:22 +0200
commit1b79a1dc72e703d975effa2de806c9afd9dbaa81 (patch)
tree7bfa5376643f1dd44ac982922a8f4f15a893bab4
parentf59507205062a0910d8ec9bd41269c0d0e9c3296 (diff)
downloaddbshell-rails-1b79a1dc72e703d975effa2de806c9afd9dbaa81.tar.bz2
Try to add support for shell aliases through Ruby's `Kernel.exec`add-support-for-shell-aliased-database-commands
Work in progress. Ruby's `system` and `exec` don't load shell aliases, so the executables defined in `EXECUTABLE_NAME` will always be the ones used to launch database shells. The trouble is, users might have installed the command with a different name (as in "mysql5"), or they might not have the command on their path (as in my case when using Postgres.app). Apparently you can use an interactive or login shell to get aliases (using the `-l` or `-i` flags, which do slightly different things, sourcing .profile and .bashrc separately). Here's what I was following: http://stackoverflow.com/questions/12060863/python-subprocess-call-a-bash-alias/25099813#25099813 Unfortunately I haven't been able to get it to work. Dropping this for now.
-rw-r--r--lib/dbshell/client/postgres.rb4
-rw-r--r--lib/dbshell/exec_alias.rb11
-rw-r--r--spec/dbshell/exec_alias_spec.rb25
3 files changed, 39 insertions, 1 deletions
diff --git a/lib/dbshell/client/postgres.rb b/lib/dbshell/client/postgres.rb
index 78b82d7..9979710 100644
--- a/lib/dbshell/client/postgres.rb
+++ b/lib/dbshell/client/postgres.rb
@@ -1,3 +1,5 @@
+require 'dbshell/exec_alias'
+
module DBShell
module Client
class Postgres
@@ -5,7 +7,7 @@ module DBShell
def self.runshell(db_info)
args = self.build_command(db_info)
- exec(*args)
+ DBShell::ExecAlias.exec(args)
end
def self.build_command(db_info)
diff --git a/lib/dbshell/exec_alias.rb b/lib/dbshell/exec_alias.rb
new file mode 100644
index 0000000..7bd86b7
--- /dev/null
+++ b/lib/dbshell/exec_alias.rb
@@ -0,0 +1,11 @@
+module DBShell
+ class ExecAlias
+ def self.exec(command_args)
+ Kernel.exec(*self.build_command(command_args))
+ end
+
+ def self.build_command(command_args)
+ [ENV['SHELL'], '--login', '-c', command_args.join(' ')]
+ end
+ end
+end
diff --git a/spec/dbshell/exec_alias_spec.rb b/spec/dbshell/exec_alias_spec.rb
new file mode 100644
index 0000000..1b8c425
--- /dev/null
+++ b/spec/dbshell/exec_alias_spec.rb
@@ -0,0 +1,25 @@
+require 'minitest/autorun'
+require 'dbshell/exec_alias'
+
+describe DBShell::ExecAlias do
+ describe ".build_command" do
+ it "prefixes the command with an interactive version of the current \
+ shell" do
+ DBShell::ExecAlias.build_command([
+ 'psql',
+ '-U',
+ 'derpyhooves',
+ '-h',
+ 'mailmarehost',
+ '-p',
+ '6027',
+ 'dbname'
+ ]).must_equal([
+ ENV['SHELL'],
+ '-i',
+ '-c',
+ 'psql -U derpyhooves -h mailmarehost -p 6027 dbname'
+ ])
+ end
+ end
+end