first commit
This commit is contained in:
42
Services/HdbClientLocator.cs
Normal file
42
Services/HdbClientLocator.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using HanaToolbox.Services.Interfaces;
|
||||
|
||||
namespace HanaToolbox.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Locates hdbsql and hdbuserstore binaries using:
|
||||
/// 1. Value from config (if set)
|
||||
/// 2. `which` lookup on PATH
|
||||
/// 3. /usr/sap/hdbclient/
|
||||
/// 4. /usr/sap/<SID>/HDB<instance>/exe/
|
||||
/// </summary>
|
||||
public sealed class HdbClientLocator(IProcessRunner runner) : IHdbClientLocator
|
||||
{
|
||||
public string LocateHdbsql(string? configuredPath, string sid, string instanceNumber) =>
|
||||
Locate("hdbsql", configuredPath, sid, instanceNumber);
|
||||
|
||||
public string LocateHdbuserstore(string? configuredPath, string sid, string instanceNumber) =>
|
||||
Locate("hdbuserstore", configuredPath, sid, instanceNumber);
|
||||
|
||||
private string Locate(string binary, string? configuredPath, string sid, string instanceNumber)
|
||||
{
|
||||
// 1. User-configured explicit path
|
||||
if (!string.IsNullOrWhiteSpace(configuredPath) && File.Exists(configuredPath))
|
||||
return configuredPath;
|
||||
|
||||
// 2. which <binary>
|
||||
var whichResult = runner.RunAsync("/usr/bin/which", [binary]).GetAwaiter().GetResult();
|
||||
if (whichResult.Success && !string.IsNullOrWhiteSpace(whichResult.StdOut))
|
||||
return whichResult.StdOut.Split('\n')[0].Trim();
|
||||
|
||||
// 3. /usr/sap/hdbclient/
|
||||
var path3 = $"/usr/sap/hdbclient/{binary}";
|
||||
if (File.Exists(path3)) return path3;
|
||||
|
||||
// 4. /usr/sap/<SID>/HDB<instance>/exe/
|
||||
var path4 = $"/usr/sap/{sid.ToUpperInvariant()}/HDB{instanceNumber}/exe/{binary}";
|
||||
if (File.Exists(path4)) return path4;
|
||||
|
||||
throw new FileNotFoundException(
|
||||
$"Could not locate '{binary}'. Set HdbsqlPath/HdbuserstorePath in hanatoolbox.json or ensure it is on PATH.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user