From 7272dfa3c6d4c3c8877eea2a66fe84c7904cafa5 Mon Sep 17 00:00:00 2001 From: elijah Date: Mon, 24 Mar 2014 10:16:56 -0700 Subject: fixed problems when default encoding is not utf8 --- lib/leap_cli/config/macros.rb | 9 +++++---- lib/leap_cli/config/manager.rb | 6 ++++-- lib/leap_cli/config/object.rb | 6 ++++++ lib/leap_cli/util.rb | 8 ++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/leap_cli/config/macros.rb b/lib/leap_cli/config/macros.rb index 69b3a22..c6938fe 100644 --- a/lib/leap_cli/config/macros.rb +++ b/lib/leap_cli/config/macros.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 # # MACROS # these are methods available when eval'ing a value in the .json configuration @@ -60,9 +61,9 @@ module LeapCli; module Config filepath = Path.find_file(filename) if filepath if filepath =~ /\.erb$/ - ERB.new(File.read(filepath), nil, '%<>').result(binding) + ERB.new(File.read(filepath, :encoding => 'UTF-8'), nil, '%<>').result(binding) else - File.read(filepath) + File.read(filepath, :encoding => 'UTF-8') end else raise FileMissing.new(Path.named_path(filename), options) @@ -343,14 +344,14 @@ module LeapCli; module Config hash = {} keys = Dir.glob(Path.named_path([:user_ssh, '*'])) keys.sort.each do |keyfile| - ssh_type, ssh_key = File.read(keyfile).strip.split(" ") + ssh_type, ssh_key = File.read(keyfile, :encoding => 'UTF-8').strip.split(" ") name = File.basename(File.dirname(keyfile)) hash[name] = { "type" => ssh_type, "key" => ssh_key } end - ssh_type, ssh_key = File.read(Path.named_path(:monitor_pub_key)).strip.split(" ") + ssh_type, ssh_key = File.read(Path.named_path(:monitor_pub_key), :encoding => 'UTF-8').strip.split(" ") hash[Leap::Platform.monitor_username] = { "type" => ssh_type, "key" => ssh_key diff --git a/lib/leap_cli/config/manager.rb b/lib/leap_cli/config/manager.rb index 46d7686..b610d3b 100644 --- a/lib/leap_cli/config/manager.rb +++ b/lib/leap_cli/config/manager.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require 'json/pure' if $ruby_version < [1,9] @@ -230,7 +232,7 @@ module LeapCli Dir.glob(pattern).each do |filename| obj = load_json(filename, object_class) if obj - name = File.basename(filename).sub(/\.json$/,'') + name = File.basename(filename).force_encoding('utf-8').sub(/\.json$/,'') obj['name'] ||= name results[name] = obj end @@ -252,7 +254,7 @@ module LeapCli # https://www.ietf.org/rfc/rfc4627.txt # buffer = StringIO.new - File.open(filename, "rb") do |f| + File.open(filename, "rb", :encoding => 'UTF-8') do |f| while (line = f.gets) next if line =~ /^\s*\/\// buffer << line diff --git a/lib/leap_cli/config/object.rb b/lib/leap_cli/config/object.rb index d4fe6bc..e9ef0f8 100644 --- a/lib/leap_cli/config/object.rb +++ b/lib/leap_cli/config/object.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 + require 'erb' require 'json/pure' # pure ruby implementation is required for our sorted trick to work. @@ -292,6 +294,10 @@ module LeapCli # def fetch_value(key, context=@node) value = fetch(key, nil) + if value.is_a?(String) && value.encoding != Encoding::UTF_8 + p [value, value.encoding] + end + if value.is_a?(String) && value =~ /^=/ if value =~ /^=> (.*)$/ value = evaluate_later(key, $1) diff --git a/lib/leap_cli/util.rb b/lib/leap_cli/util.rb index 86a9a14..0174158 100644 --- a/lib/leap_cli/util.rb +++ b/lib/leap_cli/util.rb @@ -193,13 +193,13 @@ module LeapCli def read_file!(filepath) filepath = Path.named_path(filepath) assert_files_exist!(filepath) - File.read(filepath) + File.read(filepath, :encoding => 'UTF-8') end def read_file(filepath) filepath = Path.named_path(filepath) if file_exists?(filepath) - File.read(filepath) + File.read(filepath, :encoding => 'UTF-8') end end @@ -219,7 +219,7 @@ module LeapCli write_file!(filepath, content) end else - File.open(filepath, File::RDWR|File::CREAT, 0600) do |f| + File.open(filepath, File::RDWR|File::CREAT, 0600, :encoding => 'UTF-8') do |f| f.flock(File::LOCK_EX) old_content = f.read new_content = yield(old_content) @@ -286,7 +286,7 @@ module LeapCli end end - File.open(filepath, 'w', 0600) do |f| + File.open(filepath, 'w', 0600, :encoding => 'UTF-8') do |f| f.write contents end -- cgit v1.2.3