/// <summary>
        ///   Creates and configures the parser responsible for evaluating the command line arguments.
        /// </summary>
        /// 
        /// <returns>The <see cref="T:Fclp.FluentCommandLineParser"/> instance configured for the applications arguments.</returns>
        ///
        private static FluentCommandLineParser<CommandLineContext.Arguments> CreateParser()
        {
            var parser = new FluentCommandLineParser<Arguments>();

              parser.IsCaseSensitive = false;

              parser.Setup(arg => arg.Help)
            .As('?', "Help")
            .SetDefault(false);

              parser.Setup(arg => arg.TestSuitePath)
            .As("TestSuitePath")
            .Required();

              parser.Setup(arg => arg.TestScriptContainer)
            .As("TestScriptContainer")
            .SetDefault(Environment.CurrentDirectory);

              parser.Setup(arg => arg.TestScriptPath)
            .As("TestScriptPath")
            .SetDefault(Path.Combine(parser.Object.TestScriptContainer ?? Environment.CurrentDirectory, "jasmine-runner.js"));

              parser.Setup(arg => arg.PhantomPath)
            .As("PhantomPath")
            .SetDefault(Path.Combine(parser.Object.TestScriptContainer ?? Environment.CurrentDirectory, "phantomjs.exe"));

              return parser;
        }
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler;

            var parser = new FluentCommandLineParser<AppArgs>();
            parser.Setup(arg => arg.SiteUrl).As('s', "siteUrl").SetDefault(null).Required();
            parser.Setup(arg => arg.ListNames).As('l', "listNames").SetDefault(null).Required();
            parser.Setup(arg => arg.UserName).As('u', "userName").SetDefault(null).Required();
            parser.Setup(arg => arg.Password).As('p', "password").SetDefault(null).Required();

            var result = parser.Parse(args);

            if (result.HasErrors == false &&
                string.IsNullOrEmpty(parser.Object.SiteUrl) == false &&
                string.IsNullOrEmpty(parser.Object.ListNames) == false &&
                string.IsNullOrEmpty(parser.Object.UserName) == false &&
                string.IsNullOrEmpty(parser.Object.Password) == false)
            {
                var lists = parser.Object.ListNames.Split(',');

                foreach (var list in lists)
                {
                    // Get custom list items
                    var listDataTable = SharePointHelper.GetAllListItems(parser.Object.SiteUrl, list, parser.Object.UserName, parser.Object.Password);

                    // Save custom list items as Excel
                    ExcelHelper.SaveList(listDataTable);
                }

            }
            else
            {
                Console.WriteLine(result.ErrorText);
            }
        }
 public ApplicationArgumentsParser()
 {
     _parser = new FluentCommandLineParser<ApplicationArguments>();
     var version = new Cadmus.Foundation.AppVersionInfo();
     Version = version.GetCurrentVersion();
     Setup();
 }
 public static CommandLineArgs Parse(string[] args)
 {
     var parser = new FluentCommandLineParser<CommandLineArgs>();
     parser.Setup(arg => arg.Mode)
         .As('m', "mode")
         .WithDescription("Mode {search|crop|flip|resize|split}")
         .Required();
     parser.Setup(arg => arg.TargetDirectory)
         .As('t', "target")
         .WithDescription("Target directory for images")
         .Required();
     parser.Setup(arg => arg.CategoriesFile)
         .As('c', "categories")
         .WithDescription("Path to the CSV file of categories and search terms - required for search mode");
     parser.Setup(arg => arg.SearchCount)
         .As('r', "results")
         .WithDescription("Number of search results per category - default 100");
     parser.Setup(arg => arg.ImageSize)
         .As('s', "size")
         .WithDescription("Image dimensions for resizing (square) - default 50");
     parser.Setup(arg => arg.TestNumber)
         .As('n', "testnumber")
         .WithDescription("Number of image groups to use for testing (vs training) - default 30");
     parser.SetupHelp("?", "help")
         .Callback(text => Console.WriteLine(text));
     var parseResult = parser.Parse(args);
     if (parseResult.HasErrors || !parser.Object.IsValid())
     {
         Console.WriteLine("Usage:");
         parser.HelpOption.ShowHelp(parser.Options);
         return null;
     }
     return parser.Object;
 }
Exemple #5
0
        public static bool TryGetArguments(string[] args, out ArgParser parsedArguments)
        {
            var argumentsParser = new FluentCommandLineParser<ArgParser>();
            argumentsParser.Setup(a => a.InputFileName)
                .As('i', "input")
                .WithDescription("Input file name")
                .Required();

            argumentsParser.Setup(a => a.OutputFileName)
                .As('o', "output")
                .WithDescription("Output file name")
                .Required();

            argumentsParser.SetupHelp("?", "h", "help")
                .Callback(text => Console.WriteLine(text));

            var parsingResult = argumentsParser.Parse(args);

            if (parsingResult.HasErrors)
            {
                argumentsParser.HelpOption.ShowHelp(argumentsParser.Options);
                parsedArguments = null;
                return false;
            }

            parsedArguments = argumentsParser.Object;
            return !parsingResult.HasErrors;
        }
Exemple #6
0
        //mono Xamaridea.Console.exe -p /Users/bja/Workspaces/Xamarin/SomeProj/Droid/SomeProj.Droid.csproj
        static void Main(string[] args)
        {
            var parser = new FluentCommandLineParser<ApplicationArguments> ();

            //AndroidStudioPath

            var pAS = parser.Setup<string> (arg => arg.AndroidStudioPath)
                .As ('a', "aspath")
                .WithDescription ("Path to Android Studio application");
            try {
                pAS.SetDefault (AndroidIdeDetector.TryFindIdePath ());
            } catch (System.Exception ex) {
                pAS.Required ();
            }

            // XamarinProjectPath

            parser.Setup<string> (arg => arg.XamarinProjectPath)
                .As ('p', "project")
                .WithDescription ("Path to a Xamarin.Android .csproj file")
                .Required ();

            // AndroidSDKPath

            parser.Setup<string> (arg => arg.AndroidSDKPath)
                .As ('s', "sdk")
                .WithDescription ("Path to the Android SDK folder")
                .SetDefault (null);

            // CustomTemplatePath

            parser.Setup<string> (arg => arg.CustomTemplatePath)
                .As ('t', "template")
                .WithDescription ("Path to a custom android project template (zip or directory)")
                .SetDefault (null);

            // Help
            parser.SetupHelp ("?", "h", "help")
                .Callback (text => System.Console.WriteLine (text));

            // Parse args

            var result = parser.Parse (args);

            //
            if (!result.HelpCalled) {
                if (!result.HasErrors) {
                    //Task.Run(async () => await new ConsoleReceiver().RunAsync(parser.Object));
                    new ConsoleReceiver ().RunAsync (parser.Object); // TODO: async : ticket #6
                } else {
                    if (result.ErrorText != null)
                        System.Console.WriteLine (result.ErrorText);
                    parser.HelpOption.ShowHelp (parser.Options);
                }
            }
        }
Exemple #7
0
        CLIOptions parse(string[] args)
        {
            var p = new FluentCommandLineParser<CLIOptions>();

            p.Setup(arg => arg.verbose)
                .As('v', "verbose")
                .WithDescription("Verbose protocol")
                .SetDefault(false);
            p.Setup(arg => arg.pedantic)
                .As('p', "pedantic")
                .WithDescription("Pedantic subjects")
                .SetDefault(false);
            p.Setup(arg => arg.reconnectDelay)
                .As('r', "reconnectDelay")
                .WithDescription("Delay between reconnects (milliseconds)")
                .SetDefault(500);
            p.Setup(arg => arg.uris)
                .As('u', "uris")
                .WithDescription("List of server URIs")
                .SetDefault(new List<string> {"nats://localhost:4222"});
                //.Required();

            p.Setup(arg => arg.loglevel)
                .As('l', "loglevel")
                .WithDescription("Log Level")
                .SetDefault("WARN");
            p.Setup(arg => arg.mode)
                .As('m', "mode")
                .WithDescription("Mode (PUB|SUB)")
                .Required();
            p.Setup(arg => arg.subject)
                .As('s', "subject")
                .WithDescription("Subject")
                .Required();
            p.Setup(arg => arg.data)
                .As('d', "data")
                .WithDescription("Data or path to file containing data to publish");
                //.Required();
            p.Setup(arg => arg.count)
                .As('c', "count")
                .WithDescription("Number of items to publish/receive")
                .SetDefault(10);

            p.SetupHelp("?", "h", "help")
                .Callback(text => Console.Out.WriteLine(text));

            var result = p.Parse(args);
            if (result.HasErrors)
            {
                p.HelpOption.ShowHelp(p.Options);
                throw new Exception(result.ErrorText);
            }

            return p.Object;
        }
Exemple #8
0
 static FluentCommandLineParser<ApplicationArguments> GetParser()
 {
     var p = new FluentCommandLineParser<ApplicationArguments>();
     p.Setup(arg => arg.TargetPath)
         .As("targetPath")
         .SetDefault(string.Empty);
     p.Setup(arg => arg.ProjectPath)
         .As("projectPath")
         .Required();
     return p;
 }
        static void Main(string[] args)
        {
            args = new string[] { "--action", "makewarehousesales", "--", "starttime=01/01/2016", "endtime=12/31/2016" };
            //args = new string[] { "--action", "deletedatabase" };

            try
            {
                log.Info("Application Started");
                string argstring = string.Empty;

                foreach (var arg in args)
                {
                    argstring += arg + ' ';
                }

                argstring = argstring.TrimEnd();
                log.Info("Parameters: {0}", argstring);

                applicationConfig = new ApplicationConfiguration();
                fclp = new FluentCommandLineParser();

                fclp.Setup<string>("action")
                    .CaptureAdditionalArguments(applicationConfig.Options.AddRange)
                    .WithDescription("What action to perform with the application")
                    .Required()
                    .Callback(x => applicationConfig.Action = x);

                var results = fclp.Parse(args);
                
                if (results.HasErrors == false || results.HelpCalled)
                {                    
                    DoWork();
                }
                else
                {
                    ShowHelp();
                }
            }
            catch (Exception e)
            {
                log.Error(e.Message.Trim());
                if (e.InnerException != null)
                {
                    log.Error(e.InnerException.Message.Trim());
                }
                log.Error(e.StackTrace);
            }
            finally
            {
                log.Info("Application Finished");
                Console.ReadKey();
            }
        }
Exemple #10
0
    static void Main(string[] args)
    {
       var p = new FluentCommandLineParser<ApplicationArguments>();

       // specify which property the value will be assigned too.
       p.Setup(arg => arg.Directory)
        .As('d', "directory").WithDescription("Directory to process")
        .Required();

      p.Setup(arg => arg.CSVFile)
        .As('f', "file")
        .WithDescription("CSV File name");


       p.Setup(arg => arg.Log)
        .As('l', "log")
        .WithDescription("Log output")
        .SetDefault(false); // use the standard fluent Api to define a default value if non is specified in the arguments
      p.SetupHelp("h", "?", "help")
        .Callback(s => Console.WriteLine(s));


       var result = p.Parse(args);
       if(result.HelpCalled) return;
       if(result.HasErrors == false)
       {
         //C:\Users\10063026\Desktop\BDS-MCTGCTV
         ApplicationArguments options = (ApplicationArguments)p.Object;
         var results = ProcessDirectory(options.Directory);
         Console.WriteLine(results.Count+" UAFs processed");

         if (options.CSVFile != null)
         {
           TextWriter file = new StreamWriter(options.CSVFile);
           var csv = new CsvHelper.CsvWriter(file);
           csv.WriteRecords(results);
           file.Flush();
         }
         else
         {
           CsvWriter csv = new CsvHelper.CsvWriter(Console.Out);
           csv.WriteRecords(results);
         }

       }
       else
       {
         Console.WriteLine(result.ErrorText);
       }



    }
        public FluentCommandLineParser<Arguments> Create()
        {
            var parser = new FluentCommandLineParser<Arguments>();
            parser.Setup(arg => arg.FileNames)
                .As('f', "file")
                .WithDescription("The path to the file.")
                .Required();

            parser.SetupHelp("?", "help")
                .Callback(text => Console.WriteLine(text));

            return parser;
        }
        public static FluentCommandLineParser<Arguments> Build()
        {
            var parser = new FluentCommandLineParser<Arguments>();

            parser.Setup(p => p.Path)
                .As('p', "path")
                .Required()
                .WithDescription("The path to the data file.");

            parser.SetupHelp("?", "help")
                .Callback(text => System.Console.WriteLine("Please, specify the path to the data file."));

            return parser;
        }
Exemple #13
0
        private static FluentCommandLineParser MockedArgsMapper(
            ArgsSpecification specs)
        {
            var mapper = new FluentCommandLineParser();

            foreach (var option in specs.Options)
            {
                mapper
                    .Setup<string>(option.Short, option.Long)
                    .Callback(option.Callback);
            }

            return mapper;
        }
        static void Main(string[] args)
        {
            string tfsCollectionUri = string.Empty;
            string[] tfsProjectNames = new string[0];
            string[] tfsWorkItemTypes = new string[0];
            string[] fromFields = new string[0];
            string toField = string.Empty;
            bool deleteFromFieldValue = false;

            var parser = new FluentCommandLineParser();
            parser.Setup<string>('c', "collection").Callback(p => tfsCollectionUri = p).Required();
            parser.Setup<string>('p', "projectNames").Callback(p => tfsProjectNames = p.Split(',')).Required();
            parser.Setup<string>('w', "workItemTypes").Callback(p => tfsWorkItemTypes = p.Split(',')).Required();
            parser.Setup<string>('f', "fromFields").Callback(p => fromFields = p.Split(',')).Required();
            parser.Setup<string>('t', "toField").Callback(p => toField = p).Required();
            parser.Setup<bool>('d', "deleteFromFieldValue").Callback(p => deleteFromFieldValue = p);

            parser.SetupHelp("?", "help").Callback(text => Console.WriteLine(text));

            parser.Parse(args);

            Console.WriteLine("TFS Team Project Collection Uri: {0}", tfsCollectionUri);
            Console.WriteLine("Projects: {0}", string.Join(", ", tfsProjectNames.Select(s => "'" + s + "'")));
            Console.WriteLine("WITs: {0}", string.Join(", ", tfsWorkItemTypes.Select(s => "'" + s + "'")));
            Console.WriteLine("From Field: {0}", string.Join(", ",fromFields));
            Console.WriteLine("To Field: {0}", toField);

            Console.WriteLine("Continue?");
            var confirm = Console.ReadLine();
            if (confirm.ToLowerInvariant().StartsWith("y"))
            {
                if (Uri.IsWellFormedUriString(tfsCollectionUri, UriKind.Absolute))
                {
                    FieldCopy.CopyFieldValues(new Uri(tfsCollectionUri), tfsProjectNames, tfsWorkItemTypes, fromFields, toField, deleteFromFieldValue);

                    // sample for providing a history value matching lambda function to get the right history value (newest will be checked first
                    //FieldCopy.CopyFieldValues(new Uri(tfsCollectionUri), tfsProjectNames, tfsWorkItemTypes, fromField, toField, false, val => val != null && long.Parse(val.ToString()) <= 1000);
                }
                else
                {
                    Console.WriteLine("Team Project Collection Uri is not well-formed!");
                }
            }
            else
            {
                Console.WriteLine("Cancelled!");
            }
            Console.WriteLine("<ENTER> to exit?");
            Console.ReadLine();
        }
Exemple #15
0
        static void Main(string[] args)
        {
            // parse and set up parameters
            var p = new FluentCommandLineParser();
            p.Setup<string>('s', "source")
                .Callback(source => _sourcePath = source)
                .Required();

            p.Setup<string>('d', "destination")
                .Callback(destination => _destinationPath = destination)
                .Required();

            p.Setup<bool>("silent")
                .Callback(silent => _silent = silent)
                .SetDefault(false);

            var parseresult = p.Parse(args);

            if (parseresult.HasErrors)
            {
                Console.WriteLine("An error has occured.");
                Console.WriteLine(parseresult.ErrorText);
                if (!_silent)
                {
                    Console.WriteLine("Press any key to continue.");
                    Console.ReadLine();
                }
                return;
            }

            // prepare anime name recognition list
            PrepareAnimeList();

            // prepare move list
            var result = PrepareMoveList();

            // execute move
            MoveAnime(result);

            // finish

            var temp = Directory.EnumerateFiles(_sourcePath);
            var path = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
            File.WriteAllLines(Path.Combine(path, "output.txt"), temp.Select(x => Path.GetFileName(x)));
            //Console.ReadLine();
        }
Exemple #16
0
        public static FluentCommandLineParser MapArgsForDispatcher(
            CommandDispatcher dispatcher)
        {
            var mapper = new FluentCommandLineParser();

            mapper
                .Setup<string>('c', "command")
                .Callback(command => dispatcher.Command = (Command) Enum.Parse(typeof(Command), command))
                .Required();

            mapper
                .Setup<string>('p', "project")
                .Callback(project => dispatcher.Project = project)
                .Required();

            return mapper;
        }
        public static FluentCommandLineParser<ApplicationArguments> CreateCommandLineParser()
        {
            var parser = new FluentCommandLineParser<ApplicationArguments>();

            parser.Setup(arg => arg.ResultFolder)
                .As('o', "outputFolder")
                .Required()
                .WithDescription("A folder that will contain data from different harvesters");

            parser.Setup(arg => arg.ServiceName)
                  .As('n', "name")
                  .WithDescription("The name of the service to be registered.");

            parser.SetupHelp("?", "help")
                .Callback(text => Console.WriteLine(text));

            return parser;
        }
        private static AddCommand BuildAddCommand(FluentCommandLineParser parser, string[] args)
        {
            var command = new AddCommand();

            parser
                .Setup<string>('n', "name")
                .Callback(x => command.Name = x)
                .Required();

            parser
                .Setup<string>('s', "solution")
                .Callback(x => command.Solution = x);

            parser
                .Setup<string>('t', "template")
                .Callback(x => command.Template = x)
                .Required();

            parser
                .Setup<string>('l', "location")
                .Callback(x => command.Location = x)
                .SetDefault(Environment.CurrentDirectory);

            parser
                .Setup<string>('p', "templatepaths")
                .Callback(x => command.TemplatePaths.AddRange((x ?? "").Split(';').Where(y => !string.IsNullOrEmpty(y))));

            parser
                .Setup<string>('g', "guid")
                .Callback(x => command.ProjectGuid = x)
                .SetDefault(Guid.NewGuid().ToString());

            parser
                .Setup<string>('o', "output")
                .Callback(x => command.LogTo = x);

            parser.Parse(args);

            command.TemplatePaths.Add(Path.Combine(command.Location, "Templates"));
            command.TemplatePaths.Add(Path.Combine(
                Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? "", "Templates"));

            return command;
        }
Exemple #19
0
        public Args Parse(string[] args)
        {
            if (args == null)
                throw new ArgumentException(nameof(args));

            var parser = new FluentCommandLineParser();
            var parsedArgs = new Args();

            parser.Setup<string>('t')
                .Callback(type => parsedArgs.Type = type)
                .Required();
            parser.Setup<string>('v')
                .Callback(value => parsedArgs.Value = value)
                .Required();

            parser.Parse(args);

            return parsedArgs;
        }
        public static void Main(string[] args)
        {
            var parser = new FluentCommandLineParser();

            var commandName = args.FirstOrDefault() ?? "";

            if (!CommandBuilders.ContainsKey(commandName))
            {
                Console.WriteLine(
                    $"{commandName} isn't a valid command. Available commands: {string.Join(", ", CommandBuilders.Select(x => x.Key))}.");
                return;
            }

            var commandArgs = args.Skip(1).ToArray();

            var command = CommandBuilders[commandName](parser, commandArgs);

            command.Execute().Wait();
        }
        private static CommandLineArguments GetCommandLineArguments(string[] args)
        {
            var p = new FluentCommandLineParser<CommandLineArguments>();
            p.Setup(arg => arg.SourceConnectionString).As('s', "source").Required();
            p.Setup(arg => arg.DestinationConnectionString).As('d', "destination").Required();
            p.Setup(arg => arg.Verbose).As('v', "verbose").SetDefault(false);
            p.Setup(arg => arg.DryRun).As('t', "dryRun").SetDefault(false);

            var result = p.Parse(args);
            if (result.HasErrors)
            {
                Console.WriteLine(result.ErrorText);
                Environment.ExitCode = 1;
                return null;
            }

            var commandLine = p.Object;
            return commandLine;
        }
        public void GenericFclp_Uri()
        {
            const char shortKey = 'u';
            const string longKey = "uri";
            const string uri = "http://services.internal/backoffce/service/svc";

            var variations = CreateAllKeyVariations(shortKey, longKey, uri).ToList();

            foreach (var combination in variations)
            {
                var fclp = new FluentCommandLineParser<ExampleArgsContainer>();

                fclp.Setup(args => args.Uri)
                    .As(shortKey, longKey);

                var result = fclp.Parse(combination.Args);

                Assert.IsEmpty(result.Errors);
                Assert.IsEmpty(result.AdditionalOptionsFound);
                Assert.AreEqual(uri, fclp.Object.Uri.AbsoluteUri);
            }
        }
        public FluentCommandLineParser<Arguments> Create()
        {
            var parser = new FluentCommandLineParser<Arguments>();
            parser.Setup(arg => arg.FileName)
                .As('f', "file")
                .WithDescription("The path to the file.")
                .Required();

            parser.Setup(arg => arg.LineCount)
                .As('l', "lineCount")
                .WithDescription("Optional. The number of lines in the result file. Default is 12.")
                .SetDefault(12);

            parser.Setup(arg => arg.NumbersPerLine)
                .As('n', "numbersPerLine")
                .WithDescription("Optional. How many numbers will be placed in the line. Default is 30.")
                .SetDefault(30);

            parser.SetupHelp("?", "help")
                .Callback(text => Console.WriteLine(text));

            return parser;
        }
        public static FluentCommandLineParser<ApplicationArguments> CreateCommandLineParser()
        {
            var parser = new FluentCommandLineParser<ApplicationArguments>();

            parser.Setup(arg => arg.FolderToMonitor)
                .As('i', "inputFolder")
                .Required()
                .WithDescription("A folder that will be monitored for file changes.");

            parser.Setup(arg => arg.StorageFolder)
                .As('o', "outputFolder")
                .Required()
                .WithDescription("A destination folder for the files from the source folder.");

            parser.Setup(arg => arg.ServiceName)
                  .As('n', "name")
                  .WithDescription("The name of the service to be registered.");

            parser.SetupHelp("?", "help")
                .Callback(text => Console.WriteLine(text));

            return parser;
        }
        public static FluentCommandLineParser<ApplicationArguments> CreateCommandLineParser()
        {
            var parser = new FluentCommandLineParser<ApplicationArguments>();

            parser.Setup(arg => arg.FolderToMonitor)
                .As('i', "inputFolder")
                .Required()
                .WithDescription("A folder that will be monitored for file changes.");

            parser.Setup(arg => arg.HostName)
                .As('h', "hostName")
                .Required()
                .WithDescription("A host name for messaging queue.");

            parser.Setup(arg => arg.ServiceName)
                  .As('n', "name")
                  .WithDescription("The name of the service to be registered.");

            parser.SetupHelp("?", "help")
                .Callback(text => Console.WriteLine(text));

            return parser;
        }
Exemple #26
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            bool console = false;
            var p = new FluentCommandLineParser();
            p.Setup<bool>('c', "console").Callback(c => console = c);
            p.Parse(args);

            if (console)
            {
                var service = new Service();
                service.Start(args);
                Console.ReadLine();
                service.Stop();
            }
            else
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
			    { 
				    new Service() 
			    };
                ServiceBase.Run(ServicesToRun);
            }
        }
        static void Main(string[] args)
        {
            // get input/output directories
            var p = new FluentCommandLineParser <ApplicationArguments>();

            p.Setup(arg => arg.spectraFilePath)
            .As('i', "spectraFilePath")
            .Required();

            p.Setup(arg => arg.moveTo)
            .As('o', "moveTo")
            .Required();

            var result = p.Parse(args);

            string spectraFilePath = p.Object.spectraFilePath;
            string moveTo          = p.Object.moveTo;

            // get last 5 results files from input directory and copy them to output directory
            if (Directory.Exists(spectraFilePath))
            {
                List <List <string> > allStrings = new List <List <string> >();
                // initial search results
                List <string> initialSearchPsmResults = new List <string>();
                initialSearchPsmResults.Add(">Initial search PSM results:");
                allStrings.Add(initialSearchPsmResults);

                List <string> initialSearchTime = new List <string>();
                initialSearchTime.Add("Initial search time:");
                allStrings.Add(initialSearchTime);

                // calibration results
                List <string> calibrationTimeToRun = new List <string>();
                calibrationTimeToRun.Add(">Calibration run time:");
                allStrings.Add(calibrationTimeToRun);

                // post-calibration search results
                List <string> postCalibrationSearchPsmResults = new List <string>();
                postCalibrationSearchPsmResults.Add(">Post-calibration search PSM results:");
                allStrings.Add(postCalibrationSearchPsmResults);

                List <string> postCalibrationSearchTime = new List <string>();
                postCalibrationSearchTime.Add("Post-calibration search time:");
                allStrings.Add(postCalibrationSearchTime);

                // gptmd results
                List <string> gptmdModsAdded = new List <string>();
                gptmdModsAdded.Add(">GPTMD modifications added:");
                allStrings.Add(gptmdModsAdded);

                List <string> gptmdTimeToRun = new List <string>();
                gptmdTimeToRun.Add("GPTMD run time:");
                allStrings.Add(gptmdTimeToRun);

                // post-gptmd search results
                List <string> postGptmdSearchPsmResults = new List <string>();
                postGptmdSearchPsmResults.Add(">Post-GPTMD search PSM results:");
                allStrings.Add(postGptmdSearchPsmResults);

                List <string> postGptmdSearchTime = new List <string>();
                postGptmdSearchTime.Add("Post-GPTMD search time:");
                allStrings.Add(postGptmdSearchTime);

                // parse the results files
                var      resultsFolders = Directory.EnumerateDirectories(spectraFilePath).OrderBy(v => Directory.GetCreationTime(v)).ToList();
                var      fiveMostRecentlyMadeDirectories = resultsFolders.Take(5).ToList();
                string[] lastFiveResultsPaths            = new string[5];

                for (int i = 0; i < fiveMostRecentlyMadeDirectories.Count; i++)
                {
                    var files     = Directory.GetFiles(fiveMostRecentlyMadeDirectories[i]);
                    var resultTxt = files.Where(v => Path.GetFileName(v).Equals("allResults.txt"));

                    if (resultTxt.Any())
                    {
                        lastFiveResultsPaths[i] = resultTxt.First();
                    }
                    else
                    {
                        lastFiveResultsPaths[i] = null;
                    }
                }

                Directory.CreateDirectory(moveTo);

                for (int i = 0; i < lastFiveResultsPaths.Length; i++)
                {
                    if (lastFiveResultsPaths[i] != null)
                    {
                        File.Copy(lastFiveResultsPaths[i], Path.Combine(moveTo, "allResults" + (i + 1) + ".txt"), true);
                        var time = File.GetCreationTime(lastFiveResultsPaths[i]);

                        var lines      = File.ReadAllLines(lastFiveResultsPaths[i]);
                        int taskNumber = 0;
                        foreach (var line in lines)
                        {
                            if (line.Contains("Time to run task: "))
                            {
                                taskNumber++;
                            }

                            // initial search
                            if (taskNumber == 1)
                            {
                                if (line.Contains("All target PSMS within 1% FDR:"))
                                {
                                    initialSearchPsmResults.Add("\t" + time + "\t---\t" + line);
                                }
                                else if (line.Contains("Time to run task:"))
                                {
                                    initialSearchTime.Add("\t" + time + "\t---\t" + line);
                                }
                            }

                            // calibration
                            if (taskNumber == 2)
                            {
                                if (line.Contains("Time to run task:"))
                                {
                                    calibrationTimeToRun.Add("\t" + time + "\t---\t" + line);
                                }
                            }

                            // post-calibration search
                            if (taskNumber == 3)
                            {
                                if (line.Contains("All target PSMS within 1% FDR:"))
                                {
                                    postCalibrationSearchPsmResults.Add("\t" + time + "\t---\t" + line);
                                }
                                else if (line.Contains("Time to run task:"))
                                {
                                    postCalibrationSearchTime.Add("\t" + time + "\t---\t" + line);
                                }
                            }

                            // gptmd
                            if (taskNumber == 4)
                            {
                                if (line.Contains("Modifications added:"))
                                {
                                    gptmdModsAdded.Add("\t" + time + "\t---\t" + line);
                                }
                                else if (line.Contains("Time to run task:"))
                                {
                                    gptmdTimeToRun.Add("\t" + time + "\t---\t" + line);
                                }
                            }

                            // post-gptmd search
                            if (taskNumber == 5)
                            {
                                if (line.Contains("All target PSMS within 1% FDR:"))
                                {
                                    postGptmdSearchPsmResults.Add("\t" + time + "\t---\t" + line);
                                }
                                else if (line.Contains("Time to run task:"))
                                {
                                    postGptmdSearchTime.Add("\t" + time + "\t---\t" + line);
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var stringList in allStrings)
                        {
                            stringList.Add("\t" + "--");
                        }
                    }
                }

                List <string> output = new List <string>();
                foreach (var listOfStrings in allStrings)
                {
                    if (listOfStrings.First().Contains(">"))
                    {
                        output.Add("---------------------------------------------------------------------------------\n");
                    }

                    foreach (var myString in listOfStrings)
                    {
                        output.Add(myString);
                    }
                    output.Add("\n");
                }

                File.WriteAllLines(Path.Combine(moveTo, "ProcessedResults.txt"), output);
            }
        }
Exemple #28
0
 public override void Configure(FluentCommandLineParser <SelfHostingArguments> parser)
 {
     parser.Setup(o => o.ValidationPort)
     .As("validationport")
     .WithDescription("Port to use for listening to validation requests. Note that the ACME server will always send requests to port 443. This option is only useful in combination with a port forwarding.");
 }
Exemple #29
0
        static async Task Main(string[] args)
        {
            bool helpRequested = false;
            var  parser        = new FluentCommandLineParser <DumpOptions>();

            parser.Setup(arg => arg.Target)
            .As('t', "target")
            .SetDefault("./")
            .WithDescription("Target appsettings.json file or directory with appsettings json files");

            parser.Setup(arg => arg.OutputFile)
            .As('o', "output-dir")
            .SetDefault("./")
            .WithDescription("Output directory to write result in format appsettings.{env?}.env");

            parser.Setup(arg => arg.Environment)
            .As('e', "env")
            .Required()
            .WithDescription("The name of the environment to set in appsettings.{env}.json during the load");

            parser.Setup(arg => arg.WithValues)
            .As('v', "values")
            .SetDefault(false)
            .WithDescription("Dump values along with keys");

            parser.Setup(arg => arg.WithLocal)
            .As('l', "local")
            .SetDefault(false)
            .WithDescription("For folder mode, load appsettings.Local.json if found");

            parser.Setup(arg => arg.WithDuplicates)
            .As('d', "duplicates")
            .SetDefault(false)
            .WithDescription("Dump everything even if base appsettings has the same value");

            parser.Setup(arg => arg.Separator)
            .As('s', "separator")
            .SetDefault("__")
            .WithDescription("Separator for sections, i.e. __ or :: etc");

            parser.SetupHelp("?", "help")
            .UseForEmptyArgs()
            .Callback(text =>
            {
                Console.WriteLine(text);
                helpRequested = true;
            });

            var result = parser.Parse(args);

            if (result.HasErrors)
            {
                Console.WriteLine(result.ErrorText);
                parser.HelpOption.ShowHelp(parser.Options);
                return;
            }

            var options = parser.Object;

            if (!helpRequested)
            {
                var dumper = new Dumper(options);
                await dumper.Dump();
            }
        }
 public BaseArgumentsProvider()
 {
     _parser = new FluentCommandLineParser <T>();
     _parser.IsCaseSensitive = false;
     Configure(_parser);
 }
Exemple #31
0
        static void Main(string[] args)
        {
            var p = new FluentCommandLineParser();

            p.SetupHelp("help", "h")
            .Callback(() => Console.WriteLine("Usage: \n DumpMemorySummarizer.exe ( -dump=[Dump filename] or -url=[remote RavenDB url] -databaseName=[database name])"))
            .UseForEmptyArgs();

            p.Setup <string>("dump")
            .Callback(record => dumpFilename = record)
            .Required()
            .WithDescription("Dump filename full path");

            p.Setup <string>("url")
            .Callback(record => url = record)
            .Required()
            .WithDescription("URL of RavenDB server");

            p.Setup <string>("databaseName")
            .Callback(record => databaseName = record)
            .SetDefault("DumpMemorySummary")
            .WithDescription("Destination database name");

            p.IsCaseSensitive = false;

            var parseResult = p.Parse(args);

            if (parseResult.HasErrors || String.IsNullOrWhiteSpace(dumpFilename))
            {
                Console.WriteLine(parseResult.ErrorText);
                return;
            }

            IDocumentStore store;

            if (String.IsNullOrWhiteSpace(url))
            {
                store = new EmbeddableDocumentStore
                {
                    RunInMemory           = false,
                    DefaultDatabase       = "DumpMemorySummary",
                    UseEmbeddedHttpServer = true
                };
            }
            else
            {
                store = new DocumentStore
                {
                    Url             = url,
                    DefaultDatabase = databaseName
                };
            }

            using (store)
                using (var session = ClrMDSession.LoadCrashDump(dumpFilename))
                {
                    store.Initialize();
                    store.DatabaseCommands.GlobalAdmin.EnsureDatabaseExists(databaseName);

                    new GcRootCountByKind().Execute(store);
                    new GcRootsCountByKindAndType().Execute(store);
                    new HeapObjectCountByType().Execute(store);
                    new HeapObjectStatisticsByType().Execute(store);

                    var heap = session.Heap;

                    if (heap.CanWalkHeap == false)
                    {
                        throw new ApplicationException("Cannot walk heap, aborting.");
                    }

                    using (var bulkInsert = store.BulkInsert(databaseName))
                    {
                        long rootCount = 0;
                        bulkInsert.Store(session.Runtime.GetThreadPool(), "threadpool/1");

                        Console.Write("Writing threads...");
                        int threadId = 1;
                        foreach (var thread in session.Runtime.Threads)
                        {
                            bulkInsert.Store(new Thread(thread));
                        }
                        Console.WriteLine("done");

                        Console.Write("Writing objects from finalizer queue...");
                        foreach (var finalizerObj in session.Runtime.EnumerateFinalizerQueue())
                        {
                            var clrType = heap.GetObjectType(finalizerObj);

                            if (clrType == null)
                            {
                                continue;
                            }

                            if (clrType.Name.Contains("Stream"))
                            {
                                HandleStreamObject(clrType, bulkInsert, finalizerObj);
                            }
                            else if (clrType.Name.Equals("Microsoft.Isam.Esent.Interop.Table"))
                            {
                                HandleEsentTable(clrType, bulkInsert, finalizerObj);
                            }
                            else
                            {
                                bulkInsert.Store(new FinalizerQueueObject
                                {
                                    TypeName = clrType.Name,
                                    Size     = clrType.BaseSize,
                                });
                            }
                        }
                        Console.WriteLine("done");

                        foreach (var root in heap.EnumerateRoots())
                        {
                            if (root != null && root.Type != null)
                            {
                                var clrType  = root.Type;
                                var rootInfo = new GcRoot
                                {
                                    Address  = root.Address,
                                    Name     = root.Name,
                                    RootKind = root.Kind,
                                    ObjectRefThatRootKeepsAlive = root.Object,
                                    TypeName = (clrType != null) ? clrType.Name : "<No Type>"
                                };
                                bulkInsert.Store(rootInfo);

                                List <ClrRoot> objectRoots;
                                if (gcRootsByObjRef.TryGetValue(root.Object, out objectRoots))
                                {
                                    objectRoots.Add(root);
                                }
                                else
                                {
                                    gcRootsByObjRef.Add(root.Object, new List <ClrRoot> {
                                        root
                                    });
                                }

                                Console.WriteLine("#{0}, {1}", ++rootCount, rootInfo);
                            }
                        }

                        long objectsEnumerated = 0;
                        foreach (var objRef in heap.EnumerateObjects())
                        {
                            objectsEnumerated++;
                            Console.WriteLine("Enumerated {0} objects from the heap", objectsEnumerated);
                            var clrType = heap.GetObjectType(objRef);
                            if (clrType == null)
                            {
                                Console.WriteLine("Warning: heap corrupted, could not determine object type");
                                continue;
                            }
                            var size       = clrType.GetSize(objRef);
                            var generation = heap.GetGeneration(objRef);
                            var objectInfo = new HeapObject
                            {
                                ObjRef      = objRef,
                                Generation  = generation,
                                Size        = size,
                                TypeName    = clrType.Name,
                                IsArray     = clrType.IsArray,
                                ArrayLength = clrType.IsArray ? clrType.GetArrayLength(objRef) : -1,
                                IsInLOH     = heap.IsInHeap(objRef),
                                GcRootPaths = new Dictionary <ulong, List <string> >()
                            };

//						List<ClrRoot> clrRoots;
//						if (gcRootsByObjRef.TryGetValue(objRef, out clrRoots))
//							foreach (var root in clrRoots)
//							{
//								//var path = PathToGcRoots(heap, objRef, root);
//								//objectInfo.GcRootPaths.Add(root.Address, path);
//							}

                            Console.WriteLine("#{0}, {1}", ++objectsEnumerated, objectInfo);
                            bulkInsert.Store(objectInfo);
                        }
                    }
                }
        }
Exemple #32
0
 public abstract void Configure(FluentCommandLineParser <T> parser);
 public override void Configure(FluentCommandLineParser <GodaddyArguments> parser)
 {
     _ = parser.Setup(o => o.ApiKey)
         .As("apikey")
         .WithDescription("GoDaddy API key.");
 }
Exemple #34
0
        private static void Main(string[] args)
        {
            SetupNLog();

            _logger = LogManager.GetLogger("EvtxECmd");

            _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.File)
            .As('f')
            .WithDescription("File to process. This or -d is required\r\n");
            _fluentCommandLineParser.Setup(arg => arg.Directory)
            .As('d')
            .WithDescription("Directory to process that contains evtx files. This or -f is required");

            _fluentCommandLineParser.Setup(arg => arg.CsvDirectory)
            .As("csv")
            .WithDescription(
                "Directory to save CSV formatted results to.");     // This, --json, or --xml required

            _fluentCommandLineParser.Setup(arg => arg.DateTimeFormat)
            .As("dt")
            .WithDescription(
                "The custom date/time format to use when displaying time stamps. Default is: yyyy-MM-dd HH:mm:ss.fffffff")
            .SetDefault("yyyy-MM-dd HH:mm:ss.fffffff");

            _fluentCommandLineParser.Setup(arg => arg.From)
            .As("from")
            .WithDescription(
                "The timestamp 'from' to include")
            .SetDefault("");

            _fluentCommandLineParser.Setup(arg => arg.To)
            .As("to")
            .WithDescription(
                "The timestamp up 'to' to include")
            .SetDefault("");

            _fluentCommandLineParser.Setup(arg => arg.IncludeIds)
            .As("inc")
            .WithDescription(
                "List of event IDs to process. All others are ignored. Overrides --exc Format is 4624,4625,5410")
            .SetDefault(string.Empty);

            _fluentCommandLineParser.Setup(arg => arg.ExcludeIds)
            .As("exc")
            .WithDescription(
                "List of event IDs to IGNORE. All others are included. Format is 4624,4625,5410")
            .SetDefault(string.Empty);

            _fluentCommandLineParser.Setup(arg => arg.Metrics)
            .As("met")
            .WithDescription(
                "When true, show metrics about processed event log. Default is TRUE.\r\n")
            .SetDefault(true);

            _fluentCommandLineParser.Setup(arg => arg.Debug)
            .As("debug")
            .WithDescription("Show debug information during processing").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.Trace)
            .As("trace")
            .WithDescription("Show trace information during processing\r\n").SetDefault(false);

            var header =
                $"\r\nsimple-evtx version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nAuthor: Mark Woan ([email protected])" +
                "\r\n\r\nhttps://github.com/EricZimmerman/evtx" +
                "\r\nhttps://github.com/woanware/simple-evtx";

            var footer =
                @"Examples: simple-evtx.exe -f ""C:\Temp\Application.evtx"" --csv ""c:\temp\out""" +
                "\r\n\t" +
                "  Short options (single letter) are prefixed with a single dash. Long commands are prefixed with two dashes\r\n";

            _fluentCommandLineParser.SetupHelp("?", "help")
            .WithHeader(header)
            .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (result.HelpCalled)
            {
                return;
            }

            if (result.HasErrors)
            {
                _logger.Error("");
                _logger.Error(result.ErrorText);

                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                return;
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() &&
                _fluentCommandLineParser.Object.Directory.IsNullOrEmpty())
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("-f or -d is required. Exiting");
                return;
            }

            _logger.Info(header);
            _logger.Info("");
            _logger.Info($"Command line: {string.Join(" ", Environment.GetCommandLineArgs().Skip(1))}\r\n");

            if (IsAdministrator() == false)
            {
                _logger.Fatal("Warning: Administrator privileges not found!\r\n");
            }

            if (_fluentCommandLineParser.Object.Debug)
            {
                LogManager.Configuration.LoggingRules.First().EnableLoggingForLevel(LogLevel.Debug);
            }

            if (_fluentCommandLineParser.Object.Trace)
            {
                LogManager.Configuration.LoggingRules.First().EnableLoggingForLevel(LogLevel.Trace);
            }

            LogManager.ReconfigExistingLoggers();

            var sw = new Stopwatch();

            sw.Start();

            var ts = DateTimeOffset.UtcNow;

            _errorFiles = new Dictionary <string, int>();

            if (_fluentCommandLineParser.Object.CsvDirectory.IsNullOrEmpty() == false)
            {
                if (Directory.Exists(_fluentCommandLineParser.Object.CsvDirectory) == false)
                {
                    _logger.Warn(
                        $"Path to '{_fluentCommandLineParser.Object.CsvDirectory}' doesn't exist. Creating...");

                    try
                    {
                        Directory.CreateDirectory(_fluentCommandLineParser.Object.CsvDirectory);
                    }
                    catch (Exception)
                    {
                        _logger.Fatal(
                            $"Unable to create directory '{_fluentCommandLineParser.Object.CsvDirectory}'. Does a file with the same name exist? Exiting");
                        return;
                    }
                }

                var outName = $"{ts:yyyyMMddHHmmss}-simple-evtx.csv";

                if (_fluentCommandLineParser.Object.CsvName.IsNullOrEmpty() == false)
                {
                    outName = Path.GetFileName(_fluentCommandLineParser.Object.CsvName);
                }

                var outFile = Path.Combine(_fluentCommandLineParser.Object.CsvDirectory, outName);

                _logger.Warn($"CSV output will be saved to '{outFile}'\r\n");

                try
                {
                    _swCsv     = new StreamWriter(outFile, false, Encoding.UTF8);
                    _csvWriter = new CsvWriter(_swCsv);
                }
                catch (Exception)
                {
                    _logger.Error($"Unable to open '{outFile}'! Is it in use? Exiting!\r\n");
                    Environment.Exit(0);
                }

                var foo = _csvWriter.Configuration.AutoMap <EventRecord>();
                //foo.Map(t => t.RecordPosition).Ignore();
                // foo.Map(t => t.Size).Ignore();
                //  foo.Map(t => t.Timestamp).Ignore();
                foo.Map(t => t.TimeCreated).Index(1);
                foo.Map(t => t.TimeCreated).ConvertUsing(t =>
                                                         $"{t.TimeCreated.ToString(_fluentCommandLineParser.Object.DateTimeFormat)}");
                foo.Map(t => t.EventId).Index(2);
                foo.Map(t => t.Provider).Index(3);
                foo.Map(t => t.Channel).Index(4);
                foo.Map(t => t.Computer).Index(5);
                foo.Map(t => t.Payload).Index(6);
                foo.Map(t => t.SourceFile).Index(7);

                _csvWriter.Configuration.RegisterClassMap(foo);
                _csvWriter.WriteHeader <EventRecord>();
                _csvWriter.NextRecord();
            }

            _includeIds = new HashSet <int>();
            _excludeIds = new HashSet <int>();

            if (_fluentCommandLineParser.Object.From.IsNullOrEmpty() == false)
            {
                try
                {
                    _from = DateTime.ParseExact(_fluentCommandLineParser.Object.From, "yyyy/MM/dd HH:mm", CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                    _logger.Error($"Invalid 'from' '{_fluentCommandLineParser.Object.From}' value! Exiting!\r\n");
                    Environment.Exit(0);
                }
            }

            if (_fluentCommandLineParser.Object.To.IsNullOrEmpty() == false)
            {
                try
                {
                    _to = DateTime.ParseExact(_fluentCommandLineParser.Object.To, "yyyy/MM/dd HH:mm", CultureInfo.InvariantCulture);
                }
                catch (Exception)
                {
                    _logger.Error($"Invalid 'to' '{_fluentCommandLineParser.Object.From}' value! Exiting!\r\n");
                }
            }

            if (_fluentCommandLineParser.Object.ExcludeIds.IsNullOrEmpty() == false)
            {
                var excSegs = _fluentCommandLineParser.Object.ExcludeIds.Split(',');

                foreach (var incSeg in excSegs)
                {
                    if (int.TryParse(incSeg, out var goodId))
                    {
                        _excludeIds.Add(goodId);
                    }
                }
            }

            if (_fluentCommandLineParser.Object.IncludeIds.IsNullOrEmpty() == false)
            {
                _excludeIds.Clear();
                var incSegs = _fluentCommandLineParser.Object.IncludeIds.Split(',');

                foreach (var incSeg in incSegs)
                {
                    if (int.TryParse(incSeg, out var goodId))
                    {
                        _includeIds.Add(goodId);
                    }
                }
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() == false)
            {
                if (File.Exists(_fluentCommandLineParser.Object.File) == false)
                {
                    _logger.Warn($"'{_fluentCommandLineParser.Object.File}' does not exist! Exiting");
                    return;
                }

                ProcessFile(_fluentCommandLineParser.Object.File);
            }
            else
            {
                _logger.Info($"Looking for event log files in '{_fluentCommandLineParser.Object.Directory}'");
                _logger.Info("");

                var f = new DirectoryEnumerationFilters();
                f.InclusionFilter = fsei => fsei.Extension.ToUpperInvariant() == ".EVTX";
                f.RecursionFilter = entryInfo => !entryInfo.IsMountPoint && !entryInfo.IsSymbolicLink;
                f.ErrorFilter     = (errorCode, errorMessage, pathProcessed) => true;

                var dirEnumOptions =
                    DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive |
                    DirectoryEnumerationOptions.SkipReparsePoints | DirectoryEnumerationOptions.ContinueOnException |
                    DirectoryEnumerationOptions.BasicSearch;

                var files2 =
                    Directory.EnumerateFileSystemEntries(Path.GetFullPath(_fluentCommandLineParser.Object.Directory), dirEnumOptions, f);

                foreach (var file in files2)
                {
                    ProcessFile(file);
                }
            }
            _swCsv?.Close();

            sw.Stop();
            _logger.Info("");

            var suff = string.Empty;

            if (_fileCount != 1)
            {
                suff = "s";
            }

            _logger.Error(
                $"Processed {_fileCount:N0} file{suff} in {sw.Elapsed.TotalSeconds:N4} seconds\r\n");

            if (_errorFiles.Count > 0)
            {
                _logger.Info("");
                _logger.Error("Files with errors");
                foreach (var errorFile in _errorFiles)
                {
                    _logger.Info($"'{errorFile.Key}' error count: {errorFile.Value:N0}");
                }

                _logger.Info("");
            }
        }
Exemple #35
0
        public static ProgramOptions CreateProgramOptions(string[] args)
        {
            ProgramOptions options = new ProgramOptions();
            var            p       = new FluentCommandLineParser();

            // example --inputFiles C:\file1.txt C:\file2.txt "C:\other file.txt"
            p.Setup <List <string> >('i', "inputFiles")
            .Callback(items => options.SetInputFiles(items))
            .Required()
            .WithDescription(@"Path for input files. By default it targets the test dlls. --inputFiles C:\file1.dll C:\file2.exe ""C:\other file.dll""");

            p.Setup <string>('o', "outputFile")
            .Callback(o => options.OutputFile = o)
            .WithDescription("Path to output file. Any previous extension will be removed and .bpl will be added. By default it is the same name and path of the first input file.");

            p.Setup <List <string> >('b', "bplFiles")
            .Callback(bs => options.SetBplFiles(bs))
            .WithDescription("Path to input bpl files that will be appended at the end of the resulting output file");

            p.Setup <bool>('l', "lineNumbers")
            .Callback(b => options.EmitLineNumbers = b)
            .WithDescription("Emit line numbers from source code in .bpl file. By default is true");

            p.Setup <bool>('e', "exceptions")
            .Callback(b => options.Exceptions = b)
            .WithDescription("Enable translation of exceptions handling.");

            p.Setup <bool>('s', "splitFields")
            .Callback(b => options.SplitFields = b)
            .WithDescription("Models the heap splitting instance fields into different dictionaries.");

            p.Setup <bool>('a', "atomicInitArray")
            .Callback(b => options.AtomicInitArray = b)
            .WithDescription("Handles atomic initialization of arrays.");

            p.Setup <bool>('t', "avoidSubtypeForInterfaces")
            .Callback(b => options.AvoidSubtypeCheckingForInterfaces = b)
            .WithDescription("Do not use subtypes hierarquies for variables/parameters of interface types (open world)");

            p.Setup <bool>("checkNullDereferences")
            .Callback(b => options.CheckNullDereferences = b)
            .WithDescription("Add assertions at every dereference checking that the reference is not null.");

            p.Setup <bool>('d', "DebugLargeDLL")
            .Callback(b => options.DebugLargeDLL = b)
            .WithDescription("Do not use this option.");

            p.Setup <bool>("SilentExceptionsForMethods")
            .Callback(b => options.SilentExceptionsForMethods = b)
            .WithDescription(
                "If InvalidOperationException is thrown during a method translation, " +
                "the translation will continue silently and the method will be replaced " +
                "with an extern method (not necessarily a sound over-approximation).");

            p.Setup <bool>('m', "NewAddrModelling")
            .Callback(b => options.NewAddrModelling = b)
            .WithDescription("Every variable of the three address code will be explicitly allocated in the boogie code. Every variable or field will have a memory address.");

            p.Setup <bool>("DebugLines")
            .Callback(b => options.DebugLines = b)
            .WithDescription("This settings forces the line numbers to be printed even when no input file exists (TinyBCT can be called without a file).");

            p.Setup <bool>('v', "Verbose")
            .Callback(b => options.Verbose = b)
            .WithDescription("Verbose output.");

            var result = p.Parse(args);

            if (result.HasErrors)
            {
                throw new Exception($"Unable to parse command line arguments: {result.ErrorText}");
            }

            return(options);
        }
Exemple #36
0
        public static CommandLineParameters FromArguments(string[] args)
        {
            var parser = new FluentCommandLineParser <CommandLineParameters>();

            parser.Setup(arg => arg.GitHubRepo)
            .As("github-repo")
            .Required();

            parser.Setup(arg => arg.GitHubToken)
            .As("github-token")
            .Required();

            parser.Setup(arg => arg.FileForVersion)
            .As("file-for-version")
            .Required();

            parser.Setup(arg => arg.IsPreRelease)
            .As("pre-release");

            parser.Setup(arg => arg.IssueLabelsWithHeader)
            .As("issue-labels");

            parser.Setup(arg => arg.IssueFilterLabel)
            .As("issue-filter-label");

            parser.Setup(arg => arg.ReleaseAttachments)
            .As("release-attachments");

            parser.Setup(arg => arg.IsUpdateOnly)
            .As("update-only");

            parser.Setup(arg => arg.IsChangelogFileCreationEnabled)
            .As("create-changelog-file");

            parser.Setup(arg => arg.IsDraft)
            .As("draft");

            parser.Setup(arg => arg.DeleteFilesAfterUpload)
            .As("delete-files-after-upload");

            var result = parser.Parse(args);

            var commandLineArguments = parser.Object;

            // Manual map
            if (parser.Object.IssueLabelsWithHeader != null)
            {
                parser.Object.IssueLabels = new Dictionary <string, string>();
                foreach (var issueLabelWithHeader in parser.Object.IssueLabelsWithHeader)
                {
                    var split = issueLabelWithHeader.Split(';');
                    parser.Object.IssueLabels.Add(split.First(), split.Last());
                }
            }

            commandLineArguments.Result = result;

#if DEBUG
            commandLineArguments.GitHubToken = Secrets.GitHubToken;
#endif
            return(commandLineArguments);
        }
        private async Task <object> ParseAddArguments(string[] args, Message message)
        {
            AddArguments a = new AddArguments {
                Inputs = new List <AddArguments.Stack>(), Outputs = new List <AddArguments.Stack>()
            };
            FluentCommandLineParser parser = new FluentCommandLineParser();
            bool helpCalled = false;
            Action <string, string> transformer = (type, s) =>
            {
                // Checking if input is formatted correctly
                if (!s.Contains(":"))
                {
                    throw new FormatException($"{type} not formatted correctly <itemName|itemId>:<quantity>");
                }

                // Parsing input
                string[] t    = s.Split(':');
                int      qty  = int.Parse(t[1]);
                string   item = t[0].ToSentenceCase();

                if (qty < 1)
                {
                    throw new Exception("Quantity must be greater than 0.");
                }

                a.Inputs.Add(new AddArguments.Stack
                {
                    Id       = item,
                    Quantity = qty
                });
            };

            parser.Setup <string>('n', "name")
            .Required()
            .Callback(n =>
            {
                // Validating
                if (n.Trim().Length < 4)
                {
                    throw new Exception("Name must have at more than 4 characters");
                }

                a.Name = n;
            });

            parser.Setup <double>('e', "exp")
            .Required()
            .Callback(e =>
            {
                // Validating
                if (e < 0)
                {
                    throw new Exception("Exp must be greater than 0.");
                }

                a.Exp = e;
            });

            parser.Setup <double>('u', "units")
            .Required()
            .Callback(u =>
            {
                // Validating
                if (u < 0)
                {
                    throw new Exception("Units must be greater than 0.");
                }

                a.Units = u;
            });

            parser.Setup <int>('l', "level")
            .Required()
            .Callback(l =>
            {
                // Validating
                if (l < 1 || l > 120)
                {
                    throw new Exception("Level must be between 1 and 120.");
                }

                a.Level = (sbyte)l;
            });

            parser.Setup <AddArguments.Skill>('s', "skill")
            .Required()
            .Callback(s => a.Type = s);

            parser.Setup <int>('E', "extra")
            .SetDefault(0)
            .Callback(c =>
            {
                // Validating
                if (c < 0)
                {
                    throw new Exception("Extra must be greater than -1.");
                }

                a.Extra = c;
            });

            parser.Setup <List <string> >('i', "input")
            .Callback(i => i.ForEach(input => transformer("Input", input)));

            parser.Setup <List <string> >('o', "output")
            .Callback(i => i.ForEach(output => transformer("Output", output)));

            parser.Setup <bool>('?', "help")
            .Callback(h => helpCalled = h);

            var r = parser.Parse(args);

            if (r.HasErrors || helpCalled)
            {
                if (!helpCalled)
                {
                    await message.Channel.SendMessage($"**Error:**```{r.ErrorText}```");

                    return(false);
                }

                var m = "";

                m += "**Usage:**" +
                     "```!recipe add <-n|name> <-e|exp> <-l|level> <-u|units> <-s|skill> [-E|extra] [-i|input] [-o|output]```\n" +
                     "**Options:**\n" +
                     "`-n`, `--name` **REQUIRED** [string]\n" +
                     "The name of the training method. Usually named after the output.\n" +
                     "\n" +
                     "`-e`, `--exp` **REQUIRED** [double (> 0)]" +
                     "The amount of exp this recipe produces.\n" +
                     "\n" +
                     "`-l`, `--level` **REQUIRED** [int (1-120)]\n" +
                     "The level required to do this recipe.\n" +
                     "\n" +
                     "`-u`, `--units` **REQUIRED** [double (> 0)]\n" +
                     "The about of times this recipe can be done in one hour. Fractions are allowed.\n" +
                     "\n" +
                     "`-s`, `--skill` **REQUIRED** [enum]\n" +
                     "The skill the recipe is for.\n" +
                     "\n" +
                     "`-E`, `--extra` *OPTIONAL* [int (> -1)]\n" +
                     "Any extra cost the recipe might have from items that are not tradable but buyable from a shop\n" +
                     "like cleansing crystals.\n" +
                     "\n" +
                     "`-i`, `--input` *OPTIONAL* [custom]\n" +
                     "A list of inputs for the recipe if there are any. Format: <itemName|itemId>:<quantity>\n" +
                     "\tEg. --input \"Oak logs:1\" \"Yew logs:2\" would make the recipe required 1 oak log and 2 yew logs.\n" +
                     "\n" +
                     "`-o`, `--output` *OPTIONAL* [custom]\n" +
                     "A list of outputs for the recipe if there are any. Format <itemName|itemId>:<quantity>\n" +
                     "\tEg. --output \"Oak logs:1\" \"Yew logs:2\" would make the recipe output 1 oak log and 2 yew logs.\n";

                await message.Channel.SendMessage(m);

                return(false);
            }

            return(a);
        }
Exemple #38
0
        static int Main(string[] args)
        {
            var logger = new Logger();

            try
            {
                var parser = new FluentCommandLineParser();

                var deployDbConnection = string.Empty;
                parser.Setup <string>('d', "deployDbConnection").Callback(s => deployDbConnection = s).Required();

                var environment = string.Empty;
                parser.Setup <string>('e', "environment").Callback(s => environment = s).Required();

                var scriptFolder = string.Empty;
                parser.Setup <string>('f', "folder").Callback(s => scriptFolder = s).Required();

                var filenameMask = string.Empty;
                parser.Setup <string>('m', "mask").Callback(s => filenameMask = s).Required();

                var sqlConn = string.Empty;
                parser.Setup <string>('s', "sqlConn").Callback(s => sqlConn = s).SetDefault(String.Empty);

                var psFile = string.Empty;
                parser.Setup <string>('p', "psFile").Callback(s => psFile = s).SetDefault(String.Empty);

                var cmdFile = string.Empty;
                parser.Setup <string>('c', "cmdFile").Callback(s => cmdFile = s).SetDefault(String.Empty);

                var result = parser.Parse(args);
                if (result.HasErrors)
                {
                    logger.LogError(result.ErrorText);
                    return(2);
                }

                IExecutor executor;
                if (string.IsNullOrWhiteSpace(sqlConn) == false)
                {
                    executor = new SqlExecutor(sqlConn, logger);
                }
                else if (string.IsNullOrWhiteSpace(psFile) == false)
                {
                    executor = new PowerShellExecutor();
                }
                else if (string.IsNullOrWhiteSpace(cmdFile) == false)
                {
                    executor = new CommandExecutor();
                }
                else
                {
                    throw new ApplicationException("Script type cannot be determined.  Specify sqlConn, psFile or cmdFile");
                }

                var locator   = new FileLocator();
                var history   = new ScriptHistory(deployDbConnection);
                var processor = new ScriptProcessor(environment, scriptFolder, filenameMask, locator, history, executor);

                processor.ProcessScripts();
            }
            catch (Exception ex)
            {
                logger.LogError(ex.ToString());
                return(1);
            }

            return(0);
        }
        private static void Main(string[] args)
        {
            Console.WriteLine("Welcome to MetaMorpheus");
            Console.WriteLine(GlobalVariables.MetaMorpheusVersion);

            var p = new FluentCommandLineParser <ApplicationArguments>();

            p.Setup(arg => arg.Tasks)
            .As('t', "tasks")
            .SetDefault(new List <string>())
            .WithDescription("Single-task TOMLs.");

            p.Setup(arg => arg.OutputFolder)
            .As('o', "outputFolder")
            .SetDefault(null)
            .WithDescription("Folder into which to place results.");

            p.Setup(arg => arg.MetaTasks)
            .As('m', "meta-task")
            .SetDefault(new List <string>())
            .WithDescription("Multi-task TOMLs.");

            p.Setup(arg => arg.Spectra)
            .As('s', "spectra")
            .Required()
            .WithDescription("Spectra to analyze.");

            p.Setup(arg => arg.Databases)
            .As('d', "databases")
            .Required()
            .WithDescription("Protein sequence databases (FASTA, XML).");

            p.SetupHelp("h", "help")
            .Callback(text => Console.WriteLine(text));

            var result = p.Parse(args);

            if (p.Object.MetaTasks != null && (p.Object.MetaTasks.Count != 0 || p.Object.Tasks.Count != 0))
            {
                if (!result.HasErrors)
                {
                    MetaMorpheusEngine.WarnHandler                 += WarnHandler;
                    MetaMorpheusEngine.OutProgressHandler          += MyEngine_outProgressHandler;
                    MetaMorpheusEngine.StartingSingleEngineHander  += MyEngine_startingSingleEngineHander;
                    MetaMorpheusEngine.FinishedSingleEngineHandler += MyEngine_finishedSingleEngineHandler;

                    MetaMorpheusTask.WarnHandler += WarnHandler;
                    MetaMorpheusTask.LogHandler  += LogHandler;
                    MetaMorpheusTask.StartingSingleTaskHander   += MyTaskEngine_startingSingleTaskHander;
                    MetaMorpheusTask.FinishedSingleTaskHandler  += MyTaskEngine_finishedSingleTaskHandler;
                    MetaMorpheusTask.FinishedWritingFileHandler += MyTaskEngine_finishedWritingFileHandler;

                    bool containsRawFiles = p.Object.Spectra.Select(v => Path.GetExtension(v).ToLowerInvariant()).Any(v => v == ".raw");
                    if (containsRawFiles && !GlobalVariables.GlobalSettings.UserHasAgreedToThermoRawFileReaderLicence)
                    {
                        // write the Thermo RawFileReader licence agreement
                        Console.WriteLine(ThermoRawFileReader.ThermoRawFileReaderLicence.ThermoLicenceText);
                        Console.WriteLine("\nIn order to search Thermo .raw files, you must agree to the above terms. Do you agree to the above terms? y/n\n");
                        string res = Console.ReadLine().ToLowerInvariant();
                        if (res == "y")
                        {
                            var newGlobalSettings = new GlobalSettings
                            {
                                UserHasAgreedToThermoRawFileReaderLicence = true,
                                WriteExcelCompatibleTSVs = GlobalVariables.GlobalSettings.WriteExcelCompatibleTSVs
                            };

                            Toml.WriteFile <GlobalSettings>(newGlobalSettings, Path.Combine(GlobalVariables.DataDir, @"settings.toml"));
                            GlobalVariables.GlobalSettings = newGlobalSettings;
                        }
                        else
                        {
                            Console.WriteLine("Thermo licence has been declined. Exiting MetaMorpheus. You can still search .mzML and .mgf files without agreeing to the Thermo licence.");
                            return;
                        }
                    }

                    foreach (var db in p.Object.Databases)
                    {
                        if (!Path.GetExtension(db).Equals(".fasta"))
                        {
                            GlobalVariables.AddMods(UsefulProteomicsDatabases.ProteinDbLoader.GetPtmListFromProteinXml(db).OfType <Modification>(), true);

                            // print any error messages reading the mods to the console
                            foreach (var error in GlobalVariables.ErrorsReadingMods)
                            {
                                Console.WriteLine(error);
                            }
                            GlobalVariables.ErrorsReadingMods.Clear();
                        }
                    }

                    List <(string, MetaMorpheusTask)> taskList = new List <(string, MetaMorpheusTask)>();

                    for (int i = 0; i < p.Object.Tasks.Count; i++)
                    {
                        var filePath = p.Object.Tasks[i];

                        var uhum = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);

                        switch (uhum.Get <string>("TaskType"))
                        {
                        case "Search":
                            var ye1 = Toml.ReadFile <SearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "SearchTask", ye1));
                            break;

                        case "Calibrate":
                            var ye2 = Toml.ReadFile <CalibrationTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "CalibrationTask", ye2));
                            break;

                        case "Gptmd":
                            var ye3 = Toml.ReadFile <GptmdTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "GptmdTask", ye3));
                            break;

                        case "XLSearch":
                            var ye4 = Toml.ReadFile <XLSearchTask>(filePath, MetaMorpheusTask.tomlConfig);
                            taskList.Add(("Task" + (i + 1) + "XLSearchTask", ye4));
                            break;

                        default:
                            Console.WriteLine(uhum.Get <string>("TaskType") + " is not a known task type! Skipping.");
                            break;
                        }
                    }

                    for (int i = 0; i < p.Object.MetaTasks.Count; i++)
                    {
                        var filePath = p.Object.MetaTasks[i];
                        var uhum     = Toml.ReadFile(filePath, MetaMorpheusTask.tomlConfig);
                        switch (uhum.Get <string>("TaskType"))
                        {
                        case "Search":
                            Console.WriteLine("Search tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "Calibrate":
                            Console.WriteLine("Calibrate tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "Gptmd":
                            Console.WriteLine("Gptmd tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        case "XLSearch":
                            Console.WriteLine("XLSearch tasks are individual tasks. Please use -t for task instead of -m. Skipping.");
                            break;

                        default:
                            Console.WriteLine(uhum.Get <string>("TaskType") + " is not a known task type! Skipping.");
                            break;
                        }
                    }

                    List <string>    startingRawFilenameList   = p.Object.Spectra.Select(b => Path.GetFullPath(b)).ToList();
                    List <DbForTask> startingXmlDbFilenameList = p.Object.Databases.Select(b => new DbForTask(Path.GetFullPath(b), IsContaminant(b))).ToList();

                    string outputFolder = p.Object.OutputFolder;
                    if (outputFolder == null)
                    {
                        var pathOfFirstSpectraFile = Path.GetDirectoryName(startingRawFilenameList.First());
                        outputFolder = Path.Combine(pathOfFirstSpectraFile, @"$DATETIME");
                    }

                    EverythingRunnerEngine a = new EverythingRunnerEngine(taskList, startingRawFilenameList, startingXmlDbFilenameList, outputFolder);

                    try
                    {
                        a.Run();
                    }
                    catch (Exception e)
                    {
                        while (e.InnerException != null)
                        {
                            e = e.InnerException;
                        }

                        var message = "Run failed, Exception: " + e.Message;
                        Console.WriteLine(message);
                    }
                }
                else
                {
                    Console.WriteLine("Error Text:" + result.ErrorText);
                }
            }
            else
            {
                Console.WriteLine("Error Text: No toml file was specified. Use -t for tasks or -m for meta-tasks.");
            }
        }
Exemple #40
0
        private static void Main(string[] args)
        {
            ExceptionlessClient.Default.Startup("x3MPpeQSBUUsXl3DjekRQ9kYjyN3cr5JuwdoOBpZ");

            SetupNLog();

            _keywords = new HashSet <string> {
                "temp", "tmp"
            };

            _logger = LogManager.GetCurrentClassLogger();



            _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.File)
            .As('f')
            .WithDescription("File to process. Either this or -d is required");

            _fluentCommandLineParser.Setup(arg => arg.Directory)
            .As('d')
            .WithDescription("Directory to recursively process. Either this or -f is required");

            _fluentCommandLineParser.Setup(arg => arg.Keywords)
            .As('k')
            .WithDescription(
                "Comma separated list of keywords to highlight in output. By default, 'temp' and 'tmp' are highlighted. Any additional keywords will be added to these.");

            _fluentCommandLineParser.Setup(arg => arg.OutFile)
            .As('o')
            .WithDescription(
                "When specified, save prefetch file bytes to the given path. Useful to look at decompressed Win10 files");

            _fluentCommandLineParser.Setup(arg => arg.Quiet)
            .As('q')
            .WithDescription(
                "Do not dump full details about each file processed. Speeds up processing when using --json or --csv. Default is FALSE\r\n")
            .SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.JsonDirectory)
            .As("json")
            .WithDescription(
                "Directory to save json representation to. Use --pretty for a more human readable layout");

            _fluentCommandLineParser.Setup(arg => arg.CsvDirectory)
            .As("csv")
            .WithDescription(
                "Directory to save CSV results to. Be sure to include the full path in double quotes");
            _fluentCommandLineParser.Setup(arg => arg.CsvName)
            .As("csvf")
            .WithDescription("File name to save CSV formatted results to. When present, overrides default name");


            _fluentCommandLineParser.Setup(arg => arg.xHtmlDirectory)
            .As("html")
            .WithDescription(
                "Directory to save xhtml formatted results to. Be sure to include the full path in double quotes");

            _fluentCommandLineParser.Setup(arg => arg.JsonPretty)
            .As("pretty")
            .WithDescription(
                "When exporting to json, use a more human readable layout. Default is FALSE\r\n").SetDefault(false);



            _fluentCommandLineParser.Setup(arg => arg.DateTimeFormat)
            .As("dt")
            .WithDescription(
                "The custom date/time format to use when displaying timestamps. See https://goo.gl/CNVq0k for options. Default is: yyyy-MM-dd HH:mm:ss")
            .SetDefault("yyyy-MM-dd HH:mm:ss");

            _fluentCommandLineParser.Setup(arg => arg.PreciseTimestamps)
            .As("mp")
            .WithDescription(
                "When true, display higher precision for timestamps. Default is FALSE").SetDefault(false);


            var header =
                $"PECmd version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nhttps://github.com/EricZimmerman/PECmd";

            var footer = @"Examples: PECmd.exe -f ""C:\Temp\CALC.EXE-3FBEF7FD.pf""" + "\r\n\t " +
                         @" PECmd.exe -f ""C:\Temp\CALC.EXE-3FBEF7FD.pf"" --json ""D:\jsonOutput"" --jsonpretty" +
                         "\r\n\t " +
                         @" PECmd.exe -d ""C:\Temp"" -k ""system32, fonts""" + "\r\n\t " +
                         @" PECmd.exe -d ""C:\Temp"" --csv ""c:\temp"" --csvf foo.csv --json c:\temp\json" +
                         "\r\n\t " +
                         @" PECmd.exe -d ""C:\Windows\Prefetch""" + "\r\n\t " +
                         "\r\n\t" +
                         "  Short options (single letter) are prefixed with a single dash. Long commands are prefixed with two dashes\r\n";

            _fluentCommandLineParser.SetupHelp("?", "help")
            .WithHeader(header)
            .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (result.HelpCalled)
            {
                return;
            }

            if (result.HasErrors)
            {
                _logger.Error("");
                _logger.Error(result.ErrorText);

                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                return;
            }

            if (UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.File) &&
                UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.Directory))
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("Either -f or -d is required. Exiting");
                return;
            }

            if (UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.File) == false &&
                !File.Exists(_fluentCommandLineParser.Object.File))
            {
                _logger.Warn($"File '{_fluentCommandLineParser.Object.File}' not found. Exiting");
                return;
            }

            if (UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.Directory) == false &&
                !Directory.Exists(_fluentCommandLineParser.Object.Directory))
            {
                _logger.Warn($"Directory '{_fluentCommandLineParser.Object.Directory}' not found. Exiting");
                return;
            }

            if (_fluentCommandLineParser.Object.Keywords?.Length > 0)
            {
                var kws = _fluentCommandLineParser.Object.Keywords.ToLowerInvariant().Split(new[] { ',' },
                                                                                            StringSplitOptions.RemoveEmptyEntries);

                foreach (var kw in kws)
                {
                    _keywords.Add(kw.Trim());
                }
            }


            _logger.Info(header);
            _logger.Info("");
            _logger.Info($"Command line: {string.Join(" ", Environment.GetCommandLineArgs().Skip(1))}");

            if (IsAdministrator() == false)
            {
                _logger.Fatal("\r\nWarning: Administrator privileges not found!");
            }

            _logger.Info("");
            _logger.Info($"Keywords: {string.Join(", ", _keywords)}");
            _logger.Info("");

            if (_fluentCommandLineParser.Object.PreciseTimestamps)
            {
                _fluentCommandLineParser.Object.DateTimeFormat = _preciseTimeFormat;
            }

            _processedFiles = new List <IPrefetch>();

            _failedFiles = new List <string>();

            if (_fluentCommandLineParser.Object.File?.Length > 0)
            {
                IPrefetch pf = null;

                try
                {
                    pf = LoadFile(_fluentCommandLineParser.Object.File);

                    if (pf != null)
                    {
                        if (_fluentCommandLineParser.Object.OutFile.IsNullOrEmpty() == false)
                        {
                            try
                            {
                                if (Directory.Exists(Path.GetDirectoryName(_fluentCommandLineParser.Object.OutFile)) ==
                                    false)
                                {
                                    Directory.CreateDirectory(
                                        Path.GetDirectoryName(_fluentCommandLineParser.Object.OutFile));
                                }

                                PrefetchFile.SavePrefetch(_fluentCommandLineParser.Object.OutFile, pf);
                                _logger.Info($"Saved prefetch bytes to '{_fluentCommandLineParser.Object.OutFile}'");
                            }
                            catch (Exception e)
                            {
                                _logger.Error($"Unable to save prefetch file. Error: {e.Message}");
                            }
                        }


                        _processedFiles.Add(pf);
                    }
                }
                catch (UnauthorizedAccessException ex)
                {
                    _logger.Error(
                        $"Unable to access '{_fluentCommandLineParser.Object.File}'. Are you running as an administrator? Error: {ex.Message}");
                }
                catch (Exception ex)
                {
                    _logger.Error(
                        $"Error getting prefetch files in '{_fluentCommandLineParser.Object.Directory}'. Error: {ex.Message}");
                }
            }
            else
            {
                _logger.Info($"Looking for prefetch files in '{_fluentCommandLineParser.Object.Directory}'");
                _logger.Info("");

                string[] pfFiles = null;


                var f = new DirectoryEnumerationFilters();
                f.InclusionFilter = fsei =>
                {
                    if (fsei.Extension.ToUpperInvariant() == ".PF")
                    {
                        return(true);
                    }

                    return(false);
                };

                f.RecursionFilter = entryInfo => !entryInfo.IsMountPoint && !entryInfo.IsSymbolicLink;

                f.ErrorFilter = (errorCode, errorMessage, pathProcessed) => true;

                var dirEnumOptions =
                    DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive |
                    DirectoryEnumerationOptions.SkipReparsePoints | DirectoryEnumerationOptions.ContinueOnException |
                    DirectoryEnumerationOptions.BasicSearch;

                var files2 =
                    Alphaleonis.Win32.Filesystem.Directory.EnumerateFileSystemEntries(_fluentCommandLineParser.Object.Directory, dirEnumOptions, f);



                try
                {
                    pfFiles = files2.ToArray(); //Directory.GetFiles(_fluentCommandLineParser.Object.Directory, "*.pf",                        SearchOption.AllDirectories);
                }
                catch (UnauthorizedAccessException ua)
                {
                    _logger.Error(
                        $"Unable to access '{_fluentCommandLineParser.Object.Directory}'. Are you running as an administrator? Error: {ua.Message}");
                    return;
                }
                catch (Exception ex)
                {
                    _logger.Error(
                        $"Error getting prefetch files in '{_fluentCommandLineParser.Object.Directory}'. Error: {ex.Message}");
                    return;
                }

                _logger.Info($"Found {pfFiles.Length:N0} Prefetch files");
                _logger.Info("");

                var sw = new Stopwatch();
                sw.Start();

                foreach (var file in pfFiles)
                {
                    var pf = LoadFile(file);

                    if (pf != null)
                    {
                        _processedFiles.Add(pf);
                    }
                }

                sw.Stop();

                if (_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info("");
                }

                _logger.Info(
                    $"Processed {pfFiles.Length - _failedFiles.Count:N0} out of {pfFiles.Length:N0} files in {sw.Elapsed.TotalSeconds:N4} seconds");

                if (_failedFiles.Count > 0)
                {
                    _logger.Info("");
                    _logger.Warn("Failed files");
                    foreach (var failedFile in _failedFiles)
                    {
                        _logger.Info($"  {failedFile}");
                    }
                }
            }

            if (_processedFiles.Count > 0)
            {
                _logger.Info("");

                try
                {
                    CsvWriter    csv          = null;
                    StreamWriter streamWriter = null;

                    CsvWriter    csvTl          = null;
                    StreamWriter streamWriterTl = null;


                    if (_fluentCommandLineParser.Object.CsvDirectory?.Length > 0)
                    {
                        var outName = $"{DateTimeOffset.Now:yyyyMMddHHmmss}_PECmd_Output.csv";

                        if (_fluentCommandLineParser.Object.CsvName.IsNullOrEmpty() == false)
                        {
                            outName = Path.GetFileName(_fluentCommandLineParser.Object.CsvName);
                        }

                        var outNameTl = $"{DateTimeOffset.Now:yyyyMMddHHmmss}_PECmd_Output_Timeline.csv";
                        if (_fluentCommandLineParser.Object.CsvName.IsNullOrEmpty() == false)
                        {
                            outNameTl =
                                $"{Path.GetFileNameWithoutExtension(_fluentCommandLineParser.Object.CsvName)}_Timeline{Path.GetExtension(_fluentCommandLineParser.Object.CsvName)}";
                        }


                        var outFile   = Path.Combine(_fluentCommandLineParser.Object.CsvDirectory, outName);
                        var outFileTl = Path.Combine(_fluentCommandLineParser.Object.CsvDirectory, outNameTl);


                        if (Directory.Exists(_fluentCommandLineParser.Object.CsvDirectory) == false)
                        {
                            _logger.Warn(
                                $"Path to '{_fluentCommandLineParser.Object.CsvDirectory}' does not exist. Creating...");
                            Directory.CreateDirectory(_fluentCommandLineParser.Object.CsvDirectory);
                        }

                        _logger.Warn($"CSV output will be saved to '{outFile}'");
                        _logger.Warn($"CSV time line output will be saved to '{outFileTl}'");

                        try
                        {
                            streamWriter = new StreamWriter(outFile);
                            csv          = new CsvWriter(streamWriter);


                            csv.WriteHeader(typeof(CsvOut));
                            csv.NextRecord();

                            streamWriterTl = new StreamWriter(outFileTl);
                            csvTl          = new CsvWriter(streamWriterTl);

                            csvTl.WriteHeader(typeof(CsvOutTl));
                            csvTl.NextRecord();
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(
                                $"Unable to open '{outFile}' for writing. CSV export canceled. Error: {ex.Message}");
                        }
                    }

                    if (_fluentCommandLineParser.Object.JsonDirectory?.Length > 0)
                    {
                        if (Directory.Exists(_fluentCommandLineParser.Object.JsonDirectory) == false)
                        {
                            _logger.Warn(
                                $"'{_fluentCommandLineParser.Object.JsonDirectory} does not exist. Creating...'");
                            Directory.CreateDirectory(_fluentCommandLineParser.Object.JsonDirectory);
                        }

                        _logger.Warn($"Saving json output to '{_fluentCommandLineParser.Object.JsonDirectory}'");
                    }

                    XmlTextWriter xml = null;

                    if (_fluentCommandLineParser.Object.xHtmlDirectory?.Length > 0)
                    {
                        if (Directory.Exists(_fluentCommandLineParser.Object.xHtmlDirectory) == false)
                        {
                            _logger.Warn(
                                $"'{_fluentCommandLineParser.Object.xHtmlDirectory} does not exist. Creating...'");
                            Directory.CreateDirectory(_fluentCommandLineParser.Object.xHtmlDirectory);
                        }

                        var outDir = Path.Combine(_fluentCommandLineParser.Object.xHtmlDirectory,
                                                  $"{DateTimeOffset.UtcNow:yyyyMMddHHmmss}_PECmd_Output_for_{_fluentCommandLineParser.Object.xHtmlDirectory.Replace(@":\", "_").Replace(@"\", "_")}");

                        if (Directory.Exists(outDir) == false)
                        {
                            Directory.CreateDirectory(outDir);
                        }

                        var styleDir = Path.Combine(outDir, "styles");
                        if (Directory.Exists(styleDir) == false)
                        {
                            Directory.CreateDirectory(styleDir);
                        }

                        File.WriteAllText(Path.Combine(styleDir, "normalize.css"), Resources.normalize);
                        File.WriteAllText(Path.Combine(styleDir, "style.css"), Resources.style);

                        Resources.directories.Save(Path.Combine(styleDir, "directories.png"));
                        Resources.filesloaded.Save(Path.Combine(styleDir, "filesloaded.png"));

                        var outFile = Path.Combine(_fluentCommandLineParser.Object.xHtmlDirectory, outDir,
                                                   "index.xhtml");

                        _logger.Warn($"Saving HTML output to '{outFile}'");

                        xml = new XmlTextWriter(outFile, Encoding.UTF8)
                        {
                            Formatting  = Formatting.Indented,
                            Indentation = 4
                        };

                        xml.WriteStartDocument();

                        xml.WriteProcessingInstruction("xml-stylesheet", "href=\"styles/normalize.css\"");
                        xml.WriteProcessingInstruction("xml-stylesheet", "href=\"styles/style.css\"");

                        xml.WriteStartElement("document");
                    }

                    if (_fluentCommandLineParser.Object.CsvDirectory.IsNullOrEmpty() == false ||
                        _fluentCommandLineParser.Object.JsonDirectory.IsNullOrEmpty() == false ||
                        _fluentCommandLineParser.Object.xHtmlDirectory.IsNullOrEmpty() == false)
                    {
                        foreach (var processedFile in _processedFiles)
                        {
                            var o = GetCsvFormat(processedFile);

                            try
                            {
                                foreach (var dateTimeOffset in processedFile.LastRunTimes)
                                {
                                    var t = new CsvOutTl();

                                    var exePath =
                                        processedFile.Filenames.FirstOrDefault(
                                            y => y.EndsWith(processedFile.Header.ExecutableFilename));

                                    if (exePath == null)
                                    {
                                        exePath = processedFile.Header.ExecutableFilename;
                                    }

                                    t.ExecutableName = exePath;
                                    t.RunTime        = dateTimeOffset.ToString(_fluentCommandLineParser.Object.DateTimeFormat);

                                    csvTl?.WriteRecord(t);
                                    csvTl?.NextRecord();
                                }
                            }
                            catch (Exception ex)
                            {
                                _logger.Error(
                                    $"Error getting time line record for '{processedFile.SourceFilename}' to '{_fluentCommandLineParser.Object.CsvDirectory}'. Error: {ex.Message}");
                            }

                            try
                            {
                                csv?.WriteRecord(o);
                                csv?.NextRecord();
                            }
                            catch (Exception ex)
                            {
                                _logger.Error(
                                    $"Error writing CSV record for '{processedFile.SourceFilename}' to '{_fluentCommandLineParser.Object.CsvDirectory}'. Error: {ex.Message}");
                            }

                            if (_fluentCommandLineParser.Object.JsonDirectory?.Length > 0)
                            {
                                SaveJson(processedFile, _fluentCommandLineParser.Object.JsonPretty,
                                         _fluentCommandLineParser.Object.JsonDirectory);
                            }

                            //XHTML
                            xml?.WriteStartElement("Container");
                            xml?.WriteElementString("SourceFile", o.SourceFilename);
                            xml?.WriteElementString("SourceCreated", o.SourceCreated);
                            xml?.WriteElementString("SourceModified", o.SourceModified);
                            xml?.WriteElementString("SourceAccessed", o.SourceAccessed);

                            xml?.WriteElementString("LastRun", o.LastRun);

                            xml?.WriteElementString("PreviousRun0", $"{o.PreviousRun0}");
                            xml?.WriteElementString("PreviousRun1", $"{o.PreviousRun1}");
                            xml?.WriteElementString("PreviousRun2", $"{o.PreviousRun2}");
                            xml?.WriteElementString("PreviousRun3", $"{o.PreviousRun3}");
                            xml?.WriteElementString("PreviousRun4", $"{o.PreviousRun4}");
                            xml?.WriteElementString("PreviousRun5", $"{o.PreviousRun5}");
                            xml?.WriteElementString("PreviousRun6", $"{o.PreviousRun6}");

                            xml?.WriteStartElement("ExecutableName");
                            xml?.WriteAttributeString("title",
                                                      "Note: The name of the executable tracked by the pf file");
                            xml?.WriteString(o.ExecutableName);
                            xml?.WriteEndElement();

                            xml?.WriteElementString("RunCount", $"{o.RunCount}");

                            xml?.WriteStartElement("Size");
                            xml?.WriteAttributeString("title", "Note: The size of the executable in bytes");
                            xml?.WriteString(o.Size);
                            xml?.WriteEndElement();

                            xml?.WriteStartElement("Hash");
                            xml?.WriteAttributeString("title",
                                                      "Note: The calculated hash for the pf file that should match the hash in the source file name");
                            xml?.WriteString(o.Hash);
                            xml?.WriteEndElement();

                            xml?.WriteStartElement("Version");
                            xml?.WriteAttributeString("title",
                                                      "Note: The operating system that generated the prefetch file");
                            xml?.WriteString(o.Version);
                            xml?.WriteEndElement();

                            xml?.WriteElementString("Note", o.Note);

                            xml?.WriteElementString("Volume0Name", o.Volume0Name);
                            xml?.WriteElementString("Volume0Serial", o.Volume0Serial);
                            xml?.WriteElementString("Volume0Created", o.Volume0Created);

                            xml?.WriteElementString("Volume1Name", o.Volume1Name);
                            xml?.WriteElementString("Volume1Serial", o.Volume1Serial);
                            xml?.WriteElementString("Volume1Created", o.Volume1Created);


                            xml?.WriteStartElement("Directories");
                            xml?.WriteAttributeString("title",
                                                      "A comma separated list of all directories accessed by the executable");
                            xml?.WriteString(o.Directories);
                            xml?.WriteEndElement();

                            xml?.WriteStartElement("FilesLoaded");
                            xml?.WriteAttributeString("title",
                                                      "A comma separated list of all files that were loaded by the executable");
                            xml?.WriteString(o.FilesLoaded);
                            xml?.WriteEndElement();

                            xml?.WriteEndElement();
                        }


                        //Close CSV stuff
                        streamWriter?.Flush();
                        streamWriter?.Close();

                        streamWriterTl?.Flush();
                        streamWriterTl?.Close();

                        //Close XML
                        xml?.WriteEndElement();
                        xml?.WriteEndDocument();
                        xml?.Flush();
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error($"Error exporting data! Error: {ex.Message}");
                }
            }
        }
Exemple #41
0
            public static void readFromCommandLine(string[] args)
            {
                // first pass
                var cmd_parser = new FluentCommandLineParser();

                // help
                cmd_parser.SetupHelp("h", "help").Callback(text => outputHelp());

                // config file
                cmd_parser.Setup <string>("config").Callback(value => configFilename = value).Required();

                cmd_parser.Parse(args);

                if (DLTNode.Program.noStart)
                {
                    return;
                }

                readConfigFile(configFilename);



                // second pass
                cmd_parser = new FluentCommandLineParser();

                // Block storage provider
                cmd_parser.Setup <string>("blockStorage").Callback(value => blockStorageProvider = value).Required();

                // testnet
                cmd_parser.Setup <bool>('t', "testnet").Callback(value => CoreConfig.isTestNet = true).Required();

                cmd_parser.Parse(args);

                if (CoreConfig.isTestNet)
                {
                    Config.serverPort = defaultTestnetServerPort;
                    apiPort           = testnetApiPort;
                    dataFolderPath    = "data-testnet";
                    PeerStorage.init(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "testnet-peers.dat");
                    genesisFile = "testnet-genesis.dat";
                }
                else
                {
                    Config.serverPort = defaultServerPort;
                    PeerStorage.init(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
                }



                string seedNode = "";

                // third pass
                cmd_parser = new FluentCommandLineParser();

                int start_clean = 0; // Flag to determine if node should delete cache+logs

                // version
                cmd_parser.Setup <bool>('v', "version").Callback(text => outputVersion());

                // Toggle between full history node and no history
                cmd_parser.Setup <bool>('s', "save-history").Callback(value => storeFullHistory = value).Required();

                // Toggle worker-only mode
                cmd_parser.Setup <bool>("worker").Callback(value => workerOnly = true).Required();

                // Check for password change
                cmd_parser.Setup <bool>('x', "changepass").Callback(value => changePass = value).Required();

                // Check for recovery parameter
                cmd_parser.Setup <bool>("recover").Callback(value => recoverFromFile = value).Required();

                // Check for clean parameter
                cmd_parser.Setup <bool>('c', "clean").Callback(value => start_clean = 1);
                cmd_parser.Setup <bool>('f', "force").Callback(value => { if (start_clean > 0)
                                                                          {
                                                                              start_clean += 1;
                                                                          }
                                                               });


                cmd_parser.Setup <int>('p', "port").Callback(value => Config.serverPort = value).Required();

                cmd_parser.Setup <int>('a', "apiport").Callback(value => apiPort = value).Required();

                cmd_parser.Setup <string>('i', "ip").Callback(value => externalIp = value).Required();

                cmd_parser.Setup <string>("genesis").Callback(value => genesisFunds = value).Required();

                cmd_parser.Setup <string>("genesis2").Callback(value => genesis2Address = value).Required();

                cmd_parser.Setup <int>("threads").Callback(value => miningThreads = (uint)value).Required();

                cmd_parser.Setup <string>('w', "wallet").Callback(value => walletFile = value).Required();

                cmd_parser.Setup <string>('n', "node").Callback(value => seedNode = value).Required();

                cmd_parser.Setup <int>("maxLogSize").Callback(value => maxLogSize = value).Required();

                cmd_parser.Setup <int>("maxLogCount").Callback(value => maxLogCount = value).Required();

                cmd_parser.Setup <long>("lastGoodBlock").Callback(value => lastGoodBlock = (ulong)value).Required();

                cmd_parser.Setup <bool>("disableWebStart").Callback(value => disableWebStart = true).Required();

                cmd_parser.Setup <string>("dataFolderPath").Callback(value => dataFolderPath = value).Required();

                cmd_parser.Setup <bool>("optimizeDBStorage").Callback(value => optimizeDBStorage = value).Required();

                cmd_parser.Setup <bool>("verifyStorage").Callback(value => fullStorageDataVerification = true).Required();

                cmd_parser.Setup <bool>("onlyShowAddresses").Callback(value => onlyShowAddresses = true).Required();


                // Debug
                cmd_parser.Setup <string>("netdump").Callback(value => networkDumpFile = value).SetDefault("");

                cmd_parser.Setup <int>("benchmarkKeys").Callback(value => benchmarkKeys = value).SetDefault(0);

                cmd_parser.Setup <bool>("generateWallet").Callback(value => generateWalletOnly = value).SetDefault(false);

                cmd_parser.Setup <string>("walletPassword").Callback(value => dangerCommandlinePasswordCleartextUnsafe = value).SetDefault("");

                cmd_parser.Setup <bool>("noNetworkSync").Callback(value => noNetworkSync = true).Required();

                cmd_parser.Setup <bool>("devInsertFromJson").Callback(value => devInsertFromJson = true).Required();

                cmd_parser.Setup <bool>("offline").Callback(value => CoreConfig.preventNetworkOperations = true).Required();

                cmd_parser.Parse(args);

                WalletStateStorage.path = dataFolderPath + Path.DirectorySeparatorChar + "ws";

                if (start_clean > 0)
                {
                    bool do_clean = false;
                    if (start_clean > 1)
                    {
                        Logging.warn("Ixian DLT node started with the forced clean parameter (-c -c).");
                        do_clean = true;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine("You have started the Ixian DLT node with the '-c' parameter, indicating that you wish to clear all cache and log files.");
                        Console.Write("This will cause the node to re-download any neccessary data, which may take some time. Are you sure? (Y/N)");
                        Console.ResetColor();
                        var k = Console.ReadKey();
                        if (k.Key == ConsoleKey.Y)
                        {
                            do_clean = true;
                        }
                    }
                    if (do_clean)
                    {
                        Node.checkDataFolder();
                        Node.cleanCacheAndLogs();
                    }
                    else
                    {
                        DLTNode.Program.noStart = true;
                        return;
                    }
                }

                if (miningThreads < 1)
                {
                    miningThreads = 1;
                }

                if (seedNode != "")
                {
                    if (CoreConfig.isTestNet)
                    {
                        CoreNetworkUtils.seedTestNetNodes = new List <string[]>
                        {
                            new string[2] {
                                seedNode, null
                            }
                        };
                    }
                    else
                    {
                        CoreNetworkUtils.seedNodes = new List <string[]>
                        {
                            new string[2] {
                                seedNode, null
                            }
                        };
                    }
                }
            }
Exemple #42
0
        private static void Main(string[] args)
        {
            ExceptionlessClient.Default.Startup("IudF6lFjzvdMldPtlYyPmSMHnSEL89n2WmYbCHoy");

            SetupNLog();

            _logger = LogManager.GetCurrentClassLogger();

            _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.File)
            .As('f')
            .WithDescription("File to process. Either this or -d is required");

            _fluentCommandLineParser.Setup(arg => arg.Directory)
            .As('d')
            .WithDescription("Directory to recursively process. Either this or -f is required");

            _fluentCommandLineParser.Setup(arg => arg.CsvDirectory)
            .As("csv")
            .WithDescription(
                "Directory to save CSV formatted results to. Be sure to include the full path in double quotes");

            _fluentCommandLineParser.Setup(arg => arg.CsvName)
            .As("csvf")
            .WithDescription("File name to save CSV formatted results to. When present, overrides default name\r\n");


            _fluentCommandLineParser.Setup(arg => arg.Quiet)
            .As('q')
            .WithDescription(
                "Only show the filename being processed vs all output. Useful to speed up exporting to json and/or csv\r\n")
            .SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.DateTimeFormat)
            .As("dt")
            .WithDescription(
                "The custom date/time format to use when displaying time stamps. See https://goo.gl/CNVq0k for options. Default is: yyyy-MM-dd HH:mm:ss\r\n")
            .SetDefault(_preciseTimeFormat);

            _fluentCommandLineParser.Setup(arg => arg.Debug)
            .As("debug")
            .WithDescription("Show debug information during processing").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.Trace)
            .As("trace")
            .WithDescription("Show trace information during processing\r\n").SetDefault(false);

            var header =
                $"RBCmd version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nhttps://github.com/EricZimmerman/RBCmd";


            var footer = @"Examples: RBCmd.exe -f ""C:\Temp\INFO2""" + "\r\n\t " +
                         @" RBCmd.exe -f ""C:\Temp\$I3VPA17"" --csv ""D:\csvOutput"" " + "\r\n\t " +
                         @" RBCmd.exe -d ""C:\Temp"" --csv ""c:\temp"" " + "\r\n\t " +
                         "\r\n\t" +
                         "  Short options (single letter) are prefixed with a single dash. Long commands are prefixed with two dashes\r\n";

            _fluentCommandLineParser.SetupHelp("?", "help")
            .WithHeader(header)
            .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (result.HelpCalled)
            {
                return;
            }

            if (result.HasErrors)
            {
                _logger.Error("");
                _logger.Error(result.ErrorText);

                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                return;
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() &&
                _fluentCommandLineParser.Object.Directory.IsNullOrEmpty())
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("Either -f or -d is required. Exiting");
                return;
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() == false &&
                !File.Exists(_fluentCommandLineParser.Object.File))
            {
                _logger.Warn($"File '{_fluentCommandLineParser.Object.File}' not found. Exiting");
                return;
            }

            if (_fluentCommandLineParser.Object.Directory.IsNullOrEmpty() == false &&
                !Directory.Exists(_fluentCommandLineParser.Object.Directory))
            {
                _logger.Warn($"Directory '{_fluentCommandLineParser.Object.Directory}' not found. Exiting");
                return;
            }

            _logger.Info(header);
            _logger.Info("");
            _logger.Info($"Command line: {string.Join(" ", Environment.GetCommandLineArgs().Skip(1))}\r\n");

            if (IsAdministrator() == false)
            {
                _logger.Fatal("Warning: Administrator privileges not found!\r\n");
            }

            if (_fluentCommandLineParser.Object.Debug)
            {
                foreach (var r in LogManager.Configuration.LoggingRules)
                {
                    r.EnableLoggingForLevel(LogLevel.Debug);
                }

                LogManager.ReconfigExistingLoggers();
                _logger.Debug("Enabled debug messages...");
            }

            if (_fluentCommandLineParser.Object.Trace)
            {
                foreach (var r in LogManager.Configuration.LoggingRules)
                {
                    r.EnableLoggingForLevel(LogLevel.Trace);
                }

                LogManager.ReconfigExistingLoggers();
                _logger.Trace("Enabled trace messages...");
            }

            _csvOuts     = new List <CsvOut>();
            _failedFiles = new List <string>();

            var files = new List <string>();

            var sw = new Stopwatch();

            sw.Start();

            if (_fluentCommandLineParser.Object.File?.Length > 0)
            {
                files.Add(_fluentCommandLineParser.Object.File);
            }
            else
            {
                if (_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info("");
                }

                files = GetRecycleBinFiles(_fluentCommandLineParser.Object.Directory);
            }

            foreach (var file in files)
            {
                ProcessFile(file);
            }

            sw.Stop();

            _logger.Info(
                $"Processed {files.Count - _failedFiles.Count:N0} out of {files.Count:N0} files in {sw.Elapsed.TotalSeconds:N4} seconds\r\n");

            if (_failedFiles.Count > 0)
            {
                _logger.Info("");
                _logger.Warn("Failed files");
                foreach (var failedFile in _failedFiles)
                {
                    _logger.Info($"  {failedFile}");
                }
            }

            if (_fluentCommandLineParser.Object.CsvDirectory.IsNullOrEmpty() == false && files.Count > 0)
            {
                if (Directory.Exists(_fluentCommandLineParser.Object.CsvDirectory) == false)
                {
                    _logger.Warn($"'{_fluentCommandLineParser.Object.CsvDirectory} does not exist. Creating...'");
                    Directory.CreateDirectory(_fluentCommandLineParser.Object.CsvDirectory);
                }

                var outName = $"{DateTimeOffset.Now:yyyyMMddHHmmss}_RBCmd_Output.csv";

                if (_fluentCommandLineParser.Object.CsvName.IsNullOrEmpty() == false)
                {
                    outName = Path.GetFileName(_fluentCommandLineParser.Object.CsvName);
                }

                var outFile = Path.Combine(_fluentCommandLineParser.Object.CsvDirectory, outName);

                _fluentCommandLineParser.Object.CsvDirectory =
                    Path.GetFullPath(outFile);
                _logger.Warn(
                    $"CSV output will be saved to '{Path.GetFullPath(outFile)}'");

                try
                {
                    var sw1 = new StreamWriter(outFile);
                    var csv = new CsvWriter(sw1);

                    csv.WriteHeader(typeof(CsvOut));
                    csv.NextRecord();

                    foreach (var csvOut in _csvOuts)
                    {
                        csv.WriteRecord(csvOut);
                        csv.NextRecord();
                    }

                    sw1.Flush();
                    sw1.Close();
                }
                catch (Exception ex)
                {
                    _logger.Error(
                        $"Unable to open '{outFile}' for writing. CSV export canceled. Error: {ex.Message}");
                }
            }
        }
Exemple #43
0
        private static FluentCommandLineParser <ExecutionEnvironment> SetupArguments()
        {
            var fluentParser = new FluentCommandLineParser <ExecutionEnvironment>();

            fluentParser.Setup(arg => arg.Title)
            .As('e', "title")
            .WithDescription("\nSelected tests to be ran");

            fluentParser.Setup(arg => arg.Tests)
            .As('t', "tests")
            .WithDescription("\nSelected tests to be ran");

            fluentParser.Setup(arg => arg.TestsLocation)
            .As('d', "tests-location")
            .WithDescription("\nSelected tests location to be ran");

            fluentParser.Setup(arg => arg.ParallelScopeString)
            .As('p', "parallel-scope")
            .WithDescription("\nDefine the running Parallel scope (None/All) in series or in parallel");

            fluentParser.Setup(arg => arg.BrowserTypeString)
            .As('b', "browser-type").SetDefault("chrome")
            .WithDescription("\nDefine the running browser type");

            fluentParser.Setup(arg => arg.OutputDirectoryLocation)
            .As('o', "output-directory")
            .WithDescription("\nDefine the output directory");

            fluentParser.Setup(arg => arg.ReportTemplate)
            .As('r', "report-template")
            .WithDescription("\nDefine the report template");

            fluentParser.Setup(arg => arg.ContextDirectoryLocation)
            .As('c', "contexts-directory")
            .WithDescription("\nDefine the context directory");

            fluentParser.Setup(arg => arg.ScenarioDirectoryLocation)
            .As('s', "scenario-directory")
            .WithDescription("\nDefine the scenario directory");

            fluentParser.Setup(arg => arg.SiteMapDirectoryLocation)
            .As('m', "sitemap-directory")
            .WithDescription("\nDefine the sitemap directory");

            fluentParser.Setup(arg => arg.GridEnabled)
            .As('g', "grid-enabled")
            .WithDescription("\nExecute test on remote selenium grid");

            fluentParser.Setup(arg => arg.InteractiveMode)
            .As('i', "interactive-mode")
            .WithDescription("\nEnable interactive mode (display report after execution)");

            fluentParser.Setup(arg => arg.BinaryLocation)
            .As('x', "binary-location")
            .WithDescription("\nLocation to the browser exe");

            fluentParser.Setup(arg => arg.LogLevelString)
            .As('l', "log-level")
            .WithDescription("\nChoose the log level");

            fluentParser.Setup(arg => arg.SeleniumHubAddress)
            .As('h', "selenium-hub")
            .WithDescription("\nChoose the selenium hub");

            fluentParser.SetupHelp("?", "help")
            .Callback(text => _log_.Info(text));

            return(fluentParser);
        }
Exemple #44
0
        private static void DoFileDecon(string[] args)
        {
            var p = new FluentCommandLineParser <ApplicationArguments>();

            p.Setup(arg => arg.AggregationTolerancePpm)
            .As("AggregationTolerancePpm");

            p.Setup(arg => arg.DeconvolutionTolerancePpm)
            .As("DeconvolutionTolerancePpm");

            p.Setup(arg => arg.MinScan)
            .As("MinScan");

            p.Setup(arg => arg.MaxScan)
            .As("MaxScan");

            p.Setup(arg => arg.MinAssumedChargeState)
            .As("MinAssumedChargeState");

            p.Setup(arg => arg.MaxAssumedChargeState)
            .As("MaxAssumedChargeState");

            p.Setup(arg => arg.IntensityRatioLimit)
            .As("IntensityRatioLimit");

            p.Setup(arg => arg.AverageScans)
            .As("AverageScans");

            p.Setup(arg => arg.NumScansRequired)
            .As("NumScansRequired");

            p.Setup(arg => arg.FilePath)
            .As("FilePath").
            Required();

            var result = p.Parse(args);

            Console.WriteLine("Running deconvolution using the following parameters:");
            Console.WriteLine(p.Object);

            if (result.HasErrors == false)
            {
                IMsDataFile <IMsDataScan <IMzSpectrum <IMzPeak> > > myMsDataFile;
                if (Path.GetExtension(p.Object.FilePath).Equals(".mzML", StringComparison.OrdinalIgnoreCase))
                {
                    myMsDataFile = Mzml.LoadAllStaticData(p.Object.FilePath);
                }
                else
                {
                    myMsDataFile = ThermoStaticData.LoadAllStaticData(p.Object.FilePath);
                }

                if (p.Object.AverageScans > 1)
                {
                    myMsDataFile = new SummedMsDataFile(myMsDataFile, p.Object.AverageScans, p.Object.DeconvolutionTolerancePpm);
                }

                using (StreamWriter output = new StreamWriter(@"DeconvolutionOutput-" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss", CultureInfo.InvariantCulture) + ".tsv"))
                {
                    output.WriteLine("Mass\tScore\tNumPeaks\tNumScans\tMinScan\tMaxScan\tElutionStart\tElutionEnd\tTotalNormalizedIntensity\tObservedCharges\tMostIntenseElutionTime\tMostIntenseCharge\tMostIntenseMz\tNumPeaksInMostIntenseEnvelope\tMostIntenseEnvelopeIntensity\tElutionOfMostIntenseCharge");

                    foreach (var nice in myMsDataFile.Deconvolute(p.Object.MinScan, p.Object.MaxScan, p.Object.MinAssumedChargeState, p.Object.MaxAssumedChargeState, p.Object.DeconvolutionTolerancePpm, p.Object.IntensityRatioLimit, p.Object.AggregationTolerancePpm, b => b.MsnOrder == 1).OrderByDescending(b => b.Score))
                    {
                        if ((nice.MaxScanIndex - nice.MinScanIndex + 1) >= p.Object.NumScansRequired)
                        {
                            output.WriteLine(nice.OneLineString());
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("BAD PARAMETERS");
                Console.WriteLine(result.ErrorText);
            }
        }
        public CliParser()
        {
            _product = new Product();
            _license = new License();

            _parser = new FluentCommandLineParser
            {
                IsCaseSensitive = false
            };

            _parser.SetupHelp("?", "help")
            .Callback(text => Console.WriteLine(text));

            _parser.Setup <string>('k', "key")
            .Callback(privatKey =>
            {
                _product.PrivateKey = privatKey;
                _keySet             = true;
            });

            _parser.Setup <string>('p', "key-path")
            .Callback(path => _keyPath = path);

            _parser.Setup <string>('n', "name")
            .Callback(name => _license.OwnerName = name)
            .Required();

            _parser.Setup <string>('i', "id")
            .Callback(s =>
            {
                if (!Guid.TryParse(s, out var guid))
                {
                    Console.WriteLine($"\"{s}\" is not a valid Id!\n" +
                                      "Please use either of these formats for Ids:\n" +
                                      $"\t{Guid.Empty:N}\n" +
                                      $"\t{Guid.Empty:B}\n" +
                                      $"\t{Guid.Empty:D}\n" +
                                      $"\t{Guid.Empty:P}\n" +
                                      $"\t{Guid.Empty:X}\n");

                    throw new Exception();
                }

                _license.ID = guid;
            });

            _parser.Setup <string>('e', "exp")
            .Callback(exp =>
            {
                var dateParser = new DateParser();
                if (!dateParser.Parse(exp, out var date))
                {
                    Console.WriteLine($"\"{exp}\" is not a valid Date!\n" +
                                      "Please use either of these formats for Dates:\n" +
                                      $"{dateParser.Examples}\n");

                    throw new Exception();
                }

                _license.ExpirationDate = date;
            });

            _parser.Setup <LicenseType>('l', "lic")
            .Callback(lic => _license.LicenseType = lic)
            .Required();
        }
Exemple #46
0
        public static void Main(string[] args)
        {
            Action <Repository> checkoutOld = null;
            Action <Repository> checkoutNew = null;

            var p = new FluentCommandLineParser();

            p.Setup <List <string> >('c', "commit")
            .Callback(items =>
            {
                checkoutOld = r => r.CheckoutHash(items[0]);
                checkoutNew = r => r.CheckoutHash(items[1]);
            })
            .WithDescription("[old] [new]\n\t Checkout by commit hash");

            p.Setup <bool>('l', "latest")
            .Callback(x =>
            {
                checkoutOld = r => r.CheckoutTagFromEnd(1);
                checkoutNew = r => r.CheckoutTagFromEnd(0);
            })
            .WithDescription("\n\t Checkout the two latest tags");

            p.Setup <List <string> >('t', "tag")
            .Callback(items =>
            {
                checkoutOld = r => r.CheckoutTag(items[0]);
                checkoutNew = r => r.CheckoutTag(items[1]);
            })
            .WithDescription("\n\t Checkout by tag");

            p.SetupHelp("?", "help")
            .Callback(text => Console.WriteLine(text));

            var result = p.Parse(args);

            _gitRepo  = ConfigurationManager.AppSettings["git"];
            _solution = ConfigurationManager.AppSettings["solution"];
            _username = ConfigurationManager.AppSettings["user"];
            _passwd   = ConfigurationManager.AppSettings["passwd"];
            if (string.IsNullOrWhiteSpace(_gitRepo) || string.IsNullOrWhiteSpace(_solution))
            {
                Console.WriteLine("Please configure the git and solution options in App.config");
                return;
            }

            if (result.HasErrors)
            {
                Console.WriteLine(result.ErrorText);
            }
            else if (result.EmptyArgs)
            {
                Console.Write("Please choose an option");
                p.Parse(new[] { "-?" });
            }
            else if (checkoutOld != null && checkoutNew != null)
            {
                Compare(checkoutOld, checkoutNew).Wait();
            }
            else
            {
                Console.Write("No known option found");
                p.Parse(new[] { "-?" });
            }
        }
Exemple #47
0
            public static void readFromCommandLine(string[] args)
            {
                // first pass
                var cmd_parser = new FluentCommandLineParser();

                // help
                cmd_parser.SetupHelp("h", "help").Callback(text => outputHelp());

                // config file
                cmd_parser.Setup <string>("config").Callback(value => configFilename = value).Required();

                cmd_parser.Parse(args);

                if (DLTNode.Program.noStart)
                {
                    return;
                }

                readConfigFile(configFilename);



                // second pass
                cmd_parser = new FluentCommandLineParser();

                // testnet
                cmd_parser.Setup <bool>('t', "testnet").Callback(value => isTestNet = true).Required();

                cmd_parser.Parse(args);

                if (isTestNet)
                {
                    serverPort                = testnetServerPort;
                    apiPort                   = testnetApiPort;
                    dataFolderPath            = "data-testnet";
                    PeerStorage.peersFilename = "testnet-peers.dat";
                    genesisFile               = "testnet-genesis.dat";
                }



                string seedNode = "";

                // third pass
                cmd_parser = new FluentCommandLineParser();

                bool start_clean = false; // Flag to determine if node should delete cache+logs

                // version
                cmd_parser.Setup <bool>('v', "version").Callback(text => outputVersion());

                // Toggle between full history node and no history
                cmd_parser.Setup <bool>('s', "save-history").Callback(value => storeFullHistory = value).Required();

                // Toggle worker-only mode
                cmd_parser.Setup <bool>("worker").Callback(value => workerOnly = true).Required();

                // Check for password change
                cmd_parser.Setup <bool>('x', "changepass").Callback(value => changePass = value).Required();

                // Check for recovery parameter
                cmd_parser.Setup <bool>("recover").Callback(value => recoverFromFile = value).Required();

                // Check for clean parameter
                cmd_parser.Setup <bool>('c', "clean").Callback(value => start_clean = value).Required();


                cmd_parser.Setup <int>('p', "port").Callback(value => serverPort = value).Required();

                cmd_parser.Setup <int>('a', "apiport").Callback(value => apiPort = value).Required();

                cmd_parser.Setup <string>('i', "ip").Callback(value => externalIp = value).Required();

                cmd_parser.Setup <string>("genesis").Callback(value => genesisFunds = value).Required();

                cmd_parser.Setup <string>("genesis2").Callback(value => genesis2Address = value).Required();

                cmd_parser.Setup <int>("threads").Callback(value => miningThreads = (uint)value).Required();

                cmd_parser.Setup <string>('w', "wallet").Callback(value => walletFile = value).Required();

                cmd_parser.Setup <string>('n', "node").Callback(value => seedNode = value).Required();

                cmd_parser.Setup <int>("maxLogSize").Callback(value => maxLogSize = value).Required();

                cmd_parser.Setup <int>("maxLogCount").Callback(value => maxLogCount = value).Required();

                cmd_parser.Setup <long>("lastGoodBlock").Callback(value => lastGoodBlock = (ulong)value).Required();

                cmd_parser.Setup <bool>("disableWebStart").Callback(value => disableWebStart = true).Required();

                cmd_parser.Setup <string>("dataFolderPath").Callback(value => dataFolderPath = value).Required();

                cmd_parser.Setup <bool>("disableMiner").Callback(value => disableMiner = true).Required();

                cmd_parser.Setup <bool>("verifyStorage").Callback(value => fullStorageDataVerification = true).Required();

                cmd_parser.Setup <bool>("onlyShowAddresses").Callback(value => onlyShowAddresses = true).Required();


                // Debug
                cmd_parser.Setup <string>("netdump").Callback(value => networkDumpFile = value).SetDefault("");

                cmd_parser.Setup <int>("forceTimeOffset").Callback(value => forceTimeOffset = value).Required();

                cmd_parser.Setup <int>("benchmarkKeys").Callback(value => benchmarkKeys = value).SetDefault(0);

                cmd_parser.Setup <bool>("generateWallet").Callback(value => generateWalletOnly = value).SetDefault(false);

                cmd_parser.Setup <string>("walletPassword").Callback(value => dangerCommandlinePasswordCleartextUnsafe = value).SetDefault("");

                cmd_parser.Parse(args);


                Storage.path            = dataFolderPath + Path.DirectorySeparatorChar + "blocks";
                WalletStateStorage.path = dataFolderPath + Path.DirectorySeparatorChar + "ws";

                if (start_clean)
                {
                    Node.checkDataFolder();
                    Node.cleanCacheAndLogs();
                }

                if (miningThreads < 1)
                {
                    miningThreads = 1;
                }

                if (seedNode != "")
                {
                    if (isTestNet)
                    {
                        Network.CoreNetworkUtils.seedTestNetNodes = new List <string>
                        {
                            seedNode
                        };
                    }
                    else
                    {
                        Network.CoreNetworkUtils.seedNodes = new List <string>
                        {
                            seedNode
                        };
                    }
                }
            }
Exemple #48
0
        static void Main(string[] args)
        {
            Bootstrapper.Register();

            var    p         = new FluentCommandLineParser();
            string inputName = null;

            // Parse command line arguments
            bool   isMtgDbInfo      = false;
            string mtgDbInfoSet     = null;
            bool   isMtgJson        = false;
            string mtgJsonFilename  = null;
            bool   skipTokens       = true;
            string mtgJsonDirectory = null;
            bool   importByFile     = true;

            try
            {
                mStopwatch      = Stopwatch.StartNew();
                mLoggingService = TinyIoCContainer.Current.Resolve <ILoggingService>();
                mMtgStore       = TinyIoCContainer.Current.Resolve <IMtgStore>();
                mFileSystem     = TinyIoCContainer.Current.Resolve <IFileSystem>();
                mImporter       = TinyIoCContainer.Current.Resolve <IImporter>();

                p.Setup <string>("set")
                .Callback(value => mtgDbInfoSet = value)
                .Required();

                p.Setup <bool>("mtgjson")
                .Callback(value => isMtgJson = value)
                .SetDefault(true);

                p.Setup <string>("file")
                .Callback(value => mtgJsonFilename = value);

                p.Setup <string>("dir")
                .Callback(value => mtgJsonDirectory = value);

                p.Setup <bool>("skiptokens")
                .Callback(value => skipTokens = value)
                .SetDefault(true);

                p.Setup <bool>("skippromos")
                .Callback(value => mSkipPromos = value)
                .SetDefault(true);

                p.Parse(args);

                // Make sure we have either --file or --dir
                if (string.IsNullOrEmpty(mtgJsonDirectory) && string.IsNullOrEmpty(mtgJsonFilename))
                {
                    throw new Exception("You must either use --file or --dir.");
                }

                if (!string.IsNullOrEmpty(mtgJsonDirectory))
                {
                    importByFile = false;
                }

                if (isMtgJson && importByFile)
                {
                    inputName = mtgJsonFilename;
                }
                else if (isMtgJson && !importByFile)
                {
                    inputName = mtgJsonDirectory;

                    if (!Directory.Exists(inputName))
                    {
                        throw new DirectoryNotFoundException(inputName);
                    }
                }
                else
                {
                    throw new Exception("Please provide either --mtgdbinfo or --mtgjson arguments.");
                }

                mDataReader = new MtgJsonReader(
                    TinyIoCContainer.Current.Resolve <IMtgDataMapper <MtgJsonCard, MtgJsonSet> >("MtgJson"),
                    mLoggingService);

                mDataReader.SkipTokens = skipTokens;

                // Setup worker
                mUpdaterBackgroundWorker = new BackgroundWorker();
                mUpdaterBackgroundWorker.WorkerReportsProgress      = true;
                mUpdaterBackgroundWorker.WorkerSupportsCancellation = true;
                mUpdaterBackgroundWorker.DoWork             += new DoWorkEventHandler(bw_UpdaterDoWork);
                mUpdaterBackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_Completed);
                mUpdaterBackgroundWorker.ProgressChanged    += new ProgressChangedEventHandler(bw_ProgressChanged);

                Console.WriteLine("Running worker thread...");
                mUpdaterBackgroundWorker.RunWorkerAsync(new WorkArgs {
                    ImportByFile = importByFile, InputName = inputName
                });
            }
            catch (Exception er)
            {
                string msg = string.Format("Error: {0}", er.Message);

                mLoggingService.Fatal(er, msg);
                Console.WriteLine(msg);
            }

            while (mUpdaterBackgroundWorker.IsBusy)
            {
                Thread.Sleep(100);
            }
        }
Exemple #49
0
        private static void Main(string[] args)
        {
            ExceptionlessClient.Default.Startup("prIG996gFK1y6DaZEoXh3InSg8LwrHcQV4Dze2r8");
            SetupNLog();

            _logger = LogManager.GetCurrentClassLogger();

            if (!CheckForDotnet46())
            {
                _logger.Warn(".net 4.6 not detected. Please install .net 4.6 and try again.");
                return;
            }


            _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.File)
            .As('f')
            .WithDescription("AmcacheOld.hve file to parse. Required").Required();

            _fluentCommandLineParser.Setup(arg => arg.IncludeLinked)
            .As('i').SetDefault(false)
            .WithDescription("Include file entries for Programs entries");

            _fluentCommandLineParser.Setup(arg => arg.Whitelist)
            .As('w')
            .WithDescription(
                "Path to file containing SHA-1 hashes to *exclude* from the results. Blacklisting overrides whitelisting\r\n");

            _fluentCommandLineParser.Setup(arg => arg.Blacklist)
            .As('b')
            .WithDescription(
                "Path to file containing SHA-1 hashes to *include* from the results. Blacklisting overrides whitelisting");

            _fluentCommandLineParser.Setup(arg => arg.SaveTo)
            .As("csv").Required()
            .WithDescription("Directory where results will be saved. Required");


            _fluentCommandLineParser.Setup(arg => arg.DateTimeFormat)
            .As("dt")
            .WithDescription(
                "The custom date/time format to use when displaying timestamps. See https://goo.gl/CNVq0k for options. Default is: yyyy-MM-dd HH:mm:ss")
            .SetDefault("yyyy-MM-dd HH:mm:ss");

            _fluentCommandLineParser.Setup(arg => arg.PreciseTimestamps)
            .As("mp")
            .WithDescription(
                "When true, display higher precision for timestamps. Default is false").SetDefault(false);

            var header =
                $"AmcacheParser version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nhttps://github.com/EricZimmerman/AmcacheParser";

            var footer = @"Examples: AmcacheParser.exe -f ""C:\Temp\amcache\AmcacheWin10.hve"" --csv C:\temp" +
                         "\r\n\t " +
                         @" AmcacheParser.exe -f ""C:\Temp\amcache\AmcacheWin10.hve"" -i on --csv C:\temp" + "\r\n\t " +
                         @" AmcacheParser.exe -f ""C:\Temp\amcache\AmcacheWin10.hve"" -w ""c:\temp\whitelist.txt"" --csv C:\temp" +
                         "\r\n\t" +
                         "\r\n\t" +
                         "  Short options (single letter) are prefixed with a single dash. Long commands are prefixed with two dashes\r\n";

            _fluentCommandLineParser.SetupHelp("?", "help")
            .WithHeader(header)
            .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (result.HelpCalled)
            {
                return;
            }

            if (result.HasErrors)
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("Both -f and --csv are required. Exiting");

                return;
            }

            if (!File.Exists(_fluentCommandLineParser.Object.File))
            {
                _logger.Warn($"'{_fluentCommandLineParser.Object.File}' not found. Exiting");
                return;
            }

            _logger.Info(header);
            _logger.Info("");
            _logger.Info($"Command line: {string.Join(" ", args)}");
            _logger.Info("");

            if (_fluentCommandLineParser.Object.PreciseTimestamps)
            {
                _fluentCommandLineParser.Object.DateTimeFormat = _preciseTimeFormat;
            }


            _sw = new Stopwatch();
            _sw.Start();

            try
            {
                //determine format here
                //fork accordingly

                if (Helper.IsNewFormat(_fluentCommandLineParser.Object.File))
                {
                    var amNew = new AmcacheNew(_fluentCommandLineParser.Object.File,
                                               _fluentCommandLineParser.Object.RecoverDeleted);

                    if (amNew.ProgramsEntries.Count == 0 && amNew.UnassociatedFileEntries.Count == 0)
                    {
                        _logger.Warn("Hive did not contain program entries nor file entries.");
                    }

                    _sw.Stop();

                    var whitelistHashes1 = new HashSet <string>();

                    var useBlacklist2 = false;

                    if (_fluentCommandLineParser.Object.Blacklist.Length > 0)
                    {
                        if (File.Exists(_fluentCommandLineParser.Object.Blacklist))
                        {
                            foreach (var readLine in File.ReadLines(_fluentCommandLineParser.Object.Blacklist))
                            {
                                whitelistHashes1.Add(readLine.ToLowerInvariant());
                            }
                            useBlacklist2 = true;
                        }
                        else
                        {
                            _logger.Warn($"'{_fluentCommandLineParser.Object.Blacklist}' does not exist");
                        }
                    }
                    else if (_fluentCommandLineParser.Object.Whitelist.Length > 0)
                    {
                        if (File.Exists(_fluentCommandLineParser.Object.Whitelist))
                        {
                            foreach (var readLine in File.ReadLines(_fluentCommandLineParser.Object.Whitelist))
                            {
                                whitelistHashes1.Add(readLine.ToLowerInvariant());
                            }
                        }
                        else
                        {
                            _logger.Warn($"'{_fluentCommandLineParser.Object.Whitelist}' does not exist");
                        }
                    }

                    var cleanList2 =
                        amNew.UnassociatedFileEntries.Where(t => whitelistHashes1.Contains(t.SHA1) == useBlacklist2)
                        .ToList();
                    var totalProgramFileEntries2 = 0;

                    if (Directory.Exists(_fluentCommandLineParser.Object.SaveTo) == false)
                    {
                        try
                        {
                            Directory.CreateDirectory(_fluentCommandLineParser.Object.SaveTo);
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(
                                $"There was an error creating directory '{_fluentCommandLineParser.Object.SaveTo}'. Error: {ex.Message} Exiting");
                            return;
                        }
                    }

                    foreach (var pe in amNew.ProgramsEntries)
                    {
                        var cleanList22 =
                            pe.FileEntries.Where(t => whitelistHashes1.Contains(t.SHA1) == useBlacklist2).ToList();
                        totalProgramFileEntries2 += cleanList22.Count;
                    }

                    var ts1       = DateTime.Now.ToString("yyyyMMddHHmmss");
                    var hiveName1 = Path.GetFileNameWithoutExtension(_fluentCommandLineParser.Object.File);

                    var outbase1 = $"{ts1}_{hiveName1}_Unassociated file entries.tsv";
                    var outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);



                    using (var sw = new StreamWriter(outFile1))
                    {
                        sw.AutoFlush = true;

                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new FECacheOutputMapNew(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <FileEntryNew>();
                        csv.WriteRecords(cleanList2);
                    }


                    if (_fluentCommandLineParser.Object.IncludeLinked)
                    {
                        outbase1 = $"{ts1}_{hiveName1}_Program entries.tsv";
                        outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);

                        using (var sw = new StreamWriter(outFile1))
                        {
                            sw.AutoFlush = true;

                            var csv = new CsvWriter(sw);
                            csv.Configuration.RegisterClassMap(
                                new PECacheOutputMapNew(_fluentCommandLineParser.Object.DateTimeFormat));
                            csv.Configuration.Delimiter = "\t";

                            csv.WriteHeader <ProgramsEntryNew>();
                            csv.WriteRecords(amNew.ProgramsEntries);
                        }

                        outbase1 = $"{ts1}_{hiveName1}_Associated file entries.tsv";
                        outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);

                        using (var sw = new StreamWriter(outFile1))
                        {
                            var csv = new CsvWriter(sw);
                            csv.Configuration.RegisterClassMap(
                                new FECacheOutputMapNew(_fluentCommandLineParser.Object.DateTimeFormat));
                            csv.Configuration.Delimiter = "\t";

                            csv.WriteHeader <FileEntryNew>();

                            sw.AutoFlush = true;

                            foreach (var pe in amNew.ProgramsEntries)
                            {
                                var cleanList22 =
                                    pe.FileEntries.Where(t => whitelistHashes1.Contains(t.SHA1) == useBlacklist2)
                                    .ToList();

                                csv.WriteRecords(cleanList22);
                            }
                        }
                    }


                    //DUMP NEW STUFF HERE


                    outbase1 = $"{ts1}_{hiveName1}_ShortCuts.tsv";
                    outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);

                    using (var sw = new StreamWriter(outFile1))
                    {
                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new ShortCuts(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <Shortcut>();

                        sw.AutoFlush = true;

                        foreach (var sc in amNew.ShortCuts)
                        {
                            csv.WriteRecord(sc);
                        }
                    }

                    outbase1 = $"{ts1}_{hiveName1}_DriveBinaries.tsv";
                    outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);

                    using (var sw = new StreamWriter(outFile1))
                    {
                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new DriverBinaries(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <DriverBinary>();

                        sw.AutoFlush = true;

                        foreach (var sc in amNew.DriveBinaries)
                        {
                            csv.WriteRecord(sc);
                        }
                    }


                    outbase1 = $"{ts1}_{hiveName1}_DeviceContainers.tsv";
                    outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);

                    using (var sw = new StreamWriter(outFile1))
                    {
                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new DeviceContainers(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <DeviceContainer>();

                        sw.AutoFlush = true;

                        foreach (var sc in amNew.DeviceContainers)
                        {
                            csv.WriteRecord(sc);
                        }
                    }

                    outbase1 = $"{ts1}_{hiveName1}_DriverPackages.tsv";
                    outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);

                    using (var sw = new StreamWriter(outFile1))
                    {
                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new DriverPackages(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <DriverPackage>();

                        sw.AutoFlush = true;

                        foreach (var sc in amNew.DriverPackages)
                        {
                            csv.WriteRecord(sc);
                        }
                    }

                    outbase1 = $"{ts1}_{hiveName1}_DevicePnps.tsv";
                    outFile1 = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase1);

                    using (var sw = new StreamWriter(outFile1))
                    {
                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new DevicePnps(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <DevicePnp>();

                        sw.AutoFlush = true;

                        foreach (var sc in amNew.DevicePnps)
                        {
                            csv.WriteRecord(sc);
                        }
                    }


                    _logger.Error($"\r\n'{_fluentCommandLineParser.Object.File}' is in new format!");

                    var suffix1 = amNew.UnassociatedFileEntries.Count == 1 ? "y" : "ies";


                    var linked1 = "";
                    if (_fluentCommandLineParser.Object.IncludeLinked)
                    {
                        linked1 =
                            $"and {totalProgramFileEntries2:N0} program file entries (across {amNew.ProgramsEntries.Count:N0} program entries) ";
                    }


                    _logger.Info("");

                    _logger.Info($"Total file entries found: {amNew.TotalFileEntries:N0}");
                    if (amNew.ShortCuts.Count > 0)
                    {
                        _logger.Info($"Total shortcuts found: {amNew.ShortCuts.Count:N0}");
                    }
                    if (amNew.DeviceContainers.Count > 0)
                    {
                        _logger.Info($"Total device containers found: {amNew.DeviceContainers.Count:N0}");
                    }

                    if (amNew.DevicePnps.Count > 0)
                    {
                        _logger.Info($"Total device PnPs found: {amNew.DevicePnps.Count:N0}");
                    }

                    if (amNew.DriveBinaries.Count > 0)
                    {
                        _logger.Info($"Total drive binaries found: {amNew.DriveBinaries.Count:N0}");
                    }

                    if (amNew.DriverPackages.Count > 0)
                    {
                        _logger.Info($"Total driver packages found: {amNew.DriverPackages.Count:N0}");
                    }


                    _logger.Info(
                        $"\r\nFound {cleanList2.Count:N0} unassociated file entr{suffix1} {linked1}");

                    if (whitelistHashes1.Count > 0)
                    {
                        var per = (double)(totalProgramFileEntries2 + cleanList2.Count) / amNew.TotalFileEntries;

                        _logger.Info("");

                        var list = "whitelist";
                        if (_fluentCommandLineParser.Object.Blacklist.Length > 0)
                        {
                            list = "blacklist";
                        }

                        _logger.Info($"{UppercaseFirst(list)} hash count: {whitelistHashes1.Count:N0}");

                        _logger.Info("");

                        _logger.Info($"Percentage of total shown based on {list}: {per:P3} ({1 - per:P3} savings)");
                    }
                    _logger.Info("");

                    _logger.Info($"Results saved to: {_fluentCommandLineParser.Object.SaveTo}");


                    _logger.Info("");
                    _logger.Info(
                        $"Total parsing time: {_sw.Elapsed.TotalSeconds:N3} seconds.\r\n");


                    return;
                }

                var am = new AmcacheOld(_fluentCommandLineParser.Object.File,
                                        _fluentCommandLineParser.Object.RecoverDeleted);


                if (am.ProgramsEntries.Count == 0 && am.UnassociatedFileEntries.Count == 0)
                {
                    _logger.Warn("Hive did not contain program entries nor file entries. Exiting");
                    return;
                }

                _sw.Stop();

                var whitelistHashes = new HashSet <string>();

                var useBlacklist = false;

                if (_fluentCommandLineParser.Object.Blacklist.Length > 0)
                {
                    if (File.Exists(_fluentCommandLineParser.Object.Blacklist))
                    {
                        foreach (var readLine in File.ReadLines(_fluentCommandLineParser.Object.Blacklist))
                        {
                            whitelistHashes.Add(readLine.ToLowerInvariant());
                        }
                        useBlacklist = true;
                    }
                    else
                    {
                        _logger.Warn($"'{_fluentCommandLineParser.Object.Blacklist}' does not exist");
                    }
                }
                else if (_fluentCommandLineParser.Object.Whitelist.Length > 0)
                {
                    if (File.Exists(_fluentCommandLineParser.Object.Whitelist))
                    {
                        foreach (var readLine in File.ReadLines(_fluentCommandLineParser.Object.Whitelist))
                        {
                            whitelistHashes.Add(readLine.ToLowerInvariant());
                        }
                    }
                    else
                    {
                        _logger.Warn($"'{_fluentCommandLineParser.Object.Whitelist}' does not exist");
                    }
                }

                var cleanList =
                    am.UnassociatedFileEntries.Where(t => whitelistHashes.Contains(t.SHA1) == useBlacklist).ToList();
                var totalProgramFileEntries = 0;

                if (Directory.Exists(_fluentCommandLineParser.Object.SaveTo) == false)
                {
                    try
                    {
                        Directory.CreateDirectory(_fluentCommandLineParser.Object.SaveTo);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(
                            $"There was an error creating directory '{_fluentCommandLineParser.Object.SaveTo}'. Error: {ex.Message} Exiting");
                        return;
                    }
                }


                foreach (var pe in am.ProgramsEntries)
                {
                    var cleanList2 =
                        pe.FileEntries.Where(t => whitelistHashes.Contains(t.SHA1) == useBlacklist).ToList();
                    totalProgramFileEntries += cleanList2.Count;
                }

                var ts       = DateTime.Now.ToString("yyyyMMddHHmmss");
                var hiveName = Path.GetFileNameWithoutExtension(_fluentCommandLineParser.Object.File);

                var outbase = $"{ts}_{hiveName}_Unassociated file entries.tsv";
                var outFile = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase);

                using (var sw = new StreamWriter(outFile))
                {
                    sw.AutoFlush = true;

                    var csv = new CsvWriter(sw);
                    csv.Configuration.RegisterClassMap(
                        new FECacheOutputMapOld(_fluentCommandLineParser.Object.DateTimeFormat));
                    csv.Configuration.Delimiter = "\t";

                    csv.WriteHeader <FileEntryOld>();
                    csv.WriteRecords(cleanList);
                }

                if (_fluentCommandLineParser.Object.IncludeLinked)
                {
                    outbase = $"{ts}_{hiveName}_Program entries.tsv";
                    outFile = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase);

                    using (var sw = new StreamWriter(outFile))
                    {
                        sw.AutoFlush = true;

                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new PECacheOutputMapOld(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <ProgramsEntryOld>();
                        csv.WriteRecords(am.ProgramsEntries);
                    }

                    outbase = $"{ts}_{hiveName}_Associated file entries.tsv";
                    outFile = Path.Combine(_fluentCommandLineParser.Object.SaveTo, outbase);

                    using (var sw = new StreamWriter(outFile))
                    {
                        var csv = new CsvWriter(sw);
                        csv.Configuration.RegisterClassMap(
                            new FECacheOutputMapOld(_fluentCommandLineParser.Object.DateTimeFormat));
                        csv.Configuration.Delimiter = "\t";

                        csv.WriteHeader <FileEntryOld>();

                        sw.AutoFlush = true;

                        foreach (var pe in am.ProgramsEntries)
                        {
                            var cleanList2 =
                                pe.FileEntries.Where(t => whitelistHashes.Contains(t.SHA1) == useBlacklist).ToList();

                            csv.WriteRecords(cleanList2);
                        }
                    }
                }

                _logger.Error($"\r\n'{_fluentCommandLineParser.Object.File}' is in old format!");

                var suffix = am.UnassociatedFileEntries.Count == 1 ? "y" : "ies";


                var linked = "";
                if (_fluentCommandLineParser.Object.IncludeLinked)
                {
                    linked =
                        $"and {totalProgramFileEntries:N0} program file entries (across {am.ProgramsEntries.Count:N0} program entries) ";
                }


                _logger.Info("");

                _logger.Info($"Total file entries found: {am.TotalFileEntries:N0}");

                _logger.Info(
                    $"Found {cleanList.Count:N0} unassociated file entr{suffix} {linked}");

                if (whitelistHashes.Count > 0)
                {
                    var per = (double)(totalProgramFileEntries + cleanList.Count) / am.TotalFileEntries;

                    _logger.Info("");

                    var list = "whitelist";
                    if (_fluentCommandLineParser.Object.Blacklist.Length > 0)
                    {
                        list = "blacklist";
                    }

                    _logger.Info($"{UppercaseFirst(list)} hash count: {whitelistHashes.Count:N0}");

                    _logger.Info("");

                    _logger.Info($"Percentage of total shown based on {list}: {per:P3} ({1 - per:P3} savings)");
                }
                _logger.Info("");

                _logger.Info($"Results saved to: {_fluentCommandLineParser.Object.SaveTo}");


                _logger.Info("");
                _logger.Info(
                    $"Total parsing time: {_sw.Elapsed.TotalSeconds:N3} seconds.\r\n");
            }
            catch (Exception ex)
            {
                _logger.Error($"There was an error: {ex.Message}");
                _logger.Error($"Stacktrace: {ex.StackTrace}");
                _logger.Info("");
                _logger.Error(
                    $"Please send '{_fluentCommandLineParser.Object.File}' to [email protected] in order to fix the issue");
            }
        }
Exemple #50
0
        static void Main(string[] args)
        {
            var pollingIntervalFloor  = 10000;
            var defaultCompName       = Environment.MachineName;
            var defaultConfigFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            var defaultConfigFile     = defaultConfigFilePath + "\\config.json";

            // create a generic parser for the ApplicationArguments type
            var parser = new FluentCommandLineParser <Options>();

            parser.Setup(arg => arg.ConfigFile)
            .As('c', "configFile")
            .SetDefault(defaultConfigFile)
            .WithDescription("Config file to use");

            parser.Setup(arg => arg.PollingInterval)
            .As('i', "pollInt")
            .SetDefault(pollingIntervalFloor)
            .WithDescription("Frequency of polling (ms)");

            parser.Setup(arg => arg.MachineName)
            .As('n', "compName")
            .SetDefault(defaultCompName)
            .WithDescription("Name of computer that you want to poll");

            parser.Setup(arg => arg.UserName)
            .As('u', "userName")
            .SetDefault(Environment.UserName)
            .WithDescription("User to perform polling");

            parser.Setup(arg => arg.DomainName)
            .As('d', "domainName")
            .SetDefault(Environment.UserDomainName)
            .WithDescription("Domain of user to perform polling");

            parser.Setup(arg => arg.Password)
            .As('p', "password")
            .SetDefault("")
            .WithDescription("Password of user to perform polling");

            parser.Setup(arg => arg.RunOnce)
            .As('r', "runOnce")
            .SetDefault(false)
            .WithDescription("Set to true to run this integration once and exit (instead of polling)");

            parser.Setup(arg => arg.Verbose)
            .As('v', "verbose")
            .SetDefault(false)
            .WithDescription("Verbose logging & pretty-print (for testing purposes)");

            parser.SetupHelp("?", "help")
            .Callback(text => Console.WriteLine(text));

            var parse = parser.Parse(args);

            if (parse.HasErrors)
            {
                parser.HelpOption.ShowHelp(parser.Options);
                Environment.Exit(1);
            }
            else if (parse.HelpCalled)
            {
                Environment.Exit(0);
            }

            var options = parser.Object;

            Log.Verbose = options.Verbose;

            // All of the possibilities for polling interval figured here...
            int pollingInterval = pollingIntervalFloor;

            if (!options.RunOnce)
            {
                string env_PollingInterval = Environment.GetEnvironmentVariable("POLLINGINTERVAL");
                if (String.IsNullOrEmpty(env_PollingInterval) || !int.TryParse(env_PollingInterval, out pollingInterval))
                {
                    pollingInterval = options.PollingInterval;
                }
                if (pollingInterval < pollingIntervalFloor)
                {
                    pollingInterval = pollingIntervalFloor;
                }
                options.PollingInterval = pollingInterval;
            }

            Log.WriteLog(String.Format("nri-perfmon version {0} starting with options", System.Reflection.Assembly.GetEntryAssembly().GetName().Version), options.OptionsWithoutPassword(), Log.LogLevel.INFO);

            List <Counterlist> counterlist = null;

            try
            {
                StreamReader configFileReader = new StreamReader(options.ConfigFile);
                Config       properties       = JsonConvert.DeserializeObject <Config>(configFileReader.ReadToEnd());
                counterlist = properties.counterlist;
                if (counterlist == null || counterlist.Count == 0)
                {
                    Log.WriteLog("'counterlist' is empty. Please verify " + options.ConfigFile + " is in the expected format (see README).", Log.LogLevel.ERROR);
                    Environment.Exit(1);
                }
            }
            catch (IOException ioe)
            {
                Log.WriteLog(String.Format("{0} could not be found or opened.\n {1}", options.ConfigFile, ioe.Message), Log.LogLevel.ERROR);
                Environment.Exit(1);
            }
            catch (JsonReaderException jre)
            {
                Log.WriteLog(String.Format("{0} could not be parsed.\n {1}", options.ConfigFile, jre.Message), Log.LogLevel.ERROR);
                Environment.Exit(1);
            }


            List <Counterlist> mainCounters = new List <Counterlist>();
            List <Thread>      eventThreads = new List <Thread>();

            var splitNames = options.MachineName.Split(SEPARATOR, StringSplitOptions.RemoveEmptyEntries);

            foreach (var thisName in splitNames)
            {
                var thisOptions = new Options(options)
                {
                    MachineName = thisName.Trim()
                };

                foreach (var thisCounter in counterlist)
                {
                    // WMI Event Listeners and WMI Queries with special namespaces get their own thread
                    if (thisCounter.querytype.Equals(PerfmonPlugin.WMIEvent) || !thisCounter.querynamespace.Equals(PerfmonPlugin.DefaultNamespace))
                    {
                        PerfmonPlugin aPlugin = new PerfmonPlugin(thisOptions, thisCounter);
                        Thread        aThread = new Thread(new ThreadStart(aPlugin.RunThread));
                        eventThreads.Add(aThread);
                        aThread.Start();
                    }
                    else
                    {
                        mainCounters.Add(thisCounter);
                    }
                }
            }

            if (mainCounters.Count > 0)
            {
                Log.WriteLog("nri-perfmon counters", (object)mainCounters, Log.LogLevel.VERBOSE);
                PerfmonPlugin thisPlugin = new PerfmonPlugin(options, mainCounters);
                thisPlugin.RunThread();
            }

            // If the main function has nothing or exits, wait on other threads (which should stay running)
            foreach (Thread aThread in eventThreads)
            {
                aThread.Join();
            }
        }
Exemple #51
0
        static void Main(string[] args)
        {
            ExceptionlessClient.Default.Startup("DyZCm8aZbNXf2iZ6BV00wY2UoR3U2tymh3cftNZs");

            SetupNLog();

            _logger = LogManager.GetLogger("EvtxECmd");

            _fluentCommandLineParser = new FluentCommandLineParser <ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.File)
            .As('f')
            .WithDescription("File to process. This or -d is required\r\n");
            _fluentCommandLineParser.Setup(arg => arg.Directory)
            .As('d')
            .WithDescription("Directory to process that contains SQLite files. This or -f is required");

            _fluentCommandLineParser.Setup(arg => arg.CsvDirectory)
            .As("csv")
            .WithDescription(
                "Directory to save CSV formatted results to.");     // This, --json, or --xml required

            _fluentCommandLineParser.Setup(arg => arg.JsonDirectory)
            .As("json")
            .WithDescription(
                "Directory to save JSON formatted results to.\r\n");     // This, --csv, or --xml required

            _fluentCommandLineParser.Setup(arg => arg.Dedupe)
            .As("dedupe")
            .WithDescription(
                "Deduplicate -f or -d files based on SHA-1. First file found wins. Default is TRUE")
            .SetDefault(true);

            _fluentCommandLineParser.Setup(arg => arg.Hunt)
            .As("hunt")
            .WithDescription(
                "When true, all files are looked at regardless of name and file header is used to identify SQLite files, else filename in map is used to find databases. Default is FALSE\r\n  ")
            .SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.MapsDirectory)
            .As("maps")
            .WithDescription(
                "The path where event maps are located. Defaults to 'Maps' folder where program was executed\r\n  ")
            .SetDefault(Path.Combine(BaseDirectory, "Maps"));

            _fluentCommandLineParser.Setup(arg => arg.Sync)
            .As("sync")
            .WithDescription(
                "If true, the latest maps from https://github.com/EricZimmerman/SQLECmd/tree/master/SQLMap/Maps are downloaded and local maps updated. Default is FALSE\r\n")
            .SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.Debug)
            .As("debug")
            .WithDescription("Show debug information during processing").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.Trace)
            .As("trace")
            .WithDescription("Show trace information during processing\r\n").SetDefault(false);

            var header =
                $"SQLECmd version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nhttps://github.com/EricZimmerman/SQLECmd";

            var footer =
                @"Examples: SQLECmd.exe -f ""C:\Temp\someFile.db"" --csv ""c:\temp\out"" " +
                "\r\n\t " +
                @" SQLECmd.exe -d ""C:\Temp\"" --csv ""c:\temp\out""" + "\r\n\t " +
                @" SQLECmd.exe -d ""C:\Temp\"" --hunt --csv ""c:\temp\out""" + "\r\n\t " +
                "\r\n\t" +
                "  Short options (single letter) are prefixed with a single dash. Long commands are prefixed with two dashes\r\n";

            _fluentCommandLineParser.SetupHelp("?", "help")
            .WithHeader(header)
            .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (result.HelpCalled)
            {
                return;
            }

            if (result.HasErrors)
            {
                _logger.Error("");
                _logger.Error(result.ErrorText);

                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                return;
            }

            if (_fluentCommandLineParser.Object.Sync)
            {
                try
                {
                    _logger.Info(header);
                    UpdateFromRepo();
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"There was an error checking for updates: {e.Message}");
                }

                Environment.Exit(0);
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() &&
                _fluentCommandLineParser.Object.Directory.IsNullOrEmpty())
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("-f or -d is required. Exiting");
                return;
            }

            if (_fluentCommandLineParser.Object.CsvDirectory.IsNullOrEmpty())
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("--csv is required. Exiting");
                return;
            }

            _logger.Info(header);
            _logger.Info("");
            _logger.Info($"Command line: {string.Join(" ", Environment.GetCommandLineArgs().Skip(1))}\r\n");

            if (_fluentCommandLineParser.Object.Debug)
            {
                LogManager.Configuration.LoggingRules.First().EnableLoggingForLevel(LogLevel.Debug);
            }

            if (_fluentCommandLineParser.Object.Trace)
            {
                LogManager.Configuration.LoggingRules.First().EnableLoggingForLevel(LogLevel.Trace);
            }

            LogManager.ReconfigExistingLoggers();

            DumpSqliteDll();

            var sw = new Stopwatch();

            sw.Start();

            var ts = DateTimeOffset.UtcNow;

            if (Directory.Exists(_fluentCommandLineParser.Object.MapsDirectory) == false)
            {
                _logger.Warn(
                    $"Maps directory '{_fluentCommandLineParser.Object.MapsDirectory}' does not exist! Database maps will not be loaded!!");
            }
            else
            {
                _logger.Debug($"Loading maps from '{Path.GetFullPath(_fluentCommandLineParser.Object.MapsDirectory)}'");
                var errors = SQLMap.LoadMaps(Path.GetFullPath(_fluentCommandLineParser.Object.MapsDirectory));

                if (errors)
                {
                    return;
                }

                _logger.Info($"Maps loaded: {SQLMap.MapFiles.Count:N0}");
            }

            if (Directory.Exists(_fluentCommandLineParser.Object.CsvDirectory) == false)
            {
                _logger.Warn(
                    $"Path to '{_fluentCommandLineParser.Object.CsvDirectory}' doesn't exist. Creating...");

                try
                {
                    Directory.CreateDirectory(_fluentCommandLineParser.Object.CsvDirectory);
                }
                catch (Exception)
                {
                    _logger.Fatal(
                        $"Unable to create directory '{_fluentCommandLineParser.Object.CsvDirectory}'. Does a file with the same name exist? Exiting");
                    return;
                }
            }

            if (_fluentCommandLineParser.Object.File.IsNullOrEmpty() == false)
            {
                if (File.Exists(_fluentCommandLineParser.Object.File) == false)
                {
                    _logger.Warn($"'{_fluentCommandLineParser.Object.File}' does not exist! Exiting");
                    return;
                }

                ProcessFile(Path.GetFullPath(_fluentCommandLineParser.Object.File));
            }
            else
            {
                //Directories
                _logger.Info($"Looking for files in '{_fluentCommandLineParser.Object.Directory}'");
                _logger.Info("");

                Privilege[] privs = { Privilege.EnableDelegation, Privilege.Impersonate, Privilege.Tcb };
                using (new PrivilegeEnabler(Privilege.Backup, privs))
                {
                    var dirEnumOptions =
                        DirectoryEnumerationOptions.Files | DirectoryEnumerationOptions.Recursive |
                        DirectoryEnumerationOptions.SkipReparsePoints | DirectoryEnumerationOptions.ContinueOnException |
                        DirectoryEnumerationOptions.BasicSearch;

                    var f = new DirectoryEnumerationFilters
                    {
                        RecursionFilter = entryInfo => !entryInfo.IsMountPoint && !entryInfo.IsSymbolicLink,
                        ErrorFilter     = (errorCode, errorMessage, pathProcessed) => true
                    };

                    var dbNames = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

                    if (_fluentCommandLineParser.Object.Hunt)
                    {
                        f.InclusionFilter = fsei => true;
                    }
                    else
                    {
                        foreach (var mapFile in SQLMap.MapFiles)
                        {
                            dbNames.Add(mapFile.Value.FileName);
                        }

                        f.InclusionFilter = fsei => dbNames.Contains(fsei.FileName);
                    }

                    var files2 =
                        Directory.EnumerateFileSystemEntries(Path.GetFullPath(_fluentCommandLineParser.Object.Directory), dirEnumOptions, f);

                    foreach (var file in files2)
                    {
                        try
                        {
                            ProcessFile(file);
                        }
                        catch (Exception e)
                        {
                            _logger.Error($"Error processing '{file}': {e.Message}");
                        }
                    }
                }
            }

            sw.Stop();

            if (UnmatchedDbs.Any())
            {
                Console.WriteLine();
                _logger.Fatal($"At least one database was found with no corresponding map (Use --debug for more details about discovery process)");

                foreach (var unmatchedDb in UnmatchedDbs)
                {
                    DumpUnmatched(unmatchedDb);
                }
            }

            var extra = string.Empty;

            if (ProcessedFiles.Count > 1)
            {
                extra = "s";
            }

            _logger.Info($"\r\nProcessed {ProcessedFiles.Count:N0} file{extra} in {sw.Elapsed.TotalSeconds:N4} seconds\r\n");

            if (!File.Exists("SQLite.Interop.dll"))
            {
                return;
            }

            try
            {
                File.Delete("SQLite.Interop.dll");
            }
            catch (Exception)
            {
                _logger.Warn("Unable to delete 'SQLite.Interop.dll'. Delete manually if needed.\r\n");
            }
        }
Exemple #52
0
        public static void Main(string[] args)
        {
            var p = new FluentCommandLineParser <ApplicationArguments>();

            p.Setup(arg => arg.InputFolder)
            .As('i', "InputFolder")
            .Required();

            p.Setup(arg => arg.OutputFolder)
            .As('o', "OutputFolder")
            .Required();

            var result = p.Parse(args);

            if (!result.HasErrors)
            {
                // delete old output if it exists
                if (Directory.Exists(p.Object.OutputFolder))
                {
                    var dir = new DirectoryInfo(p.Object.OutputFolder);
                    dir.Delete(true);
                }

                // get results from last 5 runs
                int historyCount = 5;
                MetaMorpheusRunResultsDirectories[] runResults = new MetaMorpheusRunResultsDirectories[historyCount];

                // get last regular run result (first thing to run)
                List <DirectoryInfo> regularRunDirectories = new DirectoryInfo(p.Object.InputFolder).GetDirectories()
                                                             .Where(v => v.Name.Contains(ClassicSearchLabel))
                                                             .OrderByDescending(v => v.CreationTime)
                                                             .Take(historyCount)
                                                             .OrderBy(v => v.CreationTime).ToList();

                for (int i = 0; i < regularRunDirectories.Count; i++)
                {
                    DirectoryInfo regularRunDir = regularRunDirectories[i];

                    Dictionary <string, DirectoryInfo> directoryInfos = new Dictionary <string, DirectoryInfo> {
                        { ClassicSearchLabel, regularRunDir }
                    };

                    var    ok        = regularRunDir.Name.Split(new char[] { '[' });
                    string timestamp = regularRunDir.Name.Split(new char[] { '[' })[1].TrimEnd(new char[] { ']' });

                    var otherLabels = labels.Where(l => l != ClassicSearchLabel).ToList();

                    foreach (string label in otherLabels)
                    {
                        DirectoryInfo otherRunDirectory = new DirectoryInfo(p.Object.InputFolder)
                                                          .GetDirectories()
                                                          .Where(v => v.Name.Contains(label) && v.Name.Contains(timestamp))
                                                          .OrderByDescending(v => v.CreationTime)
                                                          .Take(historyCount)
                                                          .OrderBy(v => v.CreationTime)
                                                          .FirstOrDefault();

                        directoryInfos.Add(label, otherRunDirectory);
                    }

                    runResults[i] = new MetaMorpheusRunResultsDirectories(directoryInfos);
                }

                // set up output file
                List <string> output = new List <string> {
                    MetaMorpheusRunResult.CommaSeparatedHeader()
                };

                // add results from each run
                runResults.Where(v => v != null).ForEach(v => output.Add(v.ParsedRunResult.ToString()));

                // write results
                // create output directory
                if (!Directory.Exists(p.Object.OutputFolder))
                {
                    Directory.CreateDirectory(p.Object.OutputFolder);
                }

                File.WriteAllLines(Path.Combine(p.Object.OutputFolder, "ProcessedResults.csv"), output);

                // delete old search results (keeps 1 result for every 7 days, except it keeps all of the last 5 days)
                List <DirectoryInfo> directoriesToPotentiallyDelete = new DirectoryInfo(p.Object.InputFolder)
                                                                      .GetDirectories()
                                                                      .OrderByDescending(v => v.CreationTime).ToList();

                DateTime dateToStartDeletingAt = new DateTime(2018, 12, 9);

                directoriesToPotentiallyDelete = directoriesToPotentiallyDelete
                                                 .Take(directoriesToPotentiallyDelete.Count)
                                                 .Where(v => v.CreationTime.Date.CompareTo(dateToStartDeletingAt) > 0)
                                                 .ToList();

                var datesToKeep = new List <DateTime>();

                int weeks = 0;
                while (!datesToKeep.Any() || datesToKeep.Last().CompareTo(DateTime.Now) < 0)
                {
                    datesToKeep.Add(dateToStartDeletingAt.Date.AddDays(weeks * 7));
                    weeks++;
                }

                for (int d = 0; d < historyCount; d++)
                {
                    datesToKeep.Add(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day - d).Date);
                }

                var directoriesToDelete = directoriesToPotentiallyDelete.Where(v => !datesToKeep.Contains(v.CreationTime.Date)).ToList();

                foreach (DirectoryInfo directory in directoriesToDelete)
                {
                    Directory.Delete(directory.FullName, true);
                }
            }
        }
Exemple #53
0
        static void Main(string[] args)
        {
            var parser = new FluentCommandLineParser <ApplicationArguments>();

            parser.Setup(arg => arg.IP).As('i', "ip").SetDefault("127.0.0.1").WithDescription("(optional) IP address of the MySQL server, will use 127.0.0.1 if not specified");
            parser.Setup(arg => arg.Port).As('n', "port").SetDefault(3306).WithDescription("(optional) Port number of the MySQL server, will use 3306 if not specified");
            parser.Setup(arg => arg.User).As('u', "user").SetDefault("root").WithDescription("(optional) Username, will use root if not specified");
            parser.Setup(arg => arg.Password).As('p', "password").SetDefault(String.Empty).WithDescription("(optional) Password, will use empty password if not specified");
            parser.Setup(arg => arg.Database).As('d', "database").Required().WithDescription("Database name");
            parser.Setup(arg => arg.Table).As('t', "table").SetDefault(String.Empty).WithDescription("(optional) Table name, will generate entire database if not specified");
            parser.Setup(arg => arg.GenerateConstructorAndOutput).As('g', "generateconstructorandoutput")
            .SetDefault(false).WithDescription("(optional) Generate a reading constructor and SQL statement output - Activate with -g true");
            parser.Setup(arg => arg.GenerateMarkupPages).As('m', "generatemarkuppages")
            .SetDefault(false)
            .WithDescription("(optional) Generate markup pages for database and tables which can be used in wikis - Activate with -m true");
            parser.Setup(arg => arg.MarkupDatabaseNameReplacement).As('r', "markupdatabasenamereplacement")
            .SetDefault("").WithDescription("(optional) Will use this instead of database name for wiki breadcrump generation");
            parser.SetupHelp("?", "help").Callback(text => Console.WriteLine(text));

            var result = parser.Parse(args);

            if (!result.HasErrors)
            {
                var conf = parser.Object as ApplicationArguments;
                if (conf.Database is null)
                {
                    Console.WriteLine("You didn't specify a database");
                    return;
                }

                var confString =
                    $"Server={conf.IP};Port={conf.Port};Uid={conf.User};Pwd={conf.Password};Database={conf.Database}";
                Console.WriteLine(confString);

                var database = new Dictionary <string, List <Column> >();

                using (var con = new MySqlConnection(confString))
                {
                    con.Open();

                    using (var cmd = con.CreateCommand())
                    {
                        cmd.CommandText =
                            $"SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{conf.Database}'";
                        if (!conf.Table.Equals(string.Empty))
                        {
                            cmd.CommandText += $" AND TABLE_NAME = '{conf.Table}'";
                        }

                        var reader = cmd.ExecuteReader();
                        if (!reader.HasRows)
                        {
                            return;
                        }

                        while (reader.Read())
                        {
                            if (database.ContainsKey(reader.GetString(0)))
                            {
                                database[reader.GetString(0)].Add(new Column(reader));
                            }
                            else
                            {
                                database.Add(reader.GetString(0), new List <Column>()
                                {
                                    new Column(reader)
                                });
                            }
                        }
                    }

                    foreach (var table in database)
                    {
                        using (var cmd = con.CreateCommand())
                        {
                            // lul - is there a way to do this without this senseless statement?
                            cmd.CommandText = $"SELECT * FROM `{table.Key}` LIMIT 0";
                            var reader = cmd.ExecuteReader();
                            var schema = reader.GetSchemaTable();
                            foreach (var column in table.Value)
                            {
                                column.Type = schema.Select($"ColumnName = '{column.Name}'")[0]["DataType"] as Type;
                            }
                        }
                    }

                    con.Close();
                }

                DbToClasses(conf.Database, database, conf.GenerateConstructorAndOutput);
                if (conf.GenerateMarkupPages)
                {
                    DbToMarkupPage(String.IsNullOrEmpty(conf.MarkupDatabaseNameReplacement) ? conf.Database : conf.MarkupDatabaseNameReplacement, database);
                }
                Console.WriteLine("Successfully generated C# classes!");
            }
            Console.ReadLine();
        }
 public BaseAction()
 {
     Parser = new FluentCommandLineParser();
 }
 public ApplicationArgumentsParser()
 {
     _operations = new Dictionary<string, IOperation>();
     _parser = new FluentCommandLineParser<ApplicationArguments>();
     Setup();
 }
Exemple #56
0
        static int Main(string[] args)
        {
            FluentCommandLineParser <Options> p = new FluentCommandLineParser <Options>();

            p.Setup(o => o.Version).As('v', "version");
            p.Setup(o => o.NuSpec).As('t', "nuspec");
            p.Setup(o => o.NugetExe).As('n', "nuget");
            p.Setup(o => o.SquirrelCom).As('s', "squirrel");
            p.Setup(o => o.ReleaseDir).As('r', "releasedir");
            p.Setup(o => o.LocalRepo).As("localrepo");

            p.Setup(o => o.SquirrelPackage).As('p', "package");

            p.SetupHelp("?", "help").Callback(text => Console.WriteLine(text));

            ICommandLineParserResult res = p.Parse(args);

            if (res.HasErrors)
            {
                Console.WriteLine(res.ErrorText);
                p.HelpOption.ShowHelp(p.Options);
                return(1);
            }

            Options options = p.Object;

            if (options.SquirrelPackage != null)
            {
                if (!File.Exists(options.SquirrelPackage))
                {
                    Console.WriteLine("Squirrel Package file '{0}' does not exist");
                    return(1);
                }
                JsonConvert.PopulateObject(File.ReadAllText(options.SquirrelPackage), options);
                Environment.CurrentDirectory = Path.GetDirectoryName(options.SquirrelPackage);
            }

            //File.WriteAllText("squirrel.json", JsonConvert.SerializeObject(p.Object, Formatting.Indented));
            if (!File.Exists(options.NuSpec))
            {
                Console.WriteLine("{0} does not exist", options.NuSpec);
                return(1);
            }
            if (options.NugetExe == null)
            {
                options.NugetExe = FindFile(options.NuSpec, ".nuget\\NuGet.exe");
            }
            if (options.SquirrelCom == null)
            {
                options.SquirrelCom = FindFile(options.NuSpec, "squirrel.com");
            }
            if (options.ReleaseDir == null)
            {
                options.ReleaseDir = Path.GetFullPath(".");
            }
            if (!File.Exists(options.NugetExe))
            {
                Console.WriteLine("NuGet.exe not found");
                return(1);
            }

            Manifest m;

            using (FileStream fileStream = File.OpenRead(options.NuSpec))
                m = Manifest.ReadFrom(fileStream, false);
            Version newVersion = SetPackageVersion(options, m);

            WriteJekyllRelease(options, newVersion);
            string nupkg   = CreatePackage(options, m);
            string sqpkg   = CreateSquirrelRelease(options, nupkg);
            object pushres = PushReleasesToGithub(options, newVersion);

            return(0);
        }
Exemple #57
0
        private static bool ParseCommandLineParameters(string[] args)
        {
            bool portAlreadySet      = false;
            bool verbosityAlreadySet = false;
            bool interruptExecution  = false;

            var parser = new FluentCommandLineParser();

            parser.IsCaseSensitive = false;

            parser.Setup <string>('h', "host")
            .Callback(item => { requestConfig.Host = item; })
            .Required()
            .WithDescription("Remote HTTP server");

            parser.Setup <string>('p', "path")
            .Callback(item => { requestConfig.Path = item; })
            .SetDefault("/")
            .WithDescription("Remote path");

            parser.Setup <bool>('v')
            .Callback(verboseFlagSet => {
                if (verboseFlagSet == true)
                {
                    requestConfig.Verbose = verboseFlagSet;
                    verbosityAlreadySet   = true;
                }
            })
            .SetDefault(false)
            .WithDescription("Make output verbose");

            parser.Setup <bool>("vv")
            .Callback(verboseFlagSet => {
                if (verboseFlagSet == true)
                {
                    requestConfig.Verbose        = verboseFlagSet;
                    requestConfig.VerboseHeaders = verboseFlagSet;
                    verbosityAlreadySet          = true;
                }
            })
            .SetDefault(false)
            .WithDescription("Make output verbose and print server response headers");

            parser.Setup <bool>("vvv")
            .Callback(verboseFlagSet => {
                if (verboseFlagSet == true)
                {
                    requestConfig.Verbose        = verboseFlagSet;
                    requestConfig.VerboseHeaders = verboseFlagSet;
                    requestConfig.VerbosePayload = verboseFlagSet;
                    verbosityAlreadySet          = true;
                }
            })
            .SetDefault(false)
            .WithDescription("Make output verbose and print server response headers plus server response content data");

            parser.Setup <string>('w', "write")
            .Callback(dumpDataToFlagSet => { requestConfig.ContentDataOutputFile = dumpDataToFlagSet; })
            .SetDefault(string.Empty)
            .WithDescription("Write conten data to FILE_NAME parameter");

            parser.Setup <bool>('s', "ssl")
            .Callback(sslFlagSet => {
                requestConfig.UseSsl = sslFlagSet;
                if (!portAlreadySet)
                {
                    requestConfig.Port = requestConfig.UseSsl ? 443 : 80;
                }
            })
            .SetDefault(false)
            .WithDescription("Use HTTPS instead HTTP to connect to the remote server");

            parser.Setup <int>("port")
            .Callback(item => { requestConfig.Port = item; portAlreadySet = true; })
            .WithDescription("Remote port");

            parser.Setup <List <string> >("headers")
            .Callback(item => requestConfig.CustomRequestHeaders = item)
            .WithDescription("Custom HTTP request headers separated by a comma: Host: www.test.com, User-Agent: Minary");


            parser.SetupHelp("?", "help")
            .Callback(text => {
                Console.WriteLine(text);
                interruptExecution = true;
            });

            ICommandLineParserResult result = parser.Parse(args);

            if (result.HasErrors == true)
            {
                Console.WriteLine("{0}\r\n\r\n", result.ErrorText);
                interruptExecution = true;
            }

            return(interruptExecution);
        }
Exemple #58
-1
		/// <summary>
		/// Creates the command line parser.
		/// </summary>
		/// <returns>The command line parser.</returns>
		private static FluentCommandLineParser<ProgramArguments> CreateParser()
		{
			#region Contract
			Contract.Ensures(Contract.Result<FluentCommandLineParser<ProgramArguments>>() != null);
			#endregion
			var pars = new FluentCommandLineParser<ProgramArguments>();
			pars.IsCaseSensitive = true;
			
			// Help
			pars.SetupHelp("?", "help", "version")
				.Callback(text => ProgramArguments.PrintHelp(text));
			// Parse table path (required)
			pars.Setup(arg => arg.ParseTablePath)
				.As('p')
				.WithDescription("Path of the parse table to use.")
				.Required();
			// Input path (default: stdin)
			pars.Setup(arg => arg.InputPath)
				.As('i')
				.WithDescription("Path of the input file to use. Default: stdin.");
			// Input path (default: stdin)
			pars.Setup(arg => arg.OutputPath)
				.As('o')
				.WithDescription("Path of the output file to use. Default: stdin.");
			
			return pars;
		}
Exemple #59
-1
        static void Main(string[] args)
        {
            _log.Info("Program started...");
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_OnUnhandledException;
            AppDomain.CurrentDomain.FirstChanceException += CurrentDomain_OnFirstChanceException;

            if (args?.Length > 0)
            {
                //System.Threading.Thread.Sleep(10 * 1200);

                var macros = new Dictionary<string, Func<string, string, object>>();
                macros["now"]           = (macro, format) => DateTime.UtcNow.ToString(format);
                macros["now-utc"]       = (macro, format) => DateTime.UtcNow.ToString(format);
                macros["now-local"]     = (macro, format) => DateTime.Now.ToString(format);
                var macroResolver = new MacroResolver();

                var parts = args.ToArray();
                var switchIndex = parts.ToList().FindIndex(x => x.StartsWith("/"));
                if (switchIndex < 0)
                    switchIndex = parts.Length;
                var verb = switchIndex > 0
                    ? String.Join(" ", parts.Take(switchIndex))
                    : parts.Length > 0 ? parts[0] : null;
                parts = switchIndex > 0
                    ? parts.Skip(switchIndex).ToArray()
                    : parts.Length > 1 ? parts.Skip(1).ToArray() : parts;

                if (verb == "fpl2016-dl")
                {
                    var parser = new FluentCommandLineParser<DownloadJsonArguments>();
                    parser.Setup(x => x.Uri)
                        .As("uri")
                        .Required()
                        .WithDescription("The uri to download from");
                    parser.Setup(x => x.FileName)
                        .As('f', "file")
                        .Required()
                        .WithDescription("The target filename");
                    parser.Setup(x => x.Username)
                        .As("user")
                        .WithDescription("The auth user");
                    parser.Setup(x => x.Password)
                        .As("pass")
                        .WithDescription("The auth password");

                    var r = parser.Parse(parts);
                    if (r.HasErrors)
                    {
                        Console.WriteLine("Error: " + r.ErrorText);
                    }
                    else if (r.HelpCalled)
                    {
                        Console.WriteLine($"Help for {verb} is not implemented...");
                    }
                    else
                    {
                        var arguments = parser.Object;

                        var fileName = arguments.FileName;
                        fileName = fileName.StartsWith("\"") && fileName.EndsWith("\"")
                            ? fileName.Substring(1, fileName.Length - 2)
                            : fileName;
                        fileName = macroResolver.Resolve(fileName, macros);

                        try
                        {
                            var dataFactory = new FantasyPremierLeague2016DataFactory();
                            dataFactory.Username = arguments.Username;
                            dataFactory.Password = arguments.Password;

                            var data = dataFactory.GetJTokenFromAPI(arguments.Uri).WaitForResult();
                            var saved = dataFactory.SaveJTokenToTextFile(data, fileName).WaitForResult();
                        }
                        catch (AggregateException ex)
                        {
                            Console.WriteLine("Error saving data. Error: " + ex?.InnerException?.GetBaseException()?.Message);
                            Console.WriteLine(ex);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error saving data. Error: " + ex?.GetBaseException()?.Message);
                            Console.WriteLine(ex);
                        }
                    }
                }
            }
            else
            {
                RunSimulator(args);

            #if DEBUG
                if (Environment.UserInteractive)
                    Console.ReadLine();
                _log.Info("Program exited...");
            #endif
            }
        }
Exemple #60
-1
        private static void Main(string[] args)
        {
            Licensing.RegisterLicenseFromFileIfExists(SSLicenseFile);

            LoadMACs();

            SetupNLog();

            _logger = LogManager.GetCurrentClassLogger();

            if (!CheckForDotnet46())
            {
                _logger.Warn(".net 4.6 not detected. Please install .net 4.6 and try again.");
                return;
            }

            _fluentCommandLineParser = new FluentCommandLineParser<ApplicationArguments>
            {
                IsCaseSensitive = false
            };

            _fluentCommandLineParser.Setup(arg => arg.File)
                .As('f')
                .WithDescription("File to process. Either this or -d is required");

            _fluentCommandLineParser.Setup(arg => arg.Directory)
                .As('d')
                .WithDescription("Directory to recursively process. Either this or -f is required");

            _fluentCommandLineParser.Setup(arg => arg.AllFiles)
                .As("all")
                .WithDescription(
                    "Process all files in directory vs. only files matching *.lnk\r\n").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.CsvDirectory)
                .As("csv")
                .WithDescription(
                    "Directory to save CSV (tab separated) formatted results to. Be sure to include the full path in double quotes");

            _fluentCommandLineParser.Setup(arg => arg.XmlDirectory)
                .As("xml")
                .WithDescription(
                    "Directory to save XML formatted results to. Be sure to include the full path in double quotes");

            _fluentCommandLineParser.Setup(arg => arg.xHtmlDirectory)
                .As("html")
                .WithDescription(
                    "Directory to save xhtml formatted results to. Be sure to include the full path in double quotes");

            _fluentCommandLineParser.Setup(arg => arg.JsonDirectory)
                .As("json")
                .WithDescription(
                    "Directory to save json representation to. Use --pretty for a more human readable layout");

            _fluentCommandLineParser.Setup(arg => arg.JsonPretty)
                .As("pretty")
                .WithDescription(
                    "When exporting to json, use a more human readable layout\r\n").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.Quiet)
                .As('q')
                .WithDescription(
                    "Only show the filename being processed vs all output. Useful to speed up exporting to json and/or csv\r\n")
                .SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.NoTargetIDList)
                .As("nid")
                .WithDescription(
                    "Suppress Target ID list details from being displayed. Default is false.").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.NoExtraBlocks)
                .As("neb")
                .WithDescription(
                    "Suppress Extra blocks information from being displayed. Default is false.\r\n").SetDefault(false);

            _fluentCommandLineParser.Setup(arg => arg.DateTimeFormat)
            .As("dt")
            .WithDescription(
            "The custom date/time format to use when displaying time stamps. Default is: yyyy-MM-dd HH:mm:ss K").SetDefault("yyyy-MM-dd HH:mm:ss K");

            _fluentCommandLineParser.Setup(arg => arg.PreciseTimestamps)
               .As("mp")
               .WithDescription(
               "Display higher precision for time stamps. Default is false").SetDefault(false);

            var header =
                $"LECmd version {Assembly.GetExecutingAssembly().GetName().Version}" +
                "\r\n\r\nAuthor: Eric Zimmerman ([email protected])" +
                "\r\nhttps://github.com/EricZimmerman/LECmd";

            var footer = @"Examples: LECmd.exe -f ""C:\Temp\foobar.lnk""" + "\r\n\t " +
                         @" LECmd.exe -f ""C:\Temp\somelink.lnk"" --json ""D:\jsonOutput"" --jsonpretty" + "\r\n\t " +
                         @" LECmd.exe -d ""C:\Temp"" --csv ""c:\temp"" --html c:\temp --xml c:\temp\xml -q" + "\r\n\t " +
                         @" LECmd.exe -f ""C:\Temp\some other link.lnk"" --nid --neb " + "\r\n\t " +
                         @" LECmd.exe -d ""C:\Temp"" --all" + "\r\n\t" +
                         "\r\n\t"+
                         "  Short options (single letter) are prefixed with a single dash. Long commands are prefixed with two dashes\r\n";

            _fluentCommandLineParser.SetupHelp("?", "help")
                .WithHeader(header)
                .Callback(text => _logger.Info(text + "\r\n" + footer));

            var result = _fluentCommandLineParser.Parse(args);

            if (result.HelpCalled)
            {
                return;
            }

            if (result.HasErrors)
            {
                _logger.Error("");
                _logger.Error(result.ErrorText);

                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                return;
            }

            if (UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.File) &&
                UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.Directory))
            {
                _fluentCommandLineParser.HelpOption.ShowHelp(_fluentCommandLineParser.Options);

                _logger.Warn("Either -f or -d is required. Exiting");
                return;
            }

            if (UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.File) == false &&
                !File.Exists(_fluentCommandLineParser.Object.File))
            {
                _logger.Warn($"File '{_fluentCommandLineParser.Object.File}' not found. Exiting");
                return;
            }

            if (UsefulExtension.IsNullOrEmpty(_fluentCommandLineParser.Object.Directory) == false &&
                !Directory.Exists(_fluentCommandLineParser.Object.Directory))
            {
                _logger.Warn($"Directory '{_fluentCommandLineParser.Object.Directory}' not found. Exiting");
                return;
            }

            _logger.Info(header);
            _logger.Info("");
            _logger.Info($"Command line: {string.Join(" ", Environment.GetCommandLineArgs().Skip(1))}\r\n");

            if (_fluentCommandLineParser.Object.PreciseTimestamps)
            {
                _fluentCommandLineParser.Object.DateTimeFormat = _preciseTimeFormat;
            }

            _processedFiles = new List<LnkFile>();

            _failedFiles = new List<string>();

            if (_fluentCommandLineParser.Object.File?.Length > 0)
            {
                LnkFile lnk = null;

                try
                {
                    lnk = ProcessFile(_fluentCommandLineParser.Object.File);
                    if (lnk != null)
                    {
                        _processedFiles.Add(lnk);
                    }
                }
                catch (UnauthorizedAccessException ua)
                {
                    _logger.Error(
                        $"Unable to access '{_fluentCommandLineParser.Object.File}'. Are you running as an administrator? Error: {ua.Message}");
                    return;
                }
                catch (Exception ex)
                {
                    _logger.Error(
                        $"Error processing file '{_fluentCommandLineParser.Object.Directory}' Please send it to [email protected]. Error: {ex.Message}");
                    return;
                }
            }
            else
            {
                _logger.Info($"Looking for lnk files in '{_fluentCommandLineParser.Object.Directory}'");
                _logger.Info("");

                string[] lnkFiles = null;

                try
                {
                    var mask = "*.lnk";
                    if (_fluentCommandLineParser.Object.AllFiles)
                    {
                        mask = "*";
                    }

                    lnkFiles = Directory.GetFiles(_fluentCommandLineParser.Object.Directory, mask,
                        SearchOption.AllDirectories);
                }
                catch (UnauthorizedAccessException ua)
                {
                    _logger.Error(
                        $"Unable to access '{_fluentCommandLineParser.Object.Directory}'. Error message: {ua.Message}");
                    return;
                }
                catch (Exception ex)
                {
                    _logger.Error(
                        $"Error getting lnk files in '{_fluentCommandLineParser.Object.Directory}'. Error: {ex.Message}");
                    return;
                }

                _logger.Info($"Found {lnkFiles.Length:N0} files");
                _logger.Info("");

                var sw = new Stopwatch();
                sw.Start();

                foreach (var file in lnkFiles)
                {
                    var lnk = ProcessFile(file);
                    if (lnk != null)
                    {
                        _processedFiles.Add(lnk);
                    }
                }

                sw.Stop();

                if (_fluentCommandLineParser.Object.Quiet)
                {
                    _logger.Info("");
                }

                _logger.Info(
                    $"Processed {lnkFiles.Length - _failedFiles.Count:N0} out of {lnkFiles.Length:N0} files in {sw.Elapsed.TotalSeconds:N4} seconds");
                if (_failedFiles.Count > 0)
                {
                    _logger.Info("");
                    _logger.Warn("Failed files");
                    foreach (var failedFile in _failedFiles)
                    {
                        _logger.Info($"  {failedFile}");
                    }
                }
            }

            if (_processedFiles.Count > 0)
            {
                _logger.Info("");

                try
                {
                    CsvWriter csv = null;
                    StreamWriter sw = null;

                    if (_fluentCommandLineParser.Object.CsvDirectory?.Length > 0)
                    {
                        if (Directory.Exists(_fluentCommandLineParser.Object.CsvDirectory) == false)
                        {
                            _logger.Warn($"'{_fluentCommandLineParser.Object.CsvDirectory} does not exist. Creating...'");
                            Directory.CreateDirectory(_fluentCommandLineParser.Object.CsvDirectory);
                        }

                        var outName = $"{DateTimeOffset.Now.ToString("yyyyMMddHHmmss")}_LECmd_Output.tsv";
                        var outFile = Path.Combine(_fluentCommandLineParser.Object.CsvDirectory, outName);

                        _fluentCommandLineParser.Object.CsvDirectory =
                            Path.GetFullPath(outFile);
                        _logger.Warn(
                            $"CSV (tab separated) output will be saved to '{Path.GetFullPath(outFile)}'");

                        try
                        {
                            sw = new StreamWriter(outFile);
                            csv = new CsvWriter(sw);
                            csv.Configuration.Delimiter = $"{'\t'}";
                            csv.WriteHeader(typeof(CsvOut));
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(
                                $"Unable to open '{outFile}' for writing. CSV export canceled. Error: {ex.Message}");
                        }
                    }

                    if (_fluentCommandLineParser.Object.JsonDirectory?.Length > 0)
                    {
                        if (Directory.Exists(_fluentCommandLineParser.Object.JsonDirectory) == false)
                        {
                            _logger.Warn($"'{_fluentCommandLineParser.Object.JsonDirectory} does not exist. Creating...'");
                            Directory.CreateDirectory(_fluentCommandLineParser.Object.JsonDirectory);
                        }
                        _logger.Warn($"Saving json output to '{_fluentCommandLineParser.Object.JsonDirectory}'");
                    }
                    if (_fluentCommandLineParser.Object.XmlDirectory?.Length > 0)
                    {
                        {
                            if (Directory.Exists(_fluentCommandLineParser.Object.XmlDirectory) == false)
                            {
                                _logger.Warn($"'{_fluentCommandLineParser.Object.XmlDirectory} does not exist. Creating...'");
                                Directory.CreateDirectory(_fluentCommandLineParser.Object.XmlDirectory);
                            }

                        }
                        _logger.Warn($"Saving XML output to '{_fluentCommandLineParser.Object.XmlDirectory}'");
                    }

                    XmlTextWriter xml = null;

                    if (_fluentCommandLineParser.Object.xHtmlDirectory?.Length > 0)
                    {

                        var outDir = Path.Combine(_fluentCommandLineParser.Object.xHtmlDirectory,
                            $"{DateTimeOffset.UtcNow.ToString("yyyyMMddHHmmss")}_LECmd_Output_for_{_fluentCommandLineParser.Object.xHtmlDirectory.Replace(@":\", "_").Replace(@"\", "_")}");

                        if (Directory.Exists(outDir) == false)
                        {
                            Directory.CreateDirectory(outDir);
                        }

                        File.WriteAllText(Path.Combine(outDir, "normalize.css"), Resources.normalize);
                        File.WriteAllText(Path.Combine(outDir, "style.css"), Resources.style);

                        var outFile = Path.Combine(_fluentCommandLineParser.Object.xHtmlDirectory, outDir, "index.xhtml");

                        _logger.Warn($"Saving HTML output to '{outFile}'");

                        xml = new XmlTextWriter(outFile, Encoding.UTF8)
                        {
                            Formatting = Formatting.Indented,
                            Indentation = 4
                        };

                        xml.WriteStartDocument();

                        xml.WriteProcessingInstruction("xml-stylesheet", "href=\"normalize.css\"");
                        xml.WriteProcessingInstruction("xml-stylesheet", "href=\"style.css\"");

                        xml.WriteStartElement("document");
                    }

                    foreach (var processedFile in _processedFiles)
                    {
                        var o = GetCsvFormat(processedFile);

                        try
                        {
                            csv?.WriteRecord(o);
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(
                                $"Error writing record for '{processedFile.SourceFile}' to '{_fluentCommandLineParser.Object.CsvDirectory}'. Error: {ex.Message}");
                        }

                        if (_fluentCommandLineParser.Object.JsonDirectory?.Length > 0)
                        {
                            SaveJson(processedFile, _fluentCommandLineParser.Object.JsonPretty,
                                _fluentCommandLineParser.Object.JsonDirectory);
                        }

                        //XHTML
                        xml?.WriteStartElement("Container");
                        xml?.WriteElementString("SourceFile", o.SourceFile);
                        xml?.WriteElementString("SourceCreated", o.SourceCreated);
                        xml?.WriteElementString("SourceModified", o.SourceModified);
                        xml?.WriteElementString("SourceAccessed", o.SourceAccessed);

                        xml?.WriteElementString("TargetCreated", o.TargetCreated);
                        xml?.WriteElementString("TargetModified", o.TargetModified);
                        xml?.WriteElementString("TargetAccessed", o.TargetModified);

                        xml?.WriteElementString("FileSize", o.FileSize.ToString());
                        xml?.WriteElementString("RelativePath", o.RelativePath);
                        xml?.WriteElementString("WorkingDirectory", o.WorkingDirectory);
                        xml?.WriteElementString("FileAttributes", o.FileAttributes);
                        xml?.WriteElementString("HeaderFlags", o.HeaderFlags);
                        xml?.WriteElementString("DriveType", o.DriveType);
                        xml?.WriteElementString("DriveSerialNumber", o.DriveSerialNumber);
                        xml?.WriteElementString("DriveLabel", o.DriveLabel);
                        xml?.WriteElementString("LocalPath", o.LocalPath);
                        xml?.WriteElementString("CommonPath", o.CommonPath);
                        xml?.WriteElementString("Arguments", o.Arguments);

                        xml?.WriteElementString("TargetIDAbsolutePath", o.TargetIDAbsolutePath);

                        xml?.WriteElementString("TargetMFTEntryNumber", $"{o.TargetMFTEntryNumber}");
                        xml?.WriteElementString("TargetMFTSequenceNumber", $"{o.TargetMFTSequenceNumber}");

                        xml?.WriteElementString("MachineID", o.MachineID);
                        xml?.WriteElementString("MachineMACAddress", o.MachineMACAddress);
                        xml?.WriteElementString("MACVendor", o.MACVendor);

                        xml?.WriteElementString("TrackerCreatedOn", o.TrackerCreatedOn);

                        xml?.WriteElementString("ExtraBlocksPresent", o.ExtraBlocksPresent);

                        xml?.WriteEndElement();

                        if (_fluentCommandLineParser.Object.XmlDirectory?.Length > 0)
                        {
                            SaveXML(o, _fluentCommandLineParser.Object.XmlDirectory);
                        }
                    }

                    //Close CSV stuff
                    sw?.Flush();
                    sw?.Close();

                    //Close XML
                    xml?.WriteEndElement();
                    xml?.WriteEndDocument();
                    xml?.Flush();
                }
                catch (Exception ex)
                {
                    _logger.Error(
                        $"Error exporting data! Error: {ex.Message}");
                }
            }
            }