diff --git a/Dockerfile b/Dockerfile index 69ff0bc..cddd231 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,5 +3,6 @@ WORKDIR /src COPY Strazh Strazh/ RUN dotnet build /src/Strazh/Strazh.csproj -c Release -o /app WORKDIR /app -ENV path=default -CMD ["sh", "-c", "dotnet Strazh.dll $path"] \ No newline at end of file +ENV path=Project.csproj +ENV cred=neo4j:neo4j:test +CMD ["sh", "-c", "dotnet Strazh.dll -p $path -c $cred"] \ No newline at end of file diff --git a/Strazh/Database/DbManager.cs b/Strazh/Database/DbManager.cs index 8b5c761..c6f5ff4 100644 --- a/Strazh/Database/DbManager.cs +++ b/Strazh/Database/DbManager.cs @@ -10,18 +10,29 @@ public static class DbManager { private const string DBNAME = "neo4j"; private const string USER = "neo4j"; - private const string PASSWORD = "strazh"; + private const string PASSWORD = "test"; private const string CONNECTION = "neo4j://localhost:7687"; - public static async Task InsertData(IEnumerable triples, bool isOverride = true) + public static async Task InsertData(IEnumerable triples, string credentials = null, bool isOverride = true) { - var driver = GraphDatabase.Driver(CONNECTION, AuthTokens.Basic(USER, PASSWORD)); - var session = driver.AsyncSession(o => o.WithDatabase(DBNAME)); + var cred = new[] { DBNAME, USER, PASSWORD }; + if (!string.IsNullOrEmpty(credentials)) + { + var args = credentials.Split(":"); + if (args.Length == 3) + { + cred = args; + } + } + Console.WriteLine($"Connecting to Neo4j database={cred[0]}, user={cred[1]}, password={cred[2]}"); + var driver = GraphDatabase.Driver(CONNECTION, AuthTokens.Basic(cred[1], cred[2])); + var session = driver.AsyncSession(o => o.WithDatabase(cred[0])); try { if (isOverride) { await session.RunAsync("MATCH (n) DETACH DELETE n;"); + Console.WriteLine($"Database `{cred[0]}` is cleaned."); } foreach (var triple in triples) { diff --git a/Strazh/Program.cs b/Strazh/Program.cs index 96c4ffd..a491705 100644 --- a/Strazh/Program.cs +++ b/Strazh/Program.cs @@ -1,4 +1,6 @@ using System; +using System.CommandLine; +using System.CommandLine.Invocation; using System.Threading.Tasks; using Strazh.Analysis; using Strazh.Database; @@ -7,7 +9,25 @@ namespace Strazh { class Program { - static async Task Main(string[] args) + static async Task Main(params string[] args) + { + var rootCommand = new RootCommand(); + + var optionPath = new Option("--path", "absolute path to .csproj file"); + optionPath.AddAlias("-p"); + optionPath.IsRequired = true; + rootCommand.Add(optionPath); + + var optionCred = new Option("--credentials", "credentials of `dbname:user:password` to connect to Neo4j batabase"); + optionCred.AddAlias("-c"); + rootCommand.Add(optionCred); + + rootCommand.Handler = CommandHandler.Create(BuildKnowledgeGraph); + + await rootCommand.InvokeAsync(args); + } + + static async Task BuildKnowledgeGraph(string path, string credentials) { try { @@ -17,10 +37,9 @@ static async Task Main(string[] args) Console.WriteLine("Strazh disappointed. There is no Neo4j instance ready to use."); return; } - Console.WriteLine("Brewing the Knowledge Graph..."); - var triples = await Analyzer.Analyze(args[0]); - await DbManager.InsertData(triples); - Console.WriteLine("Enjoy the Knowledge Graph of your codebase!"); + Console.WriteLine("Brewing the Knowledge Graph."); + var triples = await Analyzer.Analyze(path); + await DbManager.InsertData(triples, credentials); } catch (Exception ex) { diff --git a/Strazh/Strazh.csproj b/Strazh/Strazh.csproj index d68979e..73865c1 100644 --- a/Strazh/Strazh.csproj +++ b/Strazh/Strazh.csproj @@ -3,6 +3,7 @@ Exe netcoreapp3.1 + 1.0.0-alpha.1 @@ -12,7 +13,7 @@ - + diff --git a/docker-compose.yml b/docker-compose.yml index b47e065..80aa3ee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,13 +2,15 @@ version: '3' services: strazh: - image: vladbatushkov/strazh:1.0.0-alpha + build: . + image: vladbatushkov/strazh:1.0.0-alpha.1 container_name: strazh network_mode: host volumes: - ./Strazh:/dest environment: - path=/dest/Strazh.csproj + - cred=neo4j:neo4j:strazh depends_on: - neo4j