require_relative 'collab' require 'tty-prompt' require 'colorize' $prompt = TTY::Prompt.new $login = "root" def init puts "Logged in as " + $login.colorize(:cyan) + "\n" choices = %w(Users Projects Quit) case $prompt.select("What do you want to do?", choices) when "Projects" projects when "Users" users when "Quit" exit end end def users 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} case $prompt.select("What do you want to do?", choices) when :list user.keys.each{|k| puts k.pretty} keys_user user when :add key = $prompt.ask("Please enter a valid public SSH key", echo: false) 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 when :remove 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) end 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 end counter += 1 end users = $prompt.multi_select("Select users", choices, default: defaults) project.users = users project.flush project.refresh end end while true init end