require_relative 'lib/emacscollab' require 'tty-prompt' require 'colorize' require_relative 'lib/emacscollab/collab' require_relative 'lib/emacscollab/sshkey' $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 = EmacsCollab.get_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 EmacsCollab::InvalidSSHPubKey => e puts e.message.colorize(:red) keys_user(user) rescue EmacsCollab::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 = choices = {'Create new project' => :create, 'Modify existing project' => :modify} case $prompt.select("What do you want to do?", choices) when :create project_create when :modify project_modify end end def project_create id = $prompt.ask("What should be the id of the new project?") do |input| input.required true input.validate /\A\w+\Z/ end begin EmacsCollab.create_project(id) rescue EmacsCollab::DuplicateProjectError => e puts e.message.colorize(:red) projects end end def project_modify # Currently, it is implied that modify equals changing which users have access project_users($prompt.select("Pick a project", EmacsCollab.get_projects)) end def project_users(project) choices = {'List users with access' => :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 = EmacsCollab.get_users user = $prompt.select("Pick a user", choices) project.add_user(user.id) puts "Added user #{user} to #{project}".colorize(:green) when :select choices = EmacsCollab.get_users # Mark already added users as "default" defaults = [] counter = 1 choices.each do |u| if project.users.include? u.id then defaults << counter end counter += 1 end if defaults != [] users = $prompt.multi_select("Select users", choices, default: defaults) else users = $prompt.multi_select("Select users", choices) end project.users = users.map { |u| u.id } project.flush project.refresh end end while true init end