class Sshify::Commands::Config

Public Class Methods

new(options) click to toggle source
# File lib/sshify/commands/config.rb, line 11
def initialize(options)
  @options = options

  @config = TTY::Config.new
  @config.filename = "sshify-config"
  @config.extname = ".yml"
  @config.append_path Dir.home

  @cmd = TTY::Command.new
end

Public Instance Methods

execute(input: $stdin, output: $stdout) click to toggle source
# File lib/sshify/commands/config.rb, line 22
def execute(input: $stdin, output: $stdout)
  @config ||= self.class.new(@options).config
  @config.read if @config.persisted?

  selection = prompt.select("Select one") do |menu|
    menu.choice "See config"
    menu.choice "Create new config"
    menu.choice "Add new server"
    menu.choice "Remove server"
  end

  case selection
  when "See config"
    watch_config(output)
  when "Create new config"
    create_config
  when "Add new server"
    add_server
  when "Remove server"
    remove_server(output)
  end
end

Private Instance Methods

add_server() click to toggle source
# File lib/sshify/commands/config.rb, line 70
def add_server
  @config.read if @config.persisted?

  project_name = prompt.ask("What is name for project")
  user = prompt.ask("What is name for user")
  server_ip = prompt.ask("What is server ip")

  @config.append(user, to: [project_name, :user])
  @config.append(server_ip, to: [project_name, :server_ip])
  @config.write(force: true)

  @cmd.run("cat #{@config.location_paths.first}/sshify-config.yml")
end
create_config() click to toggle source
# File lib/sshify/commands/config.rb, line 58
def create_config
  project_name = prompt.ask("What is name for server")
  user = prompt.ask("What is name for user")
  server_ip = prompt.ask("What is server ip")

  @config.set(project_name, :user, value: user)
  @config.set(project_name, :server_ip, value: server_ip)
  @config.write(force: true)

  @cmd.run("cat #{@config.location_paths.first}/sshify-config.yml")
end
remove_server(output) click to toggle source
# File lib/sshify/commands/config.rb, line 84
def remove_server(output)
  @config.read if @config.persisted?

  selection = prompt.select("Select one to delete") do |menu|
    @config.read.keys.each do |server_name|
      menu.choice server_name
    end
  end

  answer = prompt.yes?("Are you sure?")
  if answer == true
    @config.delete(selection)
    @config.write(force: true)
    output.puts "#{selection} deleted"
  end

  @cmd.run("cat #{@config.location_paths.first}/sshify-config.yml")
end
watch_config(output) click to toggle source
# File lib/sshify/commands/config.rb, line 47
def watch_config(output)
  @config.read if @config.persisted?

  unless @config.exist?
    output.puts 'No config set'
    return
  end

  @cmd.run("cat #{@config.location_paths.first}/sshify-config.yml")
end