Skip to content

Commit

Permalink
Merge pull request #1 from vladbatushkov/args
Browse files Browse the repository at this point in the history
Introduce args to manage console app
  • Loading branch information
vladbatushkov authored Jan 4, 2021
2 parents 2cb6597 + b96d409 commit aee1dfe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
ENV path=Project.csproj
ENV cred=neo4j:neo4j:test
CMD ["sh", "-c", "dotnet Strazh.dll -p $path -c $cred"]
19 changes: 15 additions & 4 deletions Strazh/Database/DbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Triple> triples, bool isOverride = true)
public static async Task InsertData(IEnumerable<Triple> 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)
{
Expand Down
29 changes: 24 additions & 5 deletions Strazh/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading.Tasks;
using Strazh.Analysis;
using Strazh.Database;
Expand All @@ -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<string>("--path", "absolute path to .csproj file");
optionPath.AddAlias("-p");
optionPath.IsRequired = true;
rootCommand.Add(optionPath);

var optionCred = new Option<string>("--credentials", "credentials of `dbname:user:password` to connect to Neo4j batabase");
optionCred.AddAlias("-c");
rootCommand.Add(optionCred);

rootCommand.Handler = CommandHandler.Create<string, string>(BuildKnowledgeGraph);

await rootCommand.InvokeAsync(args);
}

static async Task BuildKnowledgeGraph(string path, string credentials)
{
try
{
Expand All @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion Strazh/Strazh.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.0.0-alpha.1</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,7 +13,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Csharp" Version="3.8.0" />
<PackageReference Include="Neo4j.Driver" Version="4.2.0" />
<PackageReference Include="Neo4j.Driver.Reactive" Version="4.2.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit aee1dfe

Please sign in to comment.