]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
fixed problems when default encoding is not utf8
authorelijah <elijah@riseup.net>
Mon, 24 Mar 2014 17:16:56 +0000 (10:16 -0700)
committerelijah <elijah@riseup.net>
Mon, 24 Mar 2014 17:16:56 +0000 (10:16 -0700)
lib/leap_cli/config/macros.rb
lib/leap_cli/config/manager.rb
lib/leap_cli/config/object.rb
lib/leap_cli/util.rb

index 69b3a22a71ab33b7caba022adc5a398b41e9b631..c6938fe71515f7d43a723e02634ec58e60496893 100644 (file)
@@ -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
index 46d7686d28e52fa825d61f8644ab215d49bbaff3..b610d3bcbec27cf17443e9478faaf3ac9000ed3b 100644 (file)
@@ -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
index d4fe6bc064df4dc82503b94ac573c0dcd06812c9..e9ef0f8914e212e7dbe88a7eb5607974f3284b5e 100644 (file)
@@ -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)
index 86a9a144dd278b9353a7e52e6c1a15528c518f51..01741580f26723a3bdff6d6697daaa6c09131946 100644 (file)
@@ -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