Class: Tojour::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/tojour/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Cli

Returns a new instance of Cli.



8
9
10
# File 'lib/tojour/cli.rb', line 8

def initialize(options)
  @options = options
end

Instance Method Details

#input(name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tojour/cli.rb', line 67

def input(name)
  Jour.new(name, 'file').register(@options[:port]) do |r|
    Utils.log "Registered as #{name}."
    Sock.new(@options).server do |server|
      connection = server.accept
      Utils.log "Receiving log data."
      while line = connection.gets
        # next unless line
        line = line.chomp
        $stdout.puts Base64.strict_decode64(line)
      end
    end
  end
end

#listObject



82
83
84
85
86
# File 'lib/tojour/cli.rb', line 82

def list
  Jour.list do |kind, name|
    Utils.log("#{name} (#{kind})")
  end
end

#output(name) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tojour/cli.rb', line 54

def output(name)
  Jour.new(name, 'file').resolve do |r|
    Utils.log "Found #{name} running at #{r.target}:#{r.port}"
    Sock.new(@options).client(r.target, r.port) do |client|
      Utils.log "Sending log data from STDIN."
      $stdin.each do |line|
        puts line
        client.puts(Base64.strict_encode64(line))
      end
    end
  end
end

#receive_file(name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tojour/cli.rb', line 34

def receive_file(name)
  Jour.new(name, 'file').register(@options[:port]) do |r|
    Utils.log "Registered as #{name}."
    Sock.new(@options).server do |server|
      connection = server.accept
      file_name = connection.gets.chomp.gsub(/^### /, '')
      Utils.log "Receiving file \"#{file_name}\"."
      File.open(file_name, 'w') do |f|
        while line = connection.gets
          line = line.chomp
          break if line === '### done'
          f.write Base64.strict_decode64(line)
        end
      end
      Utils.log "Done!"
      exit
    end
  end
end

#send_file(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tojour/cli.rb', line 12

def send_file(name)
  Jour.new(name, 'file').resolve do |r|
    Utils.log "Found #{name} running at #{r.target}:#{r.port}"
    File.open(@options[:send_filename]) do |f|
      Utils.log "Sending \"#{@options[:send_filename]}\"."
      Sock.new(@options).client(r.target, r.port) do |client|
        client.puts('### ' + @options[:send_filename])
        loop do
          begin
            client.puts(Base64.strict_encode64(f.readpartial(1024)))
          rescue EOFError
            client.puts '### done'
            break
          end
        end
        Utils.log "Done!"
        exit
      end
    end
  end
end