From ef4bef11a88b06682f01d78c3a0a6eb15a171fd5 Mon Sep 17 00:00:00 2001 From: "koffee.zip" Date: Wed, 15 Jul 2026 21:52:48 -0600 Subject: [PATCH] Added support for txt files and support for a config file --- .gitignore | 3 ++- src/rete.cr | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index a41d725..3500f09 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /bin/ /.shards/ *.dwarf -config.txt \ No newline at end of file +config.txt +test.txt \ No newline at end of file diff --git a/src/rete.cr b/src/rete.cr index f162515..8427ffc 100644 --- a/src/rete.cr +++ b/src/rete.cr @@ -1,22 +1,87 @@ +require "option_parser" require "http/client" require "validator" -repeat = 0 +def checkUrlFromFile(content) # Sorry for the crappy method name. + repeat = 0 + working = 0 + notWorking = 0 + exist = true + config = File.read("config.txt") + urls = content.split("\n") + + while repeat < urls.size + if Valid.url? urls[repeat] + begin + response = HTTP::Client.get(urls[repeat]) + if config.includes?(response.status_code.to_s) + puts "#{urls[repeat]} is a valid URL and works. Status code: #{response.status_code}" + working += 1 + else + puts "#{urls[repeat]} is a valid URL but does not work. Status Code: #{response.status_code}" + notWorking += 1 + end + rescue Socket::Addrinfo::Error + puts "#{urls[repeat]} is a valid URL but does not exist." + notWorking += 1 + end + else + puts "#{urls[repeat]} is not a valid URL" + notWorking += 1 + end + repeat += 1 + end + + puts "==============================" + puts "Finished testing urls." + puts "A total of #{repeat} url(s) were tested." + puts "#{working}/#{repeat} url(s) were working." + puts "#{notWorking}/#{repeat} url(s) were not working." + exit 0 +end + +OptionParser.parse do |parser| + parser.banner = "Usage: rete [arguments] [urls]" + parser.on("-f FILE", "--file=FILE", "Checks all urls in a txt file.") { |file| + urls = File.read(file) + checkUrlFromFile urls + exit 0} + parser.on("-h", "--help", "Show this help") do + puts parser + exit + end + parser.invalid_option do |flag| + STDERR.puts "ERROR: #{flag} is not a valid option." + STDERR.puts parser + exit(1) + end +end + +repeat = 0 +working = 0 +notWorking = 0 config = File.read("config.txt") while repeat < ARGV.size if Valid.url? ARGV[repeat] response = HTTP::Client.get(ARGV[repeat]) if config.includes?(response.status_code.to_s) - puts "#{ARGV[repeat]} is a valid URL and works. #{response.status_code}" + puts "#{ARGV[repeat]} is a valid URL and works. Status code: #{response.status_code}" + working += 1 else - puts "#{ARGV[repeat]} is a valid URL but does not work. #{response.status_code}" + puts "#{ARGV[repeat]} is a valid URL but does not work. Status Code: #{response.status_code}" + notWorking += 1 end elsif puts "#{ARGV[repeat]} is not a valid URL" + notWorking += 1 end repeat += 1 end -puts "=======================\nFinished" + puts "==============================" + puts "Finished testing urls." + puts "A total of #{repeat} url(s) were tested." + puts "#{working}/#{repeat} url(s) were working." + puts "#{notWorking}/#{repeat} url(s) were not working." \ No newline at end of file