2017-05-26 23:03:59 +00:00
|
|
|
require_relative 'collab'
|
|
|
|
require 'tty-prompt'
|
2017-06-02 23:32:23 +00:00
|
|
|
require 'colorize'
|
2017-05-26 23:03:59 +00:00
|
|
|
|
|
|
|
$prompt = TTY::Prompt.new
|
2017-06-02 23:32:23 +00:00
|
|
|
$login = "root"
|
2017-05-26 23:03:59 +00:00
|
|
|
|
|
|
|
def init
|
2017-06-02 23:32:23 +00:00
|
|
|
puts "Logged in as " + $login.colorize(:cyan) + "\n"
|
|
|
|
choices = %w(Users Projects Quit)
|
2017-05-26 23:03:59 +00:00
|
|
|
case $prompt.select("What do you want to do?", choices)
|
2017-06-02 23:32:23 +00:00
|
|
|
when "Projects"
|
|
|
|
projects
|
2017-05-26 23:03:59 +00:00
|
|
|
when "Users"
|
|
|
|
users
|
|
|
|
when "Quit"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 23:32:23 +00:00
|
|
|
def users
|
2017-05-26 23:03:59 +00:00
|
|
|
choices = $users
|
|
|
|
user = $prompt.select("Pick a user", choices)
|
|
|
|
keys_user(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys_user(user)
|
2017-06-02 23:32:23 +00:00
|
|
|
choices = {'List keys' => :list ,'Add key' => :add, 'Remove key' => :remove}
|
|
|
|
case $prompt.select("What do you want to do?", choices)
|
2017-05-26 23:03:59 +00:00
|
|
|
when :list
|
2017-06-02 23:32:23 +00:00
|
|
|
user.keys.each{|k| puts k.pretty}
|
2017-05-26 23:03:59 +00:00
|
|
|
keys_user user
|
|
|
|
when :add
|
|
|
|
key = $prompt.ask("Please enter a valid public SSH key", echo: false)
|
2017-06-02 23:32:23 +00:00
|
|
|
begin
|
|
|
|
user.add_key_from_string(key)
|
|
|
|
rescue InvalidSSHPubKey => e
|
|
|
|
puts e.message.colorize(:red)
|
|
|
|
keys_user(user)
|
|
|
|
rescue DuplicateSSHPubKey => e
|
|
|
|
puts e.message.colorize(:red)
|
|
|
|
keys_user(user)
|
|
|
|
end
|
2017-05-26 23:03:59 +00:00
|
|
|
when :remove
|
2017-06-02 23:32:23 +00:00
|
|
|
choices = user.keys.map { |k| [k.pretty, k] }.to_h
|
|
|
|
key = $prompt.select("Select the key to remove", choices)
|
|
|
|
unless $prompt.no?("Are you sure you want to remove this key?")
|
|
|
|
user.remove_key(key)
|
2017-05-26 23:03:59 +00:00
|
|
|
end
|
2017-06-02 23:32:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def projects
|
|
|
|
choices = $projects
|
|
|
|
project = $prompt.select("Pick a project", choices)
|
|
|
|
keys_project(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
def keys_project(project)
|
|
|
|
choices = {'List users' => :list,
|
|
|
|
'Add user' => :add,
|
|
|
|
'Select users with access' => :select}
|
|
|
|
case $prompt.select("What do you want to do?", choices)
|
|
|
|
when :list
|
|
|
|
project.users.each {|u| puts u.id}
|
|
|
|
when :add
|
|
|
|
choices = $users
|
|
|
|
user = $prompt.select("Pick a user", choices)
|
|
|
|
project.add_user(user)
|
|
|
|
puts "Added user #{user} to #{project}".colorize(:green)
|
|
|
|
when :select
|
|
|
|
choices = $users
|
|
|
|
# Mark already added users as "default"
|
|
|
|
counter = 1
|
|
|
|
defaults = []
|
|
|
|
choices.each do |u|
|
|
|
|
if project.users.include? u then
|
|
|
|
defaults << counter
|
2017-05-26 23:03:59 +00:00
|
|
|
end
|
2017-06-02 23:32:23 +00:00
|
|
|
counter += 1
|
2017-05-26 23:03:59 +00:00
|
|
|
end
|
2017-06-02 23:32:23 +00:00
|
|
|
users = $prompt.multi_select("Select users", choices, default: defaults)
|
|
|
|
project.users = users
|
|
|
|
project.flush
|
|
|
|
project.refresh
|
2017-05-26 23:03:59 +00:00
|
|
|
end
|
2017-06-02 23:32:23 +00:00
|
|
|
|
2017-05-26 23:03:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
while true
|
|
|
|
init
|
|
|
|
end
|