Example #1
0
        /// <summary>
        /// Handles the exception
        /// </summary>
        /// <param name="exception">The exception</param>
        /// <param name="context">The context</param>
        internal static void Handle(Exception exception, InvocationContext context)
        {
            //context.Console.Error.Write(exception.ToStringDemystified());
            var logger = LoggingUtils.ConfigureLogger(null);

            var code = Result.GeneralError;

            // TODO: move to dictionary

            switch (exception)
            {
            case FileNotFoundException fileNotFoundException:

                logger.Error(fileNotFoundException.Message);
                code = Result.FileNotFound;

                break;

            case Exception ex:
                code = Result.GeneralError;
                logger.Error("An error occurred: {error}", ex.Message);

                break;
            }

            context.ResultCode = (int)code;
        }
Example #2
0
        /// <summary>
        /// Handles the apply using the specified path
        /// </summary>
        /// <param name="path">The path</param>
        /// <param name="verbosity">The verbosity</param>
        /// <param name="config">The config</param>
        /// <param name="dryRun">The dry run</param>
        /// <param name="console">The console</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <exception cref="Exception">No implementation found for service {nameof(IApplyDocumentHandler)}</exception>
        /// <returns>A task containing the int</returns>
        private static async Task <int> HandleApplyAsync(string path, string verbosity, string config,
                                                         bool dryRun, IConsole console, CancellationToken cancellationToken)
        {
            // Configure the logger
            var logger = LoggingUtils.ConfigureLogger(verbosity);

            logger.Verbose("dotnet-runtime version: {Version}", VersionUtils.GetRuntimeVersion());

            if (VersionUtils.TryGetVersion(out var version))
            {
                logger.Verbose("dotnet-document version: {Version}", version);
            }

            logger.Verbose("Verbosity {verbosity} converted to log level {level}",
                           verbosity, LoggingUtils.ParseLogLevel(verbosity));
            logger.Verbose("Path to document: {path}", path);
            logger.Verbose("Is dry run: {dryRun}", dryRun);
            logger.Verbose("Config file from args: {config}", config);

            var configFilePath = IdentifyConfigFileToUse(config);

            // Declare a new service collection
            var services = new ServiceCollection();

            services.AddLogging(o => o.AddSerilog(logger));

            // Configure services collection
            services.ConfigureFromFile(configFilePath);
            services.AddDotnetDocument();

            // Build the service provider
            var serviceProvider = services.BuildServiceProvider();

            var handler = serviceProvider.GetService <IApplyDocumentHandler>();

            if (handler is null)
            {
                logger.Error("No implementation found for service {Type}", nameof(IApplyDocumentHandler));

                throw new Exception($"No implementation found for service {nameof(IApplyDocumentHandler)}");
            }

            var result = handler.Apply(path, dryRun);

            return(await Task.FromResult((int)result));
        }
        /// <summary>
        /// Handles the context
        /// </summary>
        /// <param name="context">The context</param>
        /// <param name="next">The next</param>
        internal static async Task Handle(InvocationContext context, Func <InvocationContext, Task> next)
        {
            if (context.ParseResult.Directives.Contains("apply"))
            {
                var stopwatch = Stopwatch.StartNew();

                await next(context);

                stopwatch.Stop();

                var logger = LoggingUtils.ConfigureLogger(null);

                logger.Information("Completed in {time} ms", stopwatch.ElapsedMilliseconds);
            }
            else
            {
                await next(context);
            }
        }
Example #4
0
        /// <summary>
        /// Handles the config using the specified default
        /// </summary>
        /// <param name="@default">The default</param>
        /// <param name="config">The config</param>
        /// <param name="console">The console</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <exception cref="Exception">No implementation found for service {nameof(IDocumentConfigHandler)}</exception>
        /// <returns>A task containing the int</returns>
        private static async Task <int> HandleConfigAsync(bool @default, string config, IConsole console,
                                                          CancellationToken cancellationToken)
        {
            // Configure the logger
            var logger = LoggingUtils.ConfigureLogger(null);

            var configFilePath = IdentifyConfigFileToUse(config);

            // Declare a new service collection
            var services = new ServiceCollection();

            services.AddLogging(o => o.AddSerilog(logger));

            // Configure services collection
            services.ConfigureFromFile(configFilePath);
            services.AddDotnetDocument();

            // Build the service provider
            var serviceProvider = services.BuildServiceProvider();

            var handler = serviceProvider.GetService <IDocumentConfigHandler>();

            if (handler is null)
            {
                logger.Error("No implementation found for service {Type}", nameof(IDocumentConfigHandler));

                throw new Exception($"No implementation found for service {nameof(IDocumentConfigHandler)}");
            }

            if (@default)
            {
                return(await Task.FromResult((int)handler.PrintDefaultConfig()));
            }

            return(await Task.FromResult((int)handler.PrintCurrentConfig()));
        }