From dd5d5e6ee86104c8e20ead33bdf58355113d6c75 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Wed, 18 Oct 2023 15:54:03 +0200 Subject: [PATCH] Added IMVCDotEnv.RequireKeys(const Keys: TArray) -- more info https://github.com/bkeepers/dotenv#required-keys --- samples/dotenv_showcase/MainFormU.dfm | 18 +++++++++++++++++ samples/dotenv_showcase/MainFormU.pas | 17 ++++++++++++++++ sources/MVCFramework.DotEnv.pas | 29 +++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/samples/dotenv_showcase/MainFormU.dfm b/samples/dotenv_showcase/MainFormU.dfm index a17ea817..09eff904 100644 --- a/samples/dotenv_showcase/MainFormU.dfm +++ b/samples/dotenv_showcase/MainFormU.dfm @@ -74,4 +74,22 @@ object MainForm: TMainForm WordWrap = True OnClick = btnSingleEnvClick end + object btnRequireKeys: TButton + Left = 8 + Top = 264 + Width = 169 + Height = 49 + Caption = 'Require Keys (OK)' + TabOrder = 5 + OnClick = btnRequireKeysClick + end + object btnRequireKeys2: TButton + Left = 8 + Top = 319 + Width = 169 + Height = 49 + Caption = 'Require Keys (FAIL)' + TabOrder = 6 + OnClick = btnRequireKeys2Click + end end diff --git a/samples/dotenv_showcase/MainFormU.pas b/samples/dotenv_showcase/MainFormU.pas index 59e2c43c..d9da3264 100644 --- a/samples/dotenv_showcase/MainFormU.pas +++ b/samples/dotenv_showcase/MainFormU.pas @@ -15,10 +15,14 @@ TMainForm = class(TForm) btnProdEnv: TButton; Shape1: TShape; btnSingleEnv: TButton; + btnRequireKeys: TButton; + btnRequireKeys2: TButton; procedure btnSimpleClick(Sender: TObject); procedure btnTestEnvClick(Sender: TObject); procedure btnProdEnvClick(Sender: TObject); procedure btnSingleEnvClick(Sender: TObject); + procedure btnRequireKeysClick(Sender: TObject); + procedure btnRequireKeys2Click(Sender: TObject); private procedure UpdateUI(dotEnv: IMVCDotEnv); public @@ -43,6 +47,19 @@ procedure TMainForm.btnProdEnvClick(Sender: TObject); UpdateUI(dotEnv); end; +procedure TMainForm.btnRequireKeys2Click(Sender: TObject); +begin + var dotEnv := NewDotEnv.WithStrategy(TMVCDotEnvPriority.EnvThenFile).Build(); + dotEnv.RequireKeys(['mode','dbuser','blablabla','dbhostname','unknown']); +end; + +procedure TMainForm.btnRequireKeysClick(Sender: TObject); +begin + var dotEnv := NewDotEnv.WithStrategy(TMVCDotEnvPriority.EnvThenFile).Build(); + dotEnv.RequireKeys(['mode','dbuser','dbpassword','dbhostname']); + ShowMessage('Required Keys FOUND!'); +end; + procedure TMainForm.btnSimpleClick(Sender: TObject); begin var dotEnv := NewDotEnv.WithStrategy(TMVCDotEnvPriority.EnvThenFile).Build(); diff --git a/sources/MVCFramework.DotEnv.pas b/sources/MVCFramework.DotEnv.pas index fad19e63..83298183 100644 --- a/sources/MVCFramework.DotEnv.pas +++ b/sources/MVCFramework.DotEnv.pas @@ -45,6 +45,7 @@ EMVCDotEnv = class(Exception) function Env(const Name: string; const DefaultValue: String): string; overload; function Env(const Name: string; const DefaultValue: Integer): Integer; overload; function Env(const Name: string; const DefaultValue: Boolean): Boolean; overload; + procedure RequireKeys(const Keys: TArray); function SaveToFile(const FileName: String): IMVCDotEnv; function ToArray(): TArray; end; @@ -104,6 +105,7 @@ TMVCDotEnv = class(TInterfacedObject, IMVCDotEnv, IMVCDotEnvBuilder) function Env(const Name: string; const DefaultValue: String): string; overload; function Env(const Name: string; const DefaultValue: Integer): Integer; overload; function Env(const Name: string; const DefaultValue: Boolean): Boolean; overload; + procedure RequireKeys(const Keys: TArray); function SaveToFile(const FileName: String): IMVCDotEnv; function ToArray(): TArray; public @@ -455,6 +457,33 @@ procedure TMVCDotEnv.ReadEnvFile; end; end; +procedure TMVCDotEnv.RequireKeys(const Keys: TArray); +var + lKey: String; + lNotFoundKeys: TArray; + lMsg: string; +begin + if Length(Keys) = 0 then + begin + Exit; + end; + DoLog('Checking required keys: ' + String.Join(', ', Keys)); + lNotFoundKeys := []; + for lKey in Keys do + begin + if Env(lKey).IsEmpty then + begin + lNotFoundKeys := lNotFoundKeys + [lKey]; + end; + end; + if Length(lNotFoundKeys) > 0 then + begin + lMsg := 'Required keys not found: ' + String.Join(', ', lNotFoundKeys); + DoLog(lMsg); + raise EMVCDotEnv.Create(lMsg); + end; +end; + function NewDotEnv: IMVCDotEnvBuilder; begin Result := TMVCDotEnv.Create;