From 340bd9038f47428c2e2b42b69b43814d8939cfdb Mon Sep 17 00:00:00 2001 From: Gerred Dillon Date: Sat, 2 Apr 2011 10:06:22 -0600 Subject: [PATCH] Adding the ENV has key check, making the File open syntax cleaner --- lib/fog/compute/models/aws/key_pair.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/fog/compute/models/aws/key_pair.rb b/lib/fog/compute/models/aws/key_pair.rb index 9aac3f073..d62adb8ff 100644 --- a/lib/fog/compute/models/aws/key_pair.rb +++ b/lib/fog/compute/models/aws/key_pair.rb @@ -31,15 +31,17 @@ module Fog new_attributes = data.reject {|key,value| !['keyFingerprint', 'keyMaterial', 'keyName'].include?(key)} merge_attributes(new_attributes) true + end def write(path="#{ENV['HOME']}/.ssh/fog_#{Fog.credential.to_s}_#{name}.pem") + if writable? split_private_key = private_key.split(/\n/) - key_file = File.new(path, "w") - split_private_key.each {|line| key_file.puts line} - key_file.chmod 0600 - key_file.close + File.open(path, "w") do |f| + split_private_key.each {|line| f.puts line} + f.chmod 0600 + end "Key file built: #{path}" else "Invalid private key" @@ -47,7 +49,7 @@ module Fog end def writable? - !!private_key + !!(private_key && ENV.has_key?('HOME')) end private