From 101d4e5cd4467e6531ecdbafbed883b50eff3524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Sat, 27 May 2017 01:03:59 +0200 Subject: [PATCH] Add interactive prompt for managing keys --- prompt.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 prompt.rb diff --git a/prompt.rb b/prompt.rb new file mode 100644 index 0000000..2e79269 --- /dev/null +++ b/prompt.rb @@ -0,0 +1,51 @@ +require_relative 'collab' +require 'tty-prompt' + +$prompt = TTY::Prompt.new + + +def init + choices = %w(Keys Users Quit) + case $prompt.select("What do you want to do?", choices) + when "Users" + users + when "Keys" + keys + when "Quit" + exit + end +end + +def keys + choices = $users + user = $prompt.select("Pick a user", choices) + keys_user(user) +end + +def keys_user(user) + choices = {'List keys' => :list ,'Add key' => :add, 'Remove key' => :remove, 'Reset authorized_keys' => :reset} + case $prompt.select("What do you want to do for user #{user}?", choices) + when :list + get_ssh_pubkeys(user).each{|k| puts k.pretty} + keys_user user + when :add + key = $prompt.ask("Please enter a valid public SSH key", echo: false) + add_ssh_pubkey(user,key) + when :remove + keys = get_ssh_pubkeys(user)#.map{|k| puts k.pretty} + key = $prompt.select("Select the key to delete", keys) + unless $prompt.no?("Are you sure you want to delete this key?") + remove_ssh_pubkey(user,key) + end + when :reset + unless $prompt.no?("Are you sure you want to reset the keys for this user?") + unless $prompt.no?("Really sure?") + reset_pubkeys(user) + end + end + end +end + +while true + init +end