Module: OsCtl::Cli::TransferProgress

Included in:
Container, Send
Defined in:
lib/osctl/cli/transfer_progress.rb

Instance Method Summary collapse

Instance Method Details

#format_str(maxsize) ⇒ Object (protected)



71
72
73
# File 'lib/osctl/cli/transfer_progress.rb', line 71

def format_str(maxsize)
  "%E %t #{(maxsize / 1024.0).round(2)} GB: [%B] %p%% %r MB/s"
end

#json_progress(msg) ⇒ Object (protected)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/osctl/cli/transfer_progress.rb', line 75

def json_progress(msg)
  if msg.is_a?(String)
    puts({ type: :update, text: msg }.to_json)
    return
  end

  case msg[:type].to_sym
  when :step
    puts({ type: :step, text: msg[:title] }.to_json)

  when :progress
    puts({ type: :progress, data: msg[:data] }.to_json)
  end
end

#terminal_progress(msg) ⇒ Object (protected)



28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
69
# File 'lib/osctl/cli/transfer_progress.rb', line 28

def terminal_progress(msg)
  return if gopts[:quiet]

  if msg.is_a?(String)
    if @pb
      @pb.finish
      @pb = nil
    end

    puts "> #{msg}"
    return
  end

  case msg[:type].to_sym
  when :step
    if @pb
      @pb.finish
      @pb = nil
    end

    puts "* #{msg[:title]}"

  when :progress
    data = msg[:data]
    @pb ||= ProgressBar.create(
      title: @progress_title || 'Transfer',
      total: data[:size],
      format: format_str(data[:size]),
      throttle_rate: 0.2,
      starting_at: 0,
      autofinish: false,
      output: $stdout
    )

    if data[:transfered] > @pb.total
      @pb.total = data[:transfered]
      @pb.format = format_str(@pb.total)
    end

    @pb.progress = data[:transfered]
  end
end

#with_progress(cmd, progress_title: 'Sending', **opts) ⇒ Object (protected)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/osctl/cli/transfer_progress.rb', line 8

def with_progress(cmd, progress_title: 'Sending', **opts)
  @progress_title = progress_title

  osctld_call(cmd, **opts) do |msg|
    if gopts[:json]
      json_progress(msg)

    else
      terminal_progress(msg)
    end
  end

  @pb.finish if @pb
rescue OsCtl::Client::Error
  @pb.cancel if @pb
  raise
ensure
  @progress_title = nil
end