Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Program.cs cleanup - App separated
Browse files Browse the repository at this point in the history
  • Loading branch information
msimecek committed Jul 1, 2019
1 parent c71352f commit 509e886
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 31 deletions.
24 changes: 24 additions & 0 deletions SpeechCLI/MainApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using McMaster.Extensions.CommandLineUtils;
using SpeechCLI.Commands;

namespace SpeechCLI
{
[Command(Name = "speech", Description = "Command-line interface for Azure Speech service.")]
[Subcommand("config", typeof(ConfigCommand))]
[Subcommand("dataset", typeof(DatasetCommand))]
[Subcommand("model", typeof(ModelCommand))]
[Subcommand("test", typeof(TestCommand))]
[Subcommand("endpoint", typeof(EndpointCommand))]
[Subcommand("compile", typeof(CompileCommand))]
[Subcommand("transcript", typeof(TranscriptCommand))]
public class MainApp
{
int OnExecute(CommandLineApplication app, IConsole console)
{
app.ShowHelp();
return 0;
}
}


}
62 changes: 31 additions & 31 deletions SpeechCLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CRIS;
using SpeechCLI.Commands;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Rest.Serialization;
Expand All @@ -11,44 +10,24 @@

namespace SpeechCLI
{
[Command(Name = "speech", Description = "Command-line interface for Azure Speech service.")]
[Subcommand("config", typeof(ConfigCommand))]
[Subcommand("dataset", typeof(DatasetCommand))]
[Subcommand("model", typeof(ModelCommand))]
[Subcommand("test", typeof(TestCommand))]
[Subcommand("endpoint", typeof(EndpointCommand))]
[Subcommand("compile", typeof(CompileCommand))]
[Subcommand("transcript", typeof(TranscriptCommand))]

class Program
{
static void Main(string[] args)
{
CommandLineApplication<Program> app = new CommandLineApplication<Program>();
app.HelpOption();
app.VersionOptionFromAssemblyAttributes(typeof(Program).Assembly);
var config = InitConfig();

if (!File.Exists(Config.CONFIG_FILENAME))
{
Directory.CreateDirectory(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".speech"));
File.WriteAllText(Config.CONFIG_FILENAME, "[]");
}

var config = SafeJsonConvert.DeserializeObject<List<Config>>(File.ReadAllText(Config.CONFIG_FILENAME)).FirstOrDefault(c => c.Selected == true);
if (config == null)
{
config = new Config("Anonymous", "", "northeurope");
}

var hc = new HttpClient();
hc.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", config.SpeechKey);

var sdk = new SpeechServicesAPIv20(hc, true);
sdk.BaseUri = new Uri($"https://{config.SpeechRegion}.cris.ai");

var services = new ServiceCollection()
.AddSingleton<Config>(config)
.AddSingleton<ISpeechServicesAPIv20>(sdk)
.BuildServiceProvider();
var services = InitServices(config, sdk);

CommandLineApplication<MainApp> app = new CommandLineApplication<MainApp>();
app.HelpOption();
app.VersionOptionFromAssemblyAttributes(typeof(Program).Assembly);
app.Conventions.UseDefaultConventions().UseConstructorInjection(services);

try
Expand All @@ -61,10 +40,31 @@ static void Main(string[] args)
}
}

private int OnExecute(CommandLineApplication app, IConsole console)
static Config InitConfig()
{
app.ShowHelp();
return 0;
if (!File.Exists(Config.CONFIG_FILENAME))
{
Directory.CreateDirectory(Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".speech"));
File.WriteAllText(Config.CONFIG_FILENAME, "[]");
}

var config = SafeJsonConvert.DeserializeObject<List<Config>>(File.ReadAllText(Config.CONFIG_FILENAME)).FirstOrDefault(c => c.Selected == true);
if (config == null)
{
config = new Config("Anonymous", "", "northeurope");
}

return config;
}

static ServiceProvider InitServices(Config config, ISpeechServicesAPIv20 sdk)
{
var services = new ServiceCollection()
.AddSingleton<Config>(config)
.AddSingleton<ISpeechServicesAPIv20>(sdk)
.BuildServiceProvider();

return services;
}

}
Expand Down

0 comments on commit 509e886

Please sign in to comment.