From 19c9ea042f60a26de9f81d1d971418765b466d97 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 17 Sep 2015 14:09:37 +0200 Subject: [PATCH] * Added template directory support * Added session variable support --- restemplate.lpi | 27 +++++++++++++++++- restemplate.lpr | 74 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 95 insertions(+), 6 deletions(-) diff --git a/restemplate.lpi b/restemplate.lpi index 0c29171..81c6cb3 100644 --- a/restemplate.lpi +++ b/restemplate.lpi @@ -19,8 +19,33 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/restemplate.lpr b/restemplate.lpr index d789a4c..3a25665 100644 --- a/restemplate.lpr +++ b/restemplate.lpr @@ -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 - AssignFile(data, ParamStr(1)); + 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)), ' '); + 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;