Skip to content

Instantly share code, notes, and snippets.

@sergio-fry
Created June 1, 2020 11:50
Show Gist options
  • Save sergio-fry/31688d0f59f0ec8c46e432c54c13defc to your computer and use it in GitHub Desktop.
Save sergio-fry/31688d0f59f0ec8c46e432c54c13defc to your computer and use it in GitHub Desktop.
DevUp!
#!/usr/bin/env ruby
require "yaml"
require "ostruct"
class Service
attr_reader :compose, :name
def initialize(compose, name)
@name = name
@compose = compose
end
def ports
compose.service(name)["ports"].map { |el| el.split(":")[-1] }.map do |from|
OpenStruct.new(
from: from,
to: compose.command("port #{name} #{from}").split(":")[-1].strip
)
end
end
end
class Compose
attr_reader :compose_path, :project
def initialize(compose_path = "docker-compose.yml", project: "devup")
@compose_path = compose_path
@project = project
end
def config
YAML.safe_load(File.read(compose_path))
end
def up
command "up -d"
end
def service(name)
config["services"][name]
end
def command(cmd)
`docker-compose -p #{project} -f #{compose_path} #{cmd}`
end
end
class Environment
attr_reader :compose
def initialize
@compose = Compose.new
end
def up
compose.up
end
def services
@compose.config["services"].map do |name, conf|
Service.new compose, name
end
end
def dotenv
services.map { |service|
service.ports.map { |port|
"#{service.name}_PORT_#{port.from}=#{port.to}".upcase
}.join("\n")
}.join("\n\n") + "\n"
end
end
env = Environment.new
env.up
File.open(".env.services", "w") { |f| f.write env.dotenv }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment