Skip to content

Commit

Permalink
Added IMVCDotEnv.RequireKeys(const Keys: TArray<String>) -- more info h…
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Oct 18, 2023
1 parent 7774cc0 commit dd5d5e6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
18 changes: 18 additions & 0 deletions samples/dotenv_showcase/MainFormU.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions samples/dotenv_showcase/MainFormU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
29 changes: 29 additions & 0 deletions sources/MVCFramework.DotEnv.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>);
function SaveToFile(const FileName: String): IMVCDotEnv;
function ToArray(): TArray<String>;
end;
Expand Down Expand Up @@ -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<String>);
function SaveToFile(const FileName: String): IMVCDotEnv;
function ToArray(): TArray<String>;
public
Expand Down Expand Up @@ -455,6 +457,33 @@ procedure TMVCDotEnv.ReadEnvFile;
end;
end;

procedure TMVCDotEnv.RequireKeys(const Keys: TArray<String>);
var
lKey: String;
lNotFoundKeys: TArray<String>;
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;
Expand Down

0 comments on commit dd5d5e6

Please sign in to comment.