* Added template directory support

* Added session variable support
This commit is contained in:
Andreas Schneider 2015-09-17 14:09:37 +02:00
parent c128b16996
commit 19c9ea042f
2 changed files with 95 additions and 6 deletions

View File

@ -19,8 +19,33 @@
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<BuildModes Count="2">
<Item1 Name="Default" Default="True"/>
<Item2 Name="Release">
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="restemplate"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="jtemplate;synapse"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<CodeGeneration>
<SmartLinkUnit Value="True"/>
<Optimizations>
<OptimizationLevel Value="3"/>
</Optimizations>
</CodeGeneration>
<Linking>
<Debugging>
<GenerateDebugInfo Value="False"/>
</Debugging>
<LinkSmart Value="True"/>
</Linking>
</CompilerOptions>
</Item2>
</BuildModes>
<PublishOptions>
<Version Value="2"/>

View File

@ -20,7 +20,8 @@
program restemplate;
uses
SysUtils, Classes, strutils, JTemplate, httpsend;
SysUtils, Classes, strutils, JTemplate, httpsend, IniFiles,
ssl_openssl;
var
data: TextFile;
@ -30,14 +31,29 @@ var
method: String;
content: TStringList;
commandMode: Boolean;
configDir, templateDir: String;
templateFile, templateName: String;
sessionIni: TIniFile;
procedure CmdAskUser(AName: String);
var
value: String;
value, default: String;
begin
Write(AName, ': ');
Write(AName);
default := sessionIni.ReadString(templateName, AName, '');
if default <> '' then
Write(' [', default, ']: ')
else
Write(': ');
ReadLn(value);
if value = '' then
value := default;
parser.Fields.Add(AName, value);
sessionIni.WriteString(templateName, AName, value);
end;
procedure CmdHeader(AHeader: String);
@ -70,7 +86,11 @@ begin
content.LoadFromStream(http.Document);
writeln(content.Text);
end else
writeln('FAILED !!!');
begin
ExitCode := 2;
writeln;
writeln('FAILED! Last Socket Error: ', http.Sock.SocksLastError);
end;
end;
function ProcessCommand(ALine: String): Boolean;
@ -98,10 +118,53 @@ begin
end;
end;
procedure ListProfiles;
var
sr: TSearchRec;
begin
Writeln('Known profiles:');
if FindFirst(templateDir + '*.rest', faAnyFile, sr) = 0 then
begin
repeat
writeln(' ', Copy(sr.Name, 1, Length(sr.Name) - 5));
until FindNext(sr) <> 0;
end;
end;
begin
configDir := GetAppConfigDir(False);
templateDir := configDir + 'templates' + PathDelim;
templateFile := templateDir + ParamStr(1) + '.rest';
if ParamCount <> 1 then
begin
Writeln('Usage: ', ExtractFileName(ParamStr(0)), ' <profile or file>');
Writeln;
ListProfiles;
Halt(0);
end;
if FileExists(ParamStr(1)) then
begin
AssignFile(data, ParamStr(1));
templateName := ExtractFileName(ParamStr(1));
if AnsiEndsStr('.rest', templateName) then
templateName := Copy(templateName, 1, Length(templateName) - 5);
end else
if FileExists(templateFile) then
begin
AssignFile(data, templateFile);
templateName := ParamStr(1);
end else
begin
writeln('Template not found!');
Halt(1);
end;
Reset(data);
sessionIni := TIniFile.Create(configDir + 'session.ini');
parser := TJTemplateParser.Create;
http := THTTPSend.Create;
content := TStringList.Create;
@ -118,6 +181,7 @@ begin
content.Add(line);
end;
sessionIni.Free;
parser.Free;
http.Free;
content.Free;