Module: Neptuno::TTY::Config

Overview

Wrapper class for TTY gem

Constant Summary collapse

TTY =
::TTY::Config.new
ABORT_MESSAGE =
'fatal: there are no registered services. Add one with: neptuno services add'

Instance Method Summary collapse

Methods included from File

#file, #in_service?, #make_service_files, #neptuno_path, #project, #service

Instance Method Details

#auto_restart_procsObject



31
32
33
# File 'lib/neptuno/tty/config.rb', line 31

def auto_restart_procs
  config.fetch('auto_restart_procs')
end

#configObject



13
14
15
# File 'lib/neptuno/tty/config.rb', line 13

def config
  TTY
end

#configured_servicesObject



41
42
43
# File 'lib/neptuno/tty/config.rb', line 41

def configured_services
  config.fetch('configured_services')
end

#docker_compose_hashObject



17
18
19
20
21
22
23
24
25
# File 'lib/neptuno/tty/config.rb', line 17

def docker_compose_hash
  source = ::File.read("#{neptuno_path}/docker-compose.yml")
  @@docker_compose ||= begin
    YAML.load(source, aliases: true)
  rescue ArgumentError
    YAML.load(source)
  end
  @@docker_compose
end

#docker_compose_servicesObject



27
28
29
# File 'lib/neptuno/tty/config.rb', line 27

def docker_compose_services
  docker_compose_hash.fetch('services').keys.sort
end

#get_dependants(services = []) ⇒ Object



69
70
71
72
73
74
# File 'lib/neptuno/tty/config.rb', line 69

def get_dependants(services = [])
  return [] if services.empty?

  deps = services.map { |service| docker_compose_hash.dig('services', service, 'depends_on') }.flatten.uniq
  [deps, get_dependants(deps - services)].flatten.compact.uniq
end

#healthy_services(status: nil) ⇒ Object



60
61
62
63
# File 'lib/neptuno/tty/config.rb', line 60

def healthy_services(status: nil)
  data = status || json_services_status
  data.select { |service| service.last.match(/\(healthy\)/) }
end

#json_services_statusObject



49
50
51
52
53
# File 'lib/neptuno/tty/config.rb', line 49

def json_services_status
  JSON.parse(`cd #{neptuno_path} && docker compose ps --all --format json`).map do |service|
    [service.dig('Service'), service.dig('Status')]
  end
end

#running_servicesObject



45
46
47
# File 'lib/neptuno/tty/config.rb', line 45

def running_services
  `cd #{neptuno_path} && docker compose ps --status running --services`.split
end

#servicesObject



35
36
37
38
39
# File 'lib/neptuno/tty/config.rb', line 35

def services
  s = docker_compose_services
  abort ABORT_MESSAGE if s.count.zero?
  s
end

#services_with_procsObject



65
66
67
# File 'lib/neptuno/tty/config.rb', line 65

def services_with_procs
  `cd #{neptuno_path} && find procfiles -type f -size +0`.split.map { |x| x.split('/')[1] }
end

#starting_services(status: nil) ⇒ Object



55
56
57
58
# File 'lib/neptuno/tty/config.rb', line 55

def starting_services(status: nil)
  data = status || json_services_status
  data.select { |service| service.last.match(/starting\)|\(unhealthy\)/) }
end