-
-
Save phromo/5dcfe1a0213c96c2d44792eac1c43c61 to your computer and use it in GitHub Desktop.
mutagen-compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'tomlrb' | |
def mutagen_list() | |
out = `mutagen list` | |
if out == "" | |
return [] | |
end | |
session_strs = out.split("-------------------").select { |x| x.include? 'Session' } | |
session_strs.collect do |session_str| | |
session_id = session_str.match(/Session:\s(.*)/)[1] | |
alpha = session_str.match(/Alpha:\n\tURL: (.*)/)[1] | |
beta = session_str.match(/Beta:\n\tURL: (.*)/)[1] | |
{:id => session_id, :alpha => alpha, :beta => beta} | |
end | |
end | |
def mutagen_match(conf) | |
mutagen_list().select do |sessiondata| | |
sessiondata[:alpha] == conf[:roots][:alpha] && sessiondata[:beta] == conf[:roots][:beta] | |
end | |
end | |
def is_running?(conf) | |
mutagen_match(conf)&.first() | |
end | |
# %% | |
def conf_parsefile(filepath) | |
conf = Tomlrb.load_file(filepath, symbolize_keys: true) | |
unless conf[:roots][:alpha].include? ':' | |
conf[:roots][:alpha] = File.expand_path(conf[:roots][:alpha]) | |
end | |
unless conf[:roots][:beta].include? ':' | |
conf[:roots][:beta] = File.expand_path(conf[:roots][:beta]) | |
end | |
return conf | |
end | |
def conf_createcommand(conf) | |
alpha = conf[:roots][:alpha] | |
beta = conf[:roots][:beta] | |
opts = [] | |
conf&.[](:ignore)&.[](:default).to_a.each do |ignstr| | |
opts << "--ignore \"#{ignstr}\"" | |
end | |
conf&.[](:ignore)&.[](:include).to_a.each do |ign_include_raw| | |
ign_include = File.expand_path(ign_include_raw) | |
if File.exist? ign_include | |
igndata = Tomlrb.load_file(ign_include, symbolize_keys: true) | |
igndata&.[](:ignore)&.[](:default).to_a.each do |ignstr| | |
opts << "--ignore \"#{ignstr}\"" | |
end | |
else | |
puts "WARN: ignore include #{ign_include} not found." | |
end | |
end | |
"mutagen create #{alpha} #{beta} #{opts.join(' ')}" | |
end | |
def compose_up() | |
conf = conf_parsefile('./.mutagen-session.toml') | |
if is_running?(conf) | |
puts "session is already running. try down or reload." | |
else | |
cmd = conf_createcommand(conf) | |
puts cmd | |
system(cmd) | |
end | |
end | |
def compose_down() | |
conf = conf_parsefile('./.mutagen-session.toml') | |
for session in mutagen_match(conf) | |
cmd = "mutagen terminate #{session[:id]}" | |
puts cmd | |
system(cmd) | |
end | |
end | |
def compose_reload() | |
compose_down() | |
compose_up() | |
end | |
if __FILE__ == $0 | |
if ARGV.length < 1 | |
puts "Usage: mutagen-compose [up | down | reload]" | |
exit(false) | |
end | |
command_str = ARGV[0] | |
case command_str | |
when 'up' | |
compose_up() | |
when 'down' | |
compose_down() | |
when 'reload' | |
compose_reload() | |
else | |
puts "Usage: mutagen-compose [up | down | reload]" | |
exit(false) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment