summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer <contact@jacobvosmaer.nl>2013-07-19 12:16:54 +0200
committerJacob Vosmaer <contact@jacobvosmaer.nl>2013-07-19 12:16:54 +0200
commit4de0ee3b8fbdbcc215b0e23ef87c7a97eadadafd (patch)
tree8e8b27355fb6cc2cf8d1d261bf93d557b1735fca
parentc4c0885a90e61faa28e928e24565ee0f6bd4b425 (diff)
downloadgitlab-shell-4de0ee3b8fbdbcc215b0e23ef87c7a97eadadafd.tar.gz
Use Tempfile instead of `sed -i`
The syntax for `sed -i` is incompatible between GNU sed and BSD sed. By Tempfile from the Ruby standard library we can avoid using the `-i` option of sed.
-rw-r--r--lib/gitlab_keys.rb7
-rw-r--r--spec/gitlab_keys_spec.rb5
2 files changed, 9 insertions, 3 deletions
diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb
index f554e50..dc54740 100644
--- a/lib/gitlab_keys.rb
+++ b/lib/gitlab_keys.rb
@@ -1,3 +1,4 @@
+require 'tempfile'
require_relative 'gitlab_config'
require_relative 'gitlab_logger'
@@ -35,8 +36,10 @@ class GitlabKeys
def rm_key
$logger.info "Removing key #{@key_id}"
- cmd = "sed -i '/shell #{@key_id}\"/d' #{auth_file}"
- system(cmd)
+ Tempfile.open('authorized_keys') do |temp|
+ cmd = "sed '/shell #{@key_id}\"/d' #{auth_file} > #{temp.path} && mv #{temp.path} #{auth_file}"
+ system(cmd)
+ end
end
def clear
diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb
index 09f5872..c888b8e 100644
--- a/spec/gitlab_keys_spec.rb
+++ b/spec/gitlab_keys_spec.rb
@@ -31,9 +31,12 @@ describe GitlabKeys do
describe :rm_key do
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
+ let(:temp_file) { mock(:temp_file, path: 'tmp_path') }
+ before { Tempfile.should_receive(:open).and_yield(temp_file) }
it "should receive valid cmd" do
- valid_cmd = "sed -i '/shell key-741\"/d' #{GitlabConfig.new.auth_file}"
+ auth_file = GitlabConfig.new.auth_file
+ valid_cmd = "sed '/shell key-741\"/d' #{auth_file} > tmp_path && mv tmp_path #{auth_file}"
gitlab_keys.should_receive(:system).with(valid_cmd)
gitlab_keys.send :rm_key
end