Class: Octo

Inherits:
Object
  • Object
show all
Includes:
Profile, Term::ANSIColor
Defined in:
lib/octo.rb

Defined Under Namespace

Modules: Profile

Instance Attribute Summary

Attributes included from Profile

#config

Instance Method Summary collapse

Methods included from Profile

#add, #config_file, #list, #load, #profile_exists?, #rm, #save

Constructor Details

#initialize(options = {}) ⇒ Octo

Returns a new instance of Octo.



12
13
14
15
16
17
18
19
# File 'lib/octo.rb', line 12

def initialize(options = {})
  @options = options.merge({
    file: false,
    multi: false
  })

  self.load
end

Instance Method Details

#run_mysql(profile, query) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/octo.rb', line 38

def run_mysql(profile, query)
  @config['mysql'][profile].each do |server|
    print "Running query on #{server}... "

    begin
      server_data = server.match(/(.+):(.+)@(.+)\/(.+)/)
      client = Mysql2::Client.new(username: server_data[1],
                                  password: server_data[2],
                                  host: server_data[3],
                                  database: server_data[4])

      results = client.query(query)
      puts "#{results.count} results"
      table = Terminal::Table.new(headings: results.fields) do |t|
        results.each(as: :array) do |row|
          t << row
        end
      end

      puts table
    rescue Exception => e
      puts "ERROR: #{e.inspect}"
    end
  end
end

#run_ssh(profile, command) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/octo.rb', line 21

def run_ssh(profile, command)
  Net::SSH::Multi.start do |session|
    @config['ssh'][profile].each do |server|
      session.use server
    end

    session.exec command do |ch, stream, data|
      stream = stream == :stderr ? $stderr : $stdout
      data.each_line do |line|
        stream.puts "[#{green(ch.properties[:server].to_s)}] #{line}"
      end
      stream.flush
    end
    session.loop
  end
end