From f0951210f2c86033b20fc7cbaad8192a80d9c770 Mon Sep 17 00:00:00 2001 From: Izimic Date: Thu, 26 Nov 2020 16:31:50 +0100 Subject: [PATCH] execute scriptfile --- src/webhooksd.cr | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/webhooksd.cr b/src/webhooksd.cr index 569434f..c43f8ca 100644 --- a/src/webhooksd.cr +++ b/src/webhooksd.cr @@ -5,6 +5,8 @@ require "./payload.cr" VERSION = 0.1 port = 3000 +args = [] of String + OptionParser.parse do |parser| parser.banner = "usage: webhooksd [option]" parser.on "-v", "--version", "Show version" do @@ -20,6 +22,28 @@ OptionParser.parse do |parser| parser.on "-p PORT", "--port=PORT", "Port to listen for connections. Default: 3000" do |p| port = p.to_i end + + parser.invalid_option do |flag| + STDERR.puts "ERROR: #{flag} is not a valid option." + STDERR.puts parser + exit 1 + end + + parser.unknown_args do |x| + args = x + + if args.size < 1 + puts parser + exit 1 + end + end +end + +scriptfile = args[0] + +if File.exists?(scriptfile) == false + STDERR.puts "ERROR: #{scriptfile} don't exists." + exit 2 end server = HTTP::Server.new do |context| @@ -29,7 +53,12 @@ server = HTTP::Server.new do |context| payload = Payload.new(context.request) context.response.content_type = "text/plain" - context.response.print payload.to_json + if Process.run command: "sh", args: [scriptfile], shell: true, + error: STDERR, output: STDOUT, chdir: Process::INITIAL_PWD + context.response.print "SUCCESS" + else + context.response.print "FAILURE" + end end end