Commit 3e84545

mo khan <mo@mokhan.ca>
2014-09-26 21:11:18
make storage enumerable and remove usage command.
1 parent 88f0335
lib/tfa/cli.rb
@@ -13,7 +13,7 @@ module TFA
 
     desc "show NAME", "shows the secret for the given key"
     def show(name = nil)
-      name ? storage.secret_for(name) : storage.all_secrets
+      name ? storage.secret_for(name) : storage.all
     end
 
     desc "totp NAME", "generate a Time based One Time Password"
lib/tfa/show_command.rb
@@ -1,12 +0,0 @@
-module TFA
-  class ShowCommand
-    def initialize(storage)
-      @storage = storage
-    end
-
-    def run(arguments)
-      return @storage.secret_for(arguments.last) if arguments.any?
-      @storage.all_secrets
-    end
-  end
-end
lib/tfa/storage.rb
@@ -1,10 +1,18 @@
 module TFA
   class Storage
+    include Enumerable
+
     def initialize(filename:)
       @storage = PStore.new(File.join(Dir.home, ".#{filename}.pstore"))
     end
 
-    def all_secrets
+    def each
+      all.each do |each|
+        yield each
+      end
+    end
+
+    def all
       open_readonly do |storage|
         storage.roots.map { |key| { key => storage[key] } }
       end
lib/tfa/totp_command.rb
@@ -16,10 +16,8 @@ module TFA
     end
 
     def all_passwords
-      @storage.all_secrets.tap do |secrets|
-        secrets.each do |hash|
-          hash[hash.keys.first] = password_for(hash[hash.keys.first])
-        end
+      @storage.each do |hash|
+        hash[hash.keys.first] = password_for(hash[hash.keys.first])
       end
     end
 
lib/tfa/usage_command.rb
@@ -1,16 +0,0 @@
-module TFA
-  class UsageCommand
-    def initialize(storage)
-      @storage = storage
-    end
-
-    def run(arguments)
-      <<-MESSAGE
-Try:
-  - tfa add develoment <secret>
-  - tfa show development
-  - tfa totp development
-      MESSAGE
-    end
-  end
-end
lib/tfa.rb
@@ -2,6 +2,5 @@ require "pstore"
 require "rotp"
 require "tfa/version"
 require "tfa/totp_command"
-require "tfa/usage_command"
-require "tfa/cli"
 require "tfa/storage"
+require "tfa/cli"