implement cli commands

This commit is contained in:
Kosta Petan
2023-06-02 16:33:22 +02:00
parent 572857e3a1
commit da38ad5dda
2 changed files with 47 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.CommandLine;
using System.IO;
using System.Net.Http;
using System.Text.Json;
@@ -8,31 +9,57 @@ class Program
{
static async Task Main(string[] args)
{
if (args.Length != 3)
{
Console.WriteLine("Usage: dotnet run <skillName> <functionName> <fileName>");
return;
}
var fileOption = new Option<FileInfo?>(
name: "--file",
description: "The file used for input to the skill function");
var skillName = args[0];
var functionName = args[1];
var filePath = args[2];
var rootCommand = new RootCommand("CLI tool for the AI Dev team");
rootCommand.AddGlobalOption(fileOption);
if (!File.Exists(filePath))
var pmCommand = new Command("pm", "Commands for the PM team");
var pmReadmeCommand = new Command("readme", "Produce a Readme for a given input");
pmReadmeCommand.SetHandler(async (file) => await CallFunction(nameof(PM), PM.Readme , file.FullName), fileOption);
var pmBootstrapCommand = new Command("bootstrap", "Bootstrap a project for a given input");
pmBootstrapCommand.SetHandler(async (file) => await CallFunction(nameof(PM), PM.BootstrapProject, file.FullName), fileOption);
pmCommand.AddCommand(pmReadmeCommand);
pmCommand.AddCommand(pmBootstrapCommand);
var devleadCommand = new Command("devlead", "Commands for the Dev Lead team");
var devleadPlanCommand = new Command("plan", "Plan the work for a given input");
devleadPlanCommand.SetHandler(async (file) => await CallFunction(nameof(DevLead), DevLead.Plan, file.FullName), fileOption);
devleadCommand.AddCommand(devleadPlanCommand);
var devCommand = new Command("dev", "Commands for the Dev team");
var devPlanCommand = new Command("plan", "Implement the module for a given input");
devPlanCommand.SetHandler(async (file) => await CallFunction(nameof(Developer), Developer.Implement, file.FullName), fileOption);
devCommand.AddCommand(devPlanCommand);
rootCommand.AddCommand(pmCommand);
rootCommand.AddCommand(devleadCommand);
rootCommand.AddCommand(devCommand);
await rootCommand.InvokeAsync(args);
}
public static async Task CallFunction(string skillName, string functionName, string file)
{
if (!File.Exists(file))
{
Console.WriteLine($"File not found: {filePath}");
Console.WriteLine($"File not found: {file}");
return;
}
var variables = new[]
{
new { key = "input", value = File.ReadAllText(filePath) }
new { key = "input", value = File.ReadAllText(file) }
};
var requestBody = new { variables };
var requestBodyJson = JsonSerializer.Serialize(requestBody);
Console.WriteLine($"Calling skill '{skillName}' function '{functionName}' with file '{filePath}'");
Console.WriteLine($"Calling skill '{skillName}' function '{functionName}' with file '{file}'");
Console.WriteLine(requestBodyJson);
using var httpClient = new HttpClient();
@@ -59,5 +86,8 @@ class Program
}
return;
}
}
}
public static class PM { public static string Readme = "Readme"; public static string BootstrapProject = "BootstrapProject"; }
public static class DevLead { public static string Plan="Plan"; }
public static class Developer { public static string Implement="Implement"; }

View File

@@ -2,13 +2,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PublishAot>true</PublishAot>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
</Project>