Ejemplo n.º 1
0
        private async Task RunStepAsync(IUpgradeContext context, UpgradeStep step, CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            await _io.Output.WriteLineAsync();

            var commands = _commandProvider.GetCommands(step, context);
            var command  = await _input.ChooseAsync("Choose a command:", commands, token);

            // TODO : It might be nice to allow commands to show more details by having a 'status' property
            //        that can be shown here. Also, commands currently only return bools but, in the future,
            //        if they return more complex objects, custom handlers could be used to respond to the different
            //        commands' return values.
            if (!await ExecuteAndTimeCommand(context, step, command, token))
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                await _io.Output.WriteAsync($"Command ({command.CommandText}) did not succeed");

                Console.ResetColor();
            }
            else if (!await _input.WaitToProceedAsync(token))
            {
                _logger.LogWarning("Upgrade process was canceled. Quitting....");
            }

            token.ThrowIfCancellationRequested();
        }
Ejemplo n.º 2
0
        public async Task RunAsync(CancellationToken token)
        {
            using var context = await _contextFactory.CreateContext(token);

            await _stateManager.LoadStateAsync(context, token);

            try
            {
                // Cache current steps here as defense-in-depth against the possibility
                // of a bug (or very weird upgrade step behavior) causing the current step
                // to reset state after being initialized by GetNextStepAsync
                var steps = await _upgrader.InitializeAsync(context, token);

                var step = await _upgrader.GetNextStepAsync(context, token);

                while (step is not null)
                {
                    while (!step.IsDone)
                    {
                        token.ThrowIfCancellationRequested();

                        ShowUpgradeSteps(steps, context, step);
                        _io.Output.WriteLine();

                        var commands = _commandProvider.GetCommands(step);
                        var command  = await _input.ChooseAsync("Choose a command:", commands, token);

                        // TODO : It might be nice to allow commands to show more details by having a 'status' property
                        //        that can be shown here. Also, commands currently only return bools but, in the future,
                        //        if they return more complex objects, custom handlers could be used to respond to the different
                        //        commands' return values.
                        if (!await command.ExecuteAsync(context, token))
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            _io.Output.WriteLine($"Command ({command.CommandText}) did not succeed");
                            Console.ResetColor();
                        }
                        else if (await _input.WaitToProceedAsync(token))
                        {
                            ConsoleUtils.Clear();
                        }
                        else
                        {
                            _logger.LogWarning("Upgrade process was canceled. Quitting....");
                            return;
                        }
                    }

                    step = await _upgrader.GetNextStepAsync(context, token);
                }

                _logger.LogInformation("Upgrade has completed. Please review any changes.");
            }
            finally
            {
                // Do not pass the same token as it may have been canceled and we still need to persist this.
                await _stateManager.SaveStateAsync(context, default);
            }
        }