From ee7f46c3eacfc22dbaf41abc66bac7b4494baf37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miskolczi=20Rich=C3=A1rd?= Date: Mon, 17 Apr 2023 16:45:22 +0200 Subject: [PATCH] Adding method for password generation --- CLI/Program.cs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/CLI/Program.cs b/CLI/Program.cs index f7b3bc9..d969a38 100644 --- a/CLI/Program.cs +++ b/CLI/Program.cs @@ -28,6 +28,12 @@ class Program return 1; } break; + case "new": + if (New() == 1) + { + return 1; + } + break; default: HelpMessage(); break; @@ -37,6 +43,61 @@ class Program return 0; } + private static int Generate(string name, string length = PasswordGenerator.DEFAULT_LENGTH.ToString(), string symbols = 'true') + { + try + { + //do not save output, just check if exceptions are thrown + Convert.ToInt32(length); + Convert.ToBoolean(symbols); + + PasswordGenerator.ExceptionOccured += (e) => ErrorOccured(e, "There was a problem generating your password. See details"); + + File.WriteAllText( + $"{ConfigFileManager.GetPath()}\\{name}", + PasswordGenerator.New( + ConfigFileManager.GetRecipient(), + length, + symbols + ) + ) + return 0; + } + catch (FormatException e) + { + ErrorOccured(e, "Please provide valid parameters:\n\t- length:\tnumber\n\t- symbols\ttrue|false)"); + return 1; + } + catch (IOException e) + { + ErrorOccured(e, "IO Error"); + } + } + + static int New() + { + switch (args.Length) + { + case 4: + Generate(args[1], args[2], args[3]); + break; + case 3: + //TODO: determine if the second flag is a bool or an int + Generate(args[1], args[2]); + break; + case 2: + Generate(args[1]); + break; + default: + //either there's an unexpected argument, or not enough + ErrorOccured(new ArgumentException(), $"Invalid or missing arguments, see '{BINARY_NAME} help'"); + return 1; + break; + } + + return 0; + } + static int ListPasswords() { DirectoryInfo d;