private static ASA_ERROR RunExportCollectCommand()

in Cli/AttackSurfaceAnalyzerClient.cs [444:519]


        private static ASA_ERROR RunExportCollectCommand(ExportCollectCommandOptions opts)
        {
            if (DatabaseManager is null)
            {
                Log.Error("Err_DatabaseManagerNull", "RunExportCollectCommand");
                return ASA_ERROR.DATABASE_NULL;
            }
            if (opts.OutputPath != null && !Directory.Exists(opts.OutputPath))
            {
                Log.Fatal(Strings.Get("Err_OutputPathNotExist"), opts.OutputPath);
                return 0;
            }

            if (opts.ExportSingleRun)
            {
                if (opts.SecondRunId is null)
                {
                    Log.Information("Provided null second run id using latest run.");
                    List<string> runIds = DatabaseManager.GetLatestRunIds(1, RUN_TYPE.COLLECT);
                    if (runIds.Count < 1)
                    {
                        Log.Fatal(Strings.Get("Err_CouldntDetermineOneRun"));
                        return ASA_ERROR.INVALID_ID;
                    }
                    else
                    {
                        // If you ask for single run everything is "Created"
                        opts.SecondRunId = runIds.First();
                        opts.FirstRunId = null;
                    }
                }
            }
            else if (opts.FirstRunId is null || opts.SecondRunId is null)
            {
                Log.Information("Provided null run Ids using latest two runs.");
                List<string> runIds = DatabaseManager.GetLatestRunIds(2, RUN_TYPE.COLLECT);

                if (runIds.Count < 2)
                {
                    Log.Fatal(Strings.Get("Err_CouldntDetermineTwoRun"));
                    System.Environment.Exit(-1);
                }
                else
                {
                    opts.SecondRunId = runIds.First();
                    opts.FirstRunId = runIds.ElementAt(1);
                }
            }

            var ruleFile = LoadRulesFromFileOrEmbedded(opts.AnalysesFile);
            if (!ruleFile.Rules.Any())
            {
                Log.Warning(Strings.Get("Err_NoRules"));
                return ASA_ERROR.INVALID_RULES;
            }

            Log.Information(Strings.Get("Comparing"), opts.FirstRunId, opts.SecondRunId);

            CompareCommandOptions options = new CompareCommandOptions(opts.FirstRunId, opts.SecondRunId)
            {
                DatabaseFilename = opts.DatabaseFilename,
                AnalysesFile = ruleFile,
                DisableAnalysis = opts.DisableAnalysis,
                SaveToDatabase = opts.SaveToDatabase,
                RunScripts = opts.RunScripts
            };

            var results = CompareRuns(options);
            var analysesHash = options.AnalysesFile.GetHash();
            if (opts.SaveToDatabase)
            {
                InsertCompareResults(results, opts.FirstRunId, opts.SecondRunId, analysesHash);
            }

            return ExportCompareResults(results, opts, AsaHelpers.MakeValidFileName(opts.FirstRunId + "_vs_" + opts.SecondRunId), analysesHash, ruleFile.Rules);
        }