public async Task <int> TryRunAsync(string[] args) { CommandLineOptions options; try { options = CommandLineOptions.Parse(args, this.console); } catch (CommandParsingException ex) { new ConsoleReporter(this.console).Warn(ex.Message); return(1); } if (options == null) { return(1); } if (options.Help.HasValue()) { return(2); } if (options.Command == null) { return(3); } var repository = new CommandDataRepository(this.provider); if (options.Command is LoginCommand.Reset) { await options.Command.ExecuteAsync(new CommandContext(this.console, null, null, null, repository)).ConfigureAwait(false); return(0); } var data = repository.GetCommandData() ?? new CommandData { Authority = LoginCommand.DefaultAuthority, ServiceUrl = LoginCommand.DefaultService, }; var authority = data.Authority; var service = data.ServiceUrl; if (options.Command is LoginCommand loginCommand) { authority = loginCommand.Authority; service = loginCommand.ServiceUrl; } else { this.console.WriteLine("Executing command against "); this.console.ForegroundColor = ConsoleColor.White; this.console.WriteLine($"Authority: {authority}"); this.console.WriteLine($"Service Url: {service}"); this.console.ResetColor(); this.console.WriteLine("..."); } var discoveryResponse = default(DiscoveryResponse); using (var discoveryClient = new DiscoveryClient(authority)) { discoveryResponse = await discoveryClient.GetAsync().ConfigureAwait(false); if (discoveryResponse.IsError) { await this.console.Error.WriteLineAsync(discoveryResponse.Error).ConfigureAwait(false); return(500); } } var api = default(Api); using (var client = new HttpClient()) { try { using (var response = client.GetAsync(new Uri($"{service}/platform")).GetAwaiter().GetResult()) { response.EnsureSuccessStatusCode(); api = JsonConvert.DeserializeObject <Api>(response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); } } catch (HttpRequestException) { await this.console.Error.WriteLineAsync($"Unable to connect to service: {service}.").ConfigureAwait(false); return(500); } if (api == null) { await this.console.Error.WriteLineAsync($"Invalid response from: {service}.").ConfigureAwait(false); return(500); } } using (var tokenClient = new TokenClient(discoveryResponse.TokenEndpoint, "donut_console")) using (var refreshTokenHandler = new RefreshTokenHandler(tokenClient, data.RefreshToken, data.AccessToken)) using (var usersClient = new UsersHttpClient(service, refreshTokenHandler)) using (var assetAccountsClient = new AssetAccountsHttpClient(service, refreshTokenHandler)) { refreshTokenHandler.TokenRefreshed += (sender, e) => { repository.SetCommandData( new CommandData { Authority = authority, AccessToken = e.AccessToken, RefreshToken = e.RefreshToken, ServiceUrl = service, }); }; var reporter = new ConsoleReporter(this.console, options.Verbose.HasValue(), false); var context = new CommandContext(this.console, reporter, usersClient, assetAccountsClient, repository); try { await options.Command.ExecuteAsync(context).ConfigureAwait(false); } catch (Exception ex) { reporter.Error(ex.Message); return(500); } finally { this.console.ResetColor(); } return(0); } }
public async Task <int> TryRunAsync(string[] args) { CommandLineOptions options; try { options = CommandLineOptions.Parse(args, this.console); } catch (CommandParsingException ex) { new ConsoleReporter(this.console).Warn(ex.Message); return(1); } if (options == null) { return(1); } if (options.Help.HasValue()) { return(2); } if (options.Command == null) { return(3); } var repository = new CommandDataRepository(this.provider); if (options.Command is LoginCommand.Reset) { await options.Command.ExecuteAsync(new CommandContext(this.console, null, null, null, null, null, null, repository)).ConfigureAwait(false); return(0); } var data = repository.GetCommandData() ?? new CommandData { Authority = LoginCommand.ProductionAuthority, }; var authority = data.Authority; if (options.Command is LoginCommand loginCommand) { authority = loginCommand.Authority; } else { this.console.Write("Executing command against "); this.console.ForegroundColor = ConsoleColor.White; this.console.Write(authority); this.console.ResetColor(); this.console.WriteLine("..."); } var discoveryResponse = default(DiscoveryResponse); using (var discoveryClient = new DiscoveryClient(authority)) { discoveryResponse = await discoveryClient.GetAsync().ConfigureAwait(false); if (discoveryResponse.IsError) { await this.console.Error.WriteLineAsync(discoveryResponse.Error).ConfigureAwait(false); return(500); } } using (var tokenClient = new TokenClient(discoveryResponse.TokenEndpoint, "auth_console")) using (var refreshTokenHandler = new RefreshTokenHandler(tokenClient, data.RefreshToken, data.AccessToken)) using (var clientsClient = new ClientsHttpClient(authority, refreshTokenHandler)) using (var apiResourcesClient = new ApiResourcesHttpClient(authority, refreshTokenHandler)) using (var identityResourcesClient = new IdentityResourcesHttpClient(authority, refreshTokenHandler)) using (var rolesClient = new RolesHttpClient(authority, refreshTokenHandler)) using (var usersClient = new UsersHttpClient(authority, refreshTokenHandler)) { refreshTokenHandler.TokenRefreshed += (sender, e) => { repository.SetCommandData( new CommandData { Authority = authority, AccessToken = e.AccessToken, RefreshToken = e.RefreshToken, }); }; var reporter = new ConsoleReporter(this.console, options.Verbose.HasValue(), false); var context = new CommandContext(this.console, reporter, clientsClient, apiResourcesClient, identityResourcesClient, rolesClient, usersClient, repository); try { await options.Command.ExecuteAsync(context).ConfigureAwait(false); } catch (Exception ex) { reporter.Error(ex.Message); return(500); } finally { this.console.ResetColor(); } return(0); } }