Commit 88f0335

mo khan <mo@mokhan.ca>
2014-09-26 21:02:14
collapse add and show commands.
1 parent 874f173
lib/tfa/add_command.rb
@@ -1,13 +0,0 @@
-module TFA
-  class AddCommand
-    def initialize(storage)
-      @storage = storage
-    end
-
-    def run(arguments)
-      name = arguments.first
-      @storage.save(arguments.first, arguments.last)
-      "Added #{name}"
-    end
-  end
-end
lib/tfa/cli.rb
@@ -7,16 +7,17 @@ module TFA
 
     desc "add NAME SECRET", "add a new secret to the database"
     def add(name, secret)
-      AddCommand.new(storage).run([name, secret])
+      storage.save(name, secret)
+      "Added #{name}"
     end
 
     desc "show NAME", "shows the secret for the given key"
     def show(name = nil)
-      ShowCommand.new(storage).run([name].compact)
+      name ? storage.secret_for(name) : storage.all_secrets
     end
 
     desc "totp NAME", "generate a Time based One Time Password"
-    def totp(name)
+    def totp(name = nil)
       TotpCommand.new(storage).run([name])
     end
 
lib/tfa/totp_command.rb
@@ -4,9 +4,9 @@ module TFA
       @storage = storage
     end
 
-    def run(arguments)
-      return password_for(secret_for(arguments.first)) if valid?(arguments)
-      all_passwords
+    def run(name)
+      secret = secret_for(name)
+      secret ? password_for(secret) : all_passwords
     end
 
     private
@@ -16,19 +16,15 @@ module TFA
     end
 
     def all_passwords
-      secrets = @storage.all_secrets
-      secrets.each do |hash|
-        hash[hash.keys.first] = password_for(hash[hash.keys.first])
+      @storage.all_secrets.tap do |secrets|
+        secrets.each do |hash|
+          hash[hash.keys.first] = password_for(hash[hash.keys.first])
+        end
       end
-      secrets
     end
 
     def secret_for(key)
       @storage.secret_for(key)
     end
-
-    def valid?(arguments)
-      arguments.any? && secret_for(arguments.first)
-    end
   end
 end
lib/tfa.rb
@@ -1,8 +1,6 @@
 require "pstore"
 require "rotp"
 require "tfa/version"
-require "tfa/add_command"
-require "tfa/show_command"
 require "tfa/totp_command"
 require "tfa/usage_command"
 require "tfa/cli"
spec/lib/show_command_spec.rb
@@ -1,26 +0,0 @@
-module TFA
-  describe ShowCommand do
-    subject { ShowCommand.new(storage) }
-    let(:storage) { Storage.new(filename: SecureRandom.uuid) }
-
-    describe "#run" do
-      context "when looking up the secret for a specific key" do
-        it "retrieves the secret associated with the key given" do
-          secret = SecureRandom.uuid
-          storage.save('production', secret)
-          result = subject.run(['production'])
-          expect(result).to eql(secret)
-        end
-      end
-
-      context "when a specific name is not given" do
-        it "returns all the secrets" do
-          storage.save('development', "1")
-          storage.save('staging', "2")
-          storage.save('production', "3")
-          expect(subject.run([])).to eql([{"development" => "1"}, { "staging" => "2" }, { "production" => "3" }])
-        end
-      end
-    end
-  end
-end
spec/lib/totp_command_spec.rb
@@ -13,7 +13,7 @@ module TFA
 
         it "returns a time based one time password for the authentication secret given" do
           storage.save('development', secret)
-          expect(subject.run(["development"])).to eql(code_for(secret))
+          expect(subject.run("development")).to eql(code_for(secret))
         end
       end
 
@@ -24,8 +24,8 @@ module TFA
         it "returns the one time password for all keys" do
           storage.save('development', development_secret)
           storage.save('staging', staging_secret)
-          expect(subject.run([])).to eql([
-            { 'development' => code_for(development_secret) }, 
+          expect(subject.run(nil)).to eql([
+            { 'development' => code_for(development_secret) },
             { 'staging' => code_for(staging_secret) }
           ])
         end