Class: CastCaster::ChannelCLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/castcaster/channel_cli.rb

Instance Method Summary collapse

Instance Method Details

#addObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/castcaster/channel_cli.rb', line 26

def add
  attrs = {
    'engine' => options[:engine],
    'source' => { 'type' => options[:source_type], 'url' => options[:source] }.compact,
    'status' => 'stopped'
  }
  attrs['transcode'] = { 'profiles' => options[:transcode], 'bitrate' => options[:bitrate] }.compact if options[:transcode] || options[:bitrate]
  attrs['snapshot'] = options[:snapshot] if options[:snapshot]
  Channel.create(options[:name], attrs)
  say_status :ok, "channel '#{options[:name]}' created"
  say_status :info, "Run `docker compose restart nginx` to apply"
rescue Error => e
  say_status :error, e.message
  exit 1
end

#listObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/castcaster/channel_cli.rb', line 4

def list
  channels = Channel.all
  if channels.empty?
    say 'No channels. Use `castcaster channel add --name NAME` to create one.'
    return
  end

  shell = Thor::Shell::Basic.new
  rows = channels.map do |ch|
    [ch.name, ch.engine, ch.live? ? 'live' : 'stopped', ch.source.fetch('url', '-')]
  end
  shell.print_table(rows, headings: %w[Name Engine Status Source])
end

#rm(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/castcaster/channel_cli.rb', line 43

def rm(name)
  ch = Channel.find(name)
  unless ch
    say_status :error, "channel '#{name}' not found"
    exit 1
  end
  ch.destroy
  say_status :ok, "channel '#{name}' removed"
  say_status :info, "Run `docker compose restart nginx` to apply"
end

#start(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/castcaster/channel_cli.rb', line 82

def start(name)
  ch = Channel.find(name)
  unless ch
    say_status :error, "channel '#{name}' not found"
    exit 1
  end
  ch.status = 'live'
  ch.save
  say_status :ok, "channel '#{name}' marked live"
end

#stop(name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/castcaster/channel_cli.rb', line 94

def stop(name)
  ch = Channel.find(name)
  unless ch
    say_status :error, "channel '#{name}' not found"
    exit 1
  end
  ch.status = 'stopped'
  ch.save
  say_status :ok, "channel '#{name}' marked stopped"
end

#update(name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/castcaster/channel_cli.rb', line 60

def update(name)
  ch = Channel.find(name)
  unless ch
    say_status :error, "channel '#{name}' not found"
    exit 1
  end
  re_read = YAML.safe_load(File.read(ch.path)) || {}
  re_read['source'] ||= {}
  re_read['source']['type'] = options[:source_type] if options[:source_type]
  re_read['source']['url'] = options[:source] if options[:source]
  if options[:transcode] || options[:bitrate]
    re_read['transcode'] = { 'profiles' => options[:transcode], 'bitrate' => options[:bitrate] }.compact
  end
  if options.key?(:snapshot)
    re_read['snapshot'] = options[:snapshot] > 0 ? options[:snapshot] : nil
  end
  File.write(ch.path, YAML.dump(re_read))
  say_status :ok, "channel '#{name}' updated"
  say_status :info, "Run `docker compose restart nginx` to apply"
end