Class: Harnex::Logs

Inherits:
Object
  • Object
show all
Defined in:
lib/harnex/commands/logs.rb

Constant Summary collapse

DEFAULT_LINES =
200
POLL_INTERVAL =
0.1
READ_CHUNK_SIZE =
4096

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Logs

Returns a new instance of Logs.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/harnex/commands/logs.rb', line 23

def initialize(argv)
  @argv = argv.dup
  @options = {
    id: nil,
    repo_path: Dir.pwd,
    cli: nil,
    follow: false,
    lines: DEFAULT_LINES,
    help: false
  }
end

Class Method Details

.usage(program_name = "harnex logs") ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/harnex/commands/logs.rb', line 9

def self.usage(program_name = "harnex logs")
  <<~TEXT
    Usage: #{program_name} [options]

    Options:
      --id ID       Session ID to inspect (required)
      --repo PATH   Resolve using PATH's repo root (default: current repo)
      --cli CLI     Filter the active session by CLI
      --follow      Keep streaming appended output until session exit
      --lines N     Print the last N lines before following (default: #{DEFAULT_LINES})
      -h, --help    Show this help
  TEXT
end

Instance Method Details

#runObject

Raises:

  • (OptionParser::InvalidArgument)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/harnex/commands/logs.rb', line 35

def run
  parser.parse!(@argv)
  if @options[:help]
    puts self.class.usage
    return 0
  end

  raise "--id is required for harnex logs" unless @options[:id]
  raise OptionParser::InvalidArgument, "--lines must be >= 0" if @options[:lines].negative?

  target = resolve_target
  return 1 unless target

  offset = print_snapshot(target.fetch(:path))
  return 0 unless @options[:follow] && target[:live]

  follow(target.fetch(:path), offset, target.fetch(:pid))
  0
end