Skip to content

Commit aee1dfe

Browse files
Merge pull request #1 from vladbatushkov/args
Introduce args to manage console app
2 parents 2cb6597 + b96d409 commit aee1dfe

5 files changed

Lines changed: 47 additions & 13 deletions

File tree

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ WORKDIR /src
33
COPY Strazh Strazh/
44
RUN dotnet build /src/Strazh/Strazh.csproj -c Release -o /app
55
WORKDIR /app
6-
ENV path=default
7-
CMD ["sh", "-c", "dotnet Strazh.dll $path"]
6+
ENV path=Project.csproj
7+
ENV cred=neo4j:neo4j:test
8+
CMD ["sh", "-c", "dotnet Strazh.dll -p $path -c $cred"]

Strazh/Database/DbManager.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,29 @@ public static class DbManager
1010
{
1111
private const string DBNAME = "neo4j";
1212
private const string USER = "neo4j";
13-
private const string PASSWORD = "strazh";
13+
private const string PASSWORD = "test";
1414
private const string CONNECTION = "neo4j://localhost:7687";
1515

16-
public static async Task InsertData(IEnumerable<Triple> triples, bool isOverride = true)
16+
public static async Task InsertData(IEnumerable<Triple> triples, string credentials = null, bool isOverride = true)
1717
{
18-
var driver = GraphDatabase.Driver(CONNECTION, AuthTokens.Basic(USER, PASSWORD));
19-
var session = driver.AsyncSession(o => o.WithDatabase(DBNAME));
18+
var cred = new[] { DBNAME, USER, PASSWORD };
19+
if (!string.IsNullOrEmpty(credentials))
20+
{
21+
var args = credentials.Split(":");
22+
if (args.Length == 3)
23+
{
24+
cred = args;
25+
}
26+
}
27+
Console.WriteLine($"Connecting to Neo4j database={cred[0]}, user={cred[1]}, password={cred[2]}");
28+
var driver = GraphDatabase.Driver(CONNECTION, AuthTokens.Basic(cred[1], cred[2]));
29+
var session = driver.AsyncSession(o => o.WithDatabase(cred[0]));
2030
try
2131
{
2232
if (isOverride)
2333
{
2434
await session.RunAsync("MATCH (n) DETACH DELETE n;");
35+
Console.WriteLine($"Database `{cred[0]}` is cleaned.");
2536
}
2637
foreach (var triple in triples)
2738
{

Strazh/Program.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.CommandLine;
3+
using System.CommandLine.Invocation;
24
using System.Threading.Tasks;
35
using Strazh.Analysis;
46
using Strazh.Database;
@@ -7,7 +9,25 @@ namespace Strazh
79
{
810
class Program
911
{
10-
static async Task Main(string[] args)
12+
static async Task Main(params string[] args)
13+
{
14+
var rootCommand = new RootCommand();
15+
16+
var optionPath = new Option<string>("--path", "absolute path to .csproj file");
17+
optionPath.AddAlias("-p");
18+
optionPath.IsRequired = true;
19+
rootCommand.Add(optionPath);
20+
21+
var optionCred = new Option<string>("--credentials", "credentials of `dbname:user:password` to connect to Neo4j batabase");
22+
optionCred.AddAlias("-c");
23+
rootCommand.Add(optionCred);
24+
25+
rootCommand.Handler = CommandHandler.Create<string, string>(BuildKnowledgeGraph);
26+
27+
await rootCommand.InvokeAsync(args);
28+
}
29+
30+
static async Task BuildKnowledgeGraph(string path, string credentials)
1131
{
1232
try
1333
{
@@ -17,10 +37,9 @@ static async Task Main(string[] args)
1737
Console.WriteLine("Strazh disappointed. There is no Neo4j instance ready to use.");
1838
return;
1939
}
20-
Console.WriteLine("Brewing the Knowledge Graph...");
21-
var triples = await Analyzer.Analyze(args[0]);
22-
await DbManager.InsertData(triples);
23-
Console.WriteLine("Enjoy the Knowledge Graph of your codebase!");
40+
Console.WriteLine("Brewing the Knowledge Graph.");
41+
var triples = await Analyzer.Analyze(path);
42+
await DbManager.InsertData(triples, credentials);
2443
}
2544
catch (Exception ex)
2645
{

Strazh/Strazh.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<Version>1.0.0-alpha.1</Version>
67
</PropertyGroup>
78

89
<ItemGroup>
@@ -12,7 +13,7 @@
1213
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.0.0" />
1314
<PackageReference Include="Microsoft.CodeAnalysis.Csharp" Version="3.8.0" />
1415
<PackageReference Include="Neo4j.Driver" Version="4.2.0" />
15-
<PackageReference Include="Neo4j.Driver.Reactive" Version="4.2.0" />
16+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
1617
</ItemGroup>
1718

1819
</Project>

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ version: '3'
22
services:
33

44
strazh:
5-
image: vladbatushkov/strazh:1.0.0-alpha
5+
build: .
6+
image: vladbatushkov/strazh:1.0.0-alpha.1
67
container_name: strazh
78
network_mode: host
89
volumes:
910
- ./Strazh:/dest
1011
environment:
1112
- path=/dest/Strazh.csproj
13+
- cred=neo4j:neo4j:strazh
1214
depends_on:
1315
- neo4j
1416

0 commit comments

Comments
 (0)