Commits

Richo Healey committed 1385e27

Add the needed shims to migrate the config to the new structure

Comments (0)

Files changed (4)

 require 'optparse'
 require 'oauth'
 
-%w[config exceptions argparse actions].each do |filename|
+%w[config exceptions argparse actions migration].each do |filename|
   require "twat/#{filename}"
 end
 

lib/twat/actions.rb

     end
 
     def updateconfig
-      puts "Sadly, not implemented"
+      config.update!
     end
 
     def show

lib/twat/config.rb

       config[k] = v
     end
 
+    # update! migrates an old config file to the current API
+    # it does this by calling a sequence of migration functions in order
+    # which rebuild the config in stages, saving and leaving it in a
+    # consistent step at each point
+    def update!
+      Migrate.new.migrate!(config_path)
+    end
+
   end
 end

lib/twat/migration.rb

+module Twat
+  class Migrate
+    def migrate!(filename)
+      @file = filename
+      migration_1
+    end
+
+    def migration_1
+      new = { accounts: {} }
+      default = nil
+      current = YAML.load_file(@file)
+      current.each do |k, v|
+        k = k.to_sym
+        if k == :default
+          default = v[:oauth_token]
+        else
+          new[:accounts][k] = v
+        end
+      end
+
+      new[:accounts].each do |k, v|
+        if v[:oauth_token] == default
+          new[:default] = k
+          break
+        end
+      end
+
+      save(new)
+    end
+
+    def save(cf)
+      File.open(@file, 'w') do |conf|
+        conf.chmod(0600)
+        conf.puts(cf.to_yaml)
+      end
+    end
+
+  end
+end