Commit 0afd424
Changed files (5)
lib/in/add_command.rb
@@ -0,0 +1,16 @@
+module In
+ class AddCommand
+ def initialize(storage)
+ @storage = storage
+ end
+
+ def run(arguments)
+ name = arguments.first
+ secret = arguments.last
+ @storage.transaction do
+ @storage[name] = secret
+ end
+ "Added #{name}"
+ end
+ end
+end
lib/in/console.rb
@@ -0,0 +1,27 @@
+module In
+ class Console
+ def initialize(filename = "secrets", authenticator)
+ @storage = PStore.new(File.join(Dir.home, ".#{filename}.pstore"))
+ @authenticator = authenticator
+ end
+
+ def run(command)
+ arguments = command.split(' ')
+ command_name = arguments.first
+ command_for(command_name).run(arguments - [command_name])
+ end
+
+ private
+
+ def command_for(command_name)
+ case command_name
+ when "add"
+ AddCommand.new(@storage)
+ when "show"
+ ShowCommand.new(@storage)
+ when "totp"
+ TotpCommand.new(@storage, @authenticator)
+ end
+ end
+ end
+end
lib/in/show_command.rb
@@ -0,0 +1,14 @@
+module In
+ class ShowCommand
+ def initialize(storage)
+ @storage = storage
+ end
+
+ def run(arguments)
+ name = arguments.last
+ @storage.transaction(true) do
+ @storage[name]
+ end
+ end
+ end
+end
lib/in/totp_command.rb
@@ -0,0 +1,16 @@
+module In
+ class TotpCommand
+ def initialize(storage, authenticator)
+ @storage = storage
+ @authenticator = authenticator
+ end
+
+ def run(arguments)
+ name = arguments.first
+ secret = @storage.transaction(true) do
+ @storage[name]
+ end
+ @authenticator.totp(secret)
+ end
+ end
+end
lib/in.rb
@@ -1,73 +1,6 @@
require "in/version"
require "pstore"
-
-module In
- class Console
- def initialize(filename = "secrets", authenticator)
- @storage = PStore.new(File.join(Dir.home, ".#{filename}.pstore"))
- @authenticator = authenticator
- end
-
- def run(command)
- arguments = command.split(' ')
- command_name = arguments.first
- command_for(command_name).run(arguments - [command_name])
- end
-
- private
-
- def command_for(command_name)
- case command_name
- when "add"
- AddCommand.new(@storage)
- when "show"
- ShowCommand.new(@storage)
- when "totp"
- TotpCommand.new(@storage, @authenticator)
- end
- end
- end
-
- class AddCommand
- def initialize(storage)
- @storage = storage
- end
-
- def run(arguments)
- name = arguments.first
- secret = arguments.last
- @storage.transaction do
- @storage[name] = secret
- end
- "Added... "
- end
- end
-
- class ShowCommand
- def initialize(storage)
- @storage = storage
- end
-
- def run(arguments)
- name = arguments.last
- @storage.transaction(true) do
- @storage[name]
- end
- end
- end
-
- class TotpCommand
- def initialize(storage, authenticator)
- @storage = storage
- @authenticator = authenticator
- end
-
- def run(arguments)
- name = arguments.first
- secret = @storage.transaction(true) do
- @storage[name]
- end
- @authenticator.totp(secret)
- end
- end
-end
+require "in/add_command"
+require "in/show_command"
+require "in/totp_command"
+require "in/console"