Parsing command line arguments via GetoptLong.
This commit is contained in:
parent
8900173016
commit
1b770d45d8
76
dup.rb
76
dup.rb
@ -2,6 +2,8 @@
|
|||||||
require 'yaml'
|
require 'yaml'
|
||||||
require 'pp'
|
require 'pp'
|
||||||
require 'shellwords'
|
require 'shellwords'
|
||||||
|
require 'getoptlong'
|
||||||
|
|
||||||
|
|
||||||
def get_sample(name="container")
|
def get_sample(name="container")
|
||||||
return <<~HEREDOC
|
return <<~HEREDOC
|
||||||
@ -49,44 +51,40 @@ def get_sample(name="container")
|
|||||||
HEREDOC
|
HEREDOC
|
||||||
end
|
end
|
||||||
|
|
||||||
if ARGV[0]=="--sample"
|
def action_sample
|
||||||
puts get_sample
|
puts get_sample
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def action_help
|
||||||
|
puts "-h, --help Show this help."
|
||||||
|
puts "-n, --dry-run Don't execute any commands."
|
||||||
|
puts "-s, --sample Outputs a sample yml file."
|
||||||
|
puts "-c, --create Create a new container yml file."
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
def run(cmd, ignore_returnvalue=false)
|
def run(cmd, ignore_returnvalue=false)
|
||||||
puts "+ #{cmd}"
|
puts "+ #{cmd}"
|
||||||
returnvalue = true
|
returnvalue = $dry_run ? true : system(cmd)
|
||||||
returnvalue = system(cmd)
|
|
||||||
raise "Command returned a non-zero exit value." if returnvalue!=true && !ignore_returnvalue
|
raise "Command returned a non-zero exit value." if returnvalue!=true && !ignore_returnvalue
|
||||||
end
|
end
|
||||||
|
|
||||||
if ARGV[0]=="--create"
|
def action_create(container, file)
|
||||||
filename = ARGV[1]
|
|
||||||
else
|
|
||||||
filename = ARGV[0]
|
|
||||||
end
|
|
||||||
|
|
||||||
if filename==nil || filename==""
|
|
||||||
raise "No name given."
|
|
||||||
end
|
|
||||||
|
|
||||||
file = "%s/%s.yml" % [ "/data/fabian/projects/dup", filename ]
|
|
||||||
|
|
||||||
if ARGV[0]=="--create"
|
|
||||||
raise "File #{file} already exists" if File.exists?(file)
|
raise "File #{file} already exists" if File.exists?(file)
|
||||||
|
|
||||||
File.open(file, "w") {|f| f.write(get_sample(filename))}
|
File.open(file, "w") {|f| f.write(get_sample(container))}
|
||||||
|
|
||||||
exec("joe #{file.shellescape}")
|
exec("joe #{file.shellescape}")
|
||||||
exit 1 # will never be reached because exec replaces this process. But just in case... ;-)
|
exit 1 # will never be reached because exec replaces this process. But just in case... ;-)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def action_run(container, file)
|
||||||
raise "File #{file} not found." unless File.exists?(file)
|
raise "File #{file} not found." unless File.exists?(file)
|
||||||
|
|
||||||
data = File.open(file, "r") {|f| YAML.load(f.read)}
|
data = File.open(file, "r") {|f| YAML.load(f.read)}
|
||||||
|
|
||||||
raise "Expected #{file} to define (at least) a container named #{filename}." unless data.has_key?(filename)
|
raise "Expected #{file} to define (at least) a container named #{container}." unless data.has_key?(container)
|
||||||
|
|
||||||
data.each do |key, data|
|
data.each do |key, data|
|
||||||
if data["build"]
|
if data["build"]
|
||||||
@ -121,3 +119,45 @@ data.each do |key, data|
|
|||||||
|
|
||||||
(data["after_run"] || []).each{|cmd| run(cmd)}
|
(data["after_run"] || []).each{|cmd| run(cmd)}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
action = :run
|
||||||
|
container = nil
|
||||||
|
$dry_run = false
|
||||||
|
|
||||||
|
opts = GetoptLong.new(
|
||||||
|
[ '--sample', '-s', GetoptLong::NO_ARGUMENT ],
|
||||||
|
[ '--create', '-c', GetoptLong::NO_ARGUMENT ],
|
||||||
|
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
||||||
|
[ '--dry-run', '-n', GetoptLong::NO_ARGUMENT ]
|
||||||
|
)
|
||||||
|
|
||||||
|
opts.each do |opt, arg|
|
||||||
|
case opt
|
||||||
|
when '--sample'
|
||||||
|
action_sample
|
||||||
|
when '--create'
|
||||||
|
action = :create
|
||||||
|
when '--help'
|
||||||
|
action_help
|
||||||
|
when '--dry-run'
|
||||||
|
puts "Dry-run. Not going to execute any command."
|
||||||
|
$dry_run = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
container = ARGV.shift
|
||||||
|
|
||||||
|
if container==nil || container==""
|
||||||
|
raise "No container given."
|
||||||
|
end
|
||||||
|
|
||||||
|
file = "%s/%s.yml" % [ "/data/fabian/projects/dup", container ]
|
||||||
|
|
||||||
|
if action == :create
|
||||||
|
action_create(container, file)
|
||||||
|
elsif action == :run
|
||||||
|
action_run(container, file)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user