예제 #1
0
        /// <summary>
        /// Main the args
        /// </summary>
        /// <param name="args">The args</param>
        /// <returns>A task containing the int</returns>
        public static async Task <int> Main(string[] args)
        {
            // Declare the dotnet-document command
            var documentCommand = new RootCommand("dotnet-document")
            {
                // Add sub commands
                ApplyCommand.Create(HandleApplyAsync),
                ConfigCommand.Create(HandleConfigAsync)
            };

            // Declare a new command line builder
            return(await new CommandLineBuilder(documentCommand)
                   .UseDefaults()
                   .UseExceptionHandler(ExceptionFilter.Handle)
                   .UseMiddleware(MeasureMiddleware.Handle)
                   .Build()
                   .InvokeAsync(args));
        }
예제 #2
0
        public static async Task <int> MainAsync(string[] args)
        {
            //Default data source.
            var dataFileName = ConfigurationManager.AppSettings["dataFilename"];

            // Default format of data storage (Xml, Json atm.)
            Type dataStoreFormat = typeof(Xmlformat);

            // Default data type
            Type dataType = typeof(AppData);

            // Initiate data manager class
            var store = new FileStore(dataFileName, dataStoreFormat, dataType);

            // Initiate Application logger
            var logger = new AppLogger("log.txt");

            // Reading data of default data type from default source.
            // If commands needs to operate with different data type and/or location,
            // store.Read() should be called at the begining of Command handler InvokeAsync method.
            var data = store.Read();

            if (data == null)
            {
                // Log warning and go on with code
                logger.Responses.Add(new LogResponse {
                    Logger          = "Main",
                    MessageLogged   = false,
                    ResponseMessage = "Can't load/missing application data needed for at least some commands."
                });
            }

            logger.Write(MsgCategory.Info, "Application started.", console: false, file: true);

            // Building command line commands and parsing provided arguments
            try
            {
                RootCommand command = new RootCommand
                {
                    ServerListCommand.Create(),
                            ConfigCommand.Create(),
                            ExportCommand.Create(),
                            LoggerCommand.Create()
                };
                Parser parser = BuildParser(command, logger, store, data);
                await parser.InvokeAsync(args);
            }
            catch (Exception ex)
            {
                logger.Write(MsgCategory.Error, "Application terminated: " + ex.Message, console: true, file: true);
                return(0);
            }

            // If app settings for warnings are enabled
            // Informing user about warnings before exit
            var appWarn = logger.Responses.Where(rs => rs.MessageLogged == false).Distinct().ToList();

            if (appWarn.Count > 0)
            {
                logger.Write(MsgCategory.Info, "Application completed with warnings:", console: true, file: false);
                foreach (var response in appWarn)
                {
                    logger.Write(MsgCategory.Warning, message: response.Logger + ": " + response.ResponseMessage, console: true, file: true);
                }
                return(1);
            }

            logger.Write(MsgCategory.Info, "Application completed.", console: false, file: true);
            return(1);
        }