From dec2f6ff19bc6f0c9fcd16dc48a99954605d6409 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 7 Mar 2024 16:16:13 -0500 Subject: [PATCH 1/2] providing one file as an argument will force that file to be used --- src/Program.cs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/Program.cs b/src/Program.cs index 60cae9b..2040073 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -2,14 +2,31 @@ using System; using System.IO; using System.Security.Cryptography; -// use merge function if given multiple arguments -if(args.Length > 0) { +var resourcePath = "/usr/share/fortune-cs/"; + +// pull file arg if 1 argument is provided +string file = null; +if(args.Length == 1) { + file = args[0]; + // if the file doens't exist, see if it's in `resourcePath` + if(!File.Exists(file)) { + if(!file.EndsWith(".txt")) + file = file + ".txt"; + file = resourcePath + file; + if(!File.Exists(file)) { + // don't try to read a file that doesn't exist + Console.WriteLine($"fortune-cs: no file '{file}' found."); + return 2; + } + } +} +// run merge if more than 1 argument is provided +else if(args.Length > 0) { Utilities.Merge(args); return 0; } // make sure fortune directory exists -var resourcePath = "/usr/share/fortune-cs/"; if(!Directory.Exists(resourcePath)) { Console.WriteLine("fortune-cs: directory '/usr/share/fortune-cs/' does not exist"); return 1; @@ -18,15 +35,17 @@ if(!Directory.Exists(resourcePath)) { // pull file list var files = Directory.GetFiles(resourcePath, "*.txt"); -// choose a file and line -var file = files[RandomNumberGenerator.GetInt32(files.Length)]; +// choose file if no arg provided +if(file == null) + file = files[RandomNumberGenerator.GetInt32(files.Length)]; +// read the file and choose a line var lines = File.ReadAllLines(file); var line = lines[RandomNumberGenerator.GetInt32(lines.Length)]; -// process escape codes +// process line breaks line = line.Replace("\\n", "\n"); -// write +// write the fortune Console.WriteLine(line); return 0; From 348f59b44daf9ce18c665d45ac9d4e97aa5331f8 Mon Sep 17 00:00:00 2001 From: Valerie Date: Thu, 7 Mar 2024 16:27:03 -0500 Subject: [PATCH 2/2] the 'FORTUNE_CS_DIR' environment variable now overrides default fortune path --- src/Program.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Program.cs b/src/Program.cs index 2040073..f52239c 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -2,7 +2,10 @@ using System; using System.IO; using System.Security.Cryptography; -var resourcePath = "/usr/share/fortune-cs/"; +// get resource path from var or default +string resourcePath = Environment.GetEnvironmentVariable("FORTUNE_CS_DIR"); +if(resourcePath == "" || !Directory.Exists(resourcePath)) + resourcePath = "/usr/share/fortune-cs/"; // pull file arg if 1 argument is provided string file = null;