Class: EasyCaddy::Commands::List

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_caddy/commands/list.rb

Instance Method Summary collapse

Constructor Details

#initialize(format: 'table') ⇒ List

Returns a new instance of List.



10
11
12
13
# File 'lib/easy_caddy/commands/list.rb', line 10

def initialize(format: 'table')
  @format   = format
  @registry = Registry.load
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/easy_caddy/commands/list.rb', line 15

def call
  sites = @registry.all
  if sites.empty?
    puts '  No sites registered. Use `ecaddy run --config ./Caddyfile --site NAME` to add one.'
    return
  end

  if @format == 'json'
    require 'json'
    puts JSON.generate(sites.map { |s| row_data(s) })
    return
  end

  require 'tty-table'
  rows = sites.map { |s| row_data(s).values }

  table = TTY::Table.new(
    header: ['Name', 'Status', 'Domains', 'Ports', 'Source'],
    rows:   rows
  )
  puts table.render(:unicode, padding: [0, 1])
end