Sha256: 131601394632c91d41a96a403e04192a6f74d3fbcee9eaa4b303e26353b994f9

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'hobo/helper/shell'

module Hobo
  module Helper
    def vm_shell command
      shell VmCommand.new(command).to_s
    end

    def vm_mysql opts = {}
      opts = {
        :auto_echo => true,
        :db => "",
        :user => maybe(Hobo.project_config.mysql.username) || "root",
        :pass => maybe(Hobo.project_config.mysql.password) || "root"
      }.merge(opts)

      VmCommand.new "mysql -u#{opts[:user].shellescape} -p#{opts[:pass].shellescape} #{opts[:db].shellescape}", opts
    end

    def vm_command command = nil
      VmCommand.new command
    end

    private

    class VmCommand
      attr_accessor :opts, :command

      def initialize command, opts = {}
        @command = command
        @opts = {
          :auto_echo => false,
          :psuedo_tty => true,
          :ssh_identity => "#{ENV['HOME'].shellescape}/.vagrant.d/insecure_private_key",
          :ssh_user => "vagrant",
          :ssh_host => maybe(Hobo.project_config.hostname) || ""
        }.merge(opts)
      end

      def << pipe
        pipe = "echo #{pipe.shellescape}" if opts[:auto_echo]
        @pipe = pipe
        @opts[:psuedo_tty] = false
        return self
      end

      def to_s
        psuedo_tty = @opts[:psuedo_tty] ? "-t" : ""
        command = [
          "ssh -i #{opts[:ssh_identity]} #{psuedo_tty} #{opts[:ssh_user].shellescape}@#{opts[:ssh_host].shellescape}",
          @command
        ].compact.join(" -- ")

        [
          @pipe,
          command
        ].compact.join(" | ")
      end

      def to_str
        to_s
      end
    end
  end
end

include Hobo::Helper

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hobo-inviqa-0.0.6 lib/hobo/helper/vm_command.rb