Class: EasyCaddy::Commands::Edit

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

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ Edit

Returns a new instance of Edit.



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

def initialize(name:)
  @name     = name.downcase
  @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
# File 'lib/easy_caddy/commands/edit.rb', line 15

def call
  site = @registry.find(@name)
  unless site
    warn "  Site '#{@name}' is not registered."
    exit 1
  end

  file = site.enabled ? Paths.site_file(@name) : Paths.disabled_file(@name)
  unless file.exist?
    warn "  Fragment file not found: #{file}"
    exit 1
  end

  editor = ENV.fetch('EDITOR', 'vi')
  system("#{editor} #{file}")
  Caddy.validate!(Paths.caddyfile)
  Caddy.reload(Paths.caddyfile) if site.enabled
  puts "  Saved and reloaded."
end