private void ConfigRules() { _logger.LogTrace("ExportTagsCommand::ConfigRules"); _rules = new RuleSet(_loggerFactory); if (!_options.IgnoreDefaultRules) { _rules = RuleSetUtils.GetDefaultRuleSet(_loggerFactory); } if (!string.IsNullOrEmpty(_options?.CustomRulesPath)) { if (Directory.Exists(_options.CustomRulesPath)) { _rules.AddDirectory(_options.CustomRulesPath); } else if (File.Exists(_options.CustomRulesPath)) { _rules.AddFile(_options.CustomRulesPath); } else { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_INVALID_RULE_PATH, _options.CustomRulesPath)); } } //error check based on ruleset not path enumeration if (_rules == null || !_rules.Any()) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } }
/// <summary> /// Intentional as no identified value in calling from DLL at this time /// </summary> /// <returns></returns> public PackRulesResult GetResult() { WriteOnce.SafeLog("PackRules::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Pack Rules")); PackRulesResult packRulesResult = new() { AppVersion = Common.Utils.GetVersionString() }; try { RulesVerifier verifier = new(_rules_path, _options?.Log); if (_options?.PackEmbeddedRules ?? false) { verifier.LoadRuleSet(RuleSetUtils.GetDefaultRuleSet()); } verifier.Verify(); if (!verifier.IsVerified) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_RESULTS_FAIL)); } packRulesResult.Rules = verifier.CompiledRuleset?.GetAppInspectorRules().ToList() ?? new List <Rule>(); packRulesResult.ResultCode = PackRulesResult.ExitCode.Success; } catch (OpException e) { WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(packRulesResult); } }
public void ConfigureRules() { WriteOnce.SafeLog("TagTestCommand::ConfigRules", LogLevel.Trace); if (string.IsNullOrEmpty(_options.CustomRulesPath)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } RulesVerifier verifier = new RulesVerifier(_options.CustomRulesPath, _options.Log); verifier.Verify(); if (!verifier.IsVerified) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_RESULTS_FAIL)); } _rulesSet = verifier.CompiledRuleset ?? new RuleSet(null); //error check based on ruleset not path enumeration if (!_rulesSet.Any()) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } }
public void ConfigureRules() { WriteOnce.SafeLog("TagTestCommand::ConfigRules", LogLevel.Trace); if (string.IsNullOrEmpty(_options.CustomRulesPath)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } try { RulesVerifier verifier = new RulesVerifier(_options.CustomRulesPath, _options.Log); verifier.Verify(); _rulesSet = verifier.CompiledRuleset; } catch (Exception e) { WriteOnce.SafeLog(e.Message + "\n" + e.StackTrace, NLog.LogLevel.Error); throw new OpException(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULE_FAILED, _options.CustomRulesPath)); } //error check based on ruleset not path enumeration if (_rulesSet.Count() == 0) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } }
/// <summary> /// Option for DLL use as alternate to Run which only outputs a file to return results as string /// CommandOption defaults will not have been set when used as DLL via CLI processing so some checks added /// </summary> /// <returns>output results</returns> public VerifyRulesResult GetResult() { _logger.LogTrace("VerifyRulesCommand::Run"); _logger.LogInformation(MsgHelp.GetString(MsgHelp.ID.CMD_RUNNING), "Verify Rules"); VerifyRulesResult verifyRulesResult = new() { AppVersion = Utils.GetVersionString() }; try { var analyzer = new ApplicationInspectorAnalyzer(); RulesVerifierOptions options = new() { Analyzer = analyzer, FailFast = false, LoggerFactory = _loggerFactory, LanguageSpecs = Languages.FromConfigurationFiles(_loggerFactory, _options.CustomCommentsPath, _options.CustomLanguagesPath) }; RulesVerifier verifier = new(options); verifyRulesResult.ResultCode = VerifyRulesResult.ExitCode.Verified; var stati = new List <RuleStatus>(); RuleSet?ruleSet = new(_loggerFactory); if (_options.VerifyDefaultRules) { ruleSet = RuleSetUtils.GetDefaultRuleSet(_loggerFactory); } try { if (_options.CustomRulesPath != null) { if (Directory.Exists(_options.CustomRulesPath)) { ruleSet.AddDirectory(_options.CustomRulesPath); } else if (File.Exists(_options.CustomRulesPath)) { ruleSet.AddFile(_options.CustomRulesPath); } } } catch (JsonException e) { _logger.LogError(e.Message); verifyRulesResult.ResultCode = VerifyRulesResult.ExitCode.CriticalError; return(verifyRulesResult); } var verifyResult = verifier.Verify(ruleSet); verifyRulesResult.RuleStatusList = verifyResult.RuleStatuses; verifyRulesResult.ResultCode = verifyResult.Verified ? VerifyRulesResult.ExitCode.Verified : VerifyRulesResult.ExitCode.NotVerified; } catch (OpException e) { _logger.LogTrace(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(verifyRulesResult); } }
private void ConfigRules() { _logger.LogTrace("PackRulesCommand::ConfigRules"); if (string.IsNullOrEmpty(_options.CustomRulesPath) && !_options.PackEmbeddedRules) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } }
private void ConfigRules() { _logger.LogTrace("VerifyRulesCommand::ConfigRules"); if (!_options.VerifyDefaultRules && string.IsNullOrEmpty(_options.CustomRulesPath)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } }
private void ConfigSourceToScan() { _logger.LogTrace("TagDiff::ConfigRules"); if ((!_options?.SourcePath1.Any() ?? true) || (!_options?.SourcePath2.Any() ?? true)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_INVALID_ARG_VALUE)); } }
private void ConfigSourceToScan() { WriteOnce.SafeLog("TagDiff::ConfigRules", LogLevel.Trace); if ((!_options?.SourcePath1.Any() ?? true) || (!_options?.SourcePath2.Any() ?? true)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_INVALID_ARG_VALUE)); } }
private void ConfigRules() { WriteOnce.SafeLog("VerifyRulesCommand::ConfigRules", LogLevel.Trace); if (!_options.VerifyDefaultRules && string.IsNullOrEmpty(_options.CustomRulesPath)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } _rules_path = _options.VerifyDefaultRules ? null : _options.CustomRulesPath; }
public static void OpenBrowser(string?url) { if (string.IsNullOrEmpty(url)) { WriteOnce.Log?.Error("Bad url for OpenBrowser method"); return; } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { try { Process.Start(new ProcessStartInfo("cmd", $"/c start {url}")); WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_FAIL));//soft error } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("BROWSER"))) { try { Process.Start("xdg-open", "\"" + url + "\""); WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_FAIL));//soft error } } else { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_ENVIRONMENT_VAR)); } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { try { Process.Start("open", "\"" + url + "\""); WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_SUCCESS)); } catch (Exception) { WriteOnce.General(MsgHelp.GetString(MsgHelp.ID.BROWSER_START_FAIL));//soft error } } }
private void ConfigSourceToScan() { WriteOnce.SafeLog("TagDiff::ConfigRules", LogLevel.Trace); if (_options.SourcePath1 == _options.SourcePath2) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.TAGDIFF_SAME_FILE_ARG)); } else if (string.IsNullOrEmpty(_options.SourcePath1) || string.IsNullOrEmpty(_options.SourcePath2)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_INVALID_ARG_VALUE)); } }
public ExportTagsResult GetResult() { _logger.LogTrace("ExportTagsCommand::Run"); _logger.LogInformation(MsgHelp.GetString(MsgHelp.ID.CMD_RUNNING), "Export Tags"); ExportTagsResult exportTagsResult = new() { AppVersion = Common.Utils.GetVersionString() }; SortedDictionary <string, string> uniqueTags = new(); try { foreach (Rule?r in _rules) { //builds a list of unique tags foreach (string t in r?.Tags ?? Array.Empty <string>()) { if (uniqueTags.ContainsKey(t)) { continue; } else { uniqueTags.Add(t, t); } } } //generate results list foreach (string s in uniqueTags.Values) { exportTagsResult.TagsList.Add(s); } exportTagsResult.ResultCode = ExportTagsResult.ExitCode.Success; } catch (OpException e) { _logger.LogError(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(exportTagsResult); } }
private void ConfigRules() { WriteOnce.SafeLog("PackRulesCommand::ConfigRules", LogLevel.Trace); if (_options.RepackDefaultRules && !Utils.CLIExecutionContext) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_NO_CLI_DEFAULT)); } if (!_options.RepackDefaultRules && string.IsNullOrEmpty(_options.CustomRulesPath)) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } _rules_path = _options.RepackDefaultRules ? Utils.GetPath(Utils.AppPath.defaultRulesSrc) : _options.CustomRulesPath; }
/// <summary> /// Intentional as no identified value in calling from DLL at this time /// </summary> /// <returns></returns> public PackRulesResult GetResult() { _logger.LogTrace("PackRulesCommand::ConfigRules"); _logger.LogInformation(MsgHelp.GetString(MsgHelp.ID.CMD_RUNNING), "Pack Rules"); PackRulesResult packRulesResult = new() { AppVersion = Common.Utils.GetVersionString() }; try { RulesVerifierOptions options = new() { FailFast = false, LoggerFactory = _loggerFactory, LanguageSpecs = Languages.FromConfigurationFiles(_loggerFactory, _options.CustomCommentsPath, _options.CustomLanguagesPath) }; RulesVerifier verifier = new(options); RuleSet? ruleSet = _options.PackEmbeddedRules ? RuleSetUtils.GetDefaultRuleSet() : new RuleSet(); if (!string.IsNullOrEmpty(_options.CustomRulesPath)) { ruleSet.AddDirectory(_options.CustomRulesPath); } RulesVerifierResult result = verifier.Verify(ruleSet); if (!result.Verified) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_RESULTS_FAIL)); } packRulesResult.Rules = result.CompiledRuleSet.GetAppInspectorRules().ToList(); packRulesResult.ResultCode = PackRulesResult.ExitCode.Success; } catch (OpException e) { _logger.LogError(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(packRulesResult); } }
public List <RuleStatus> CheckIntegrity(RuleSet ruleSet) { List <RuleStatus> ruleStatuses = new(); foreach (ConvertedOatRule rule in ruleSet.GetOatRules() ?? Array.Empty <ConvertedOatRule>()) { RuleStatus ruleVerified = CheckIntegrity(rule); ruleStatuses.Add(ruleVerified); if (_failFast && !ruleVerified.Verified) { break; } } var duplicatedRules = ruleSet.GroupBy(x => x.Id).Where(y => y.Count() > 1); if (duplicatedRules.Any()) { foreach (var rule in duplicatedRules) { _logger.LogError(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_DUPLICATEID_FAIL), rule.Key); var relevantStati = ruleStatuses.Where(x => x.RulesId == rule.Key); foreach (var status in relevantStati) { status.Errors = status.Errors.Append(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULES_DUPLICATEID_FAIL, rule.Key)); } if (_failFast) { break; } } } return(ruleStatuses); }
private void ConfigRules() { WriteOnce.SafeLog("ExportTagsCommand::ConfigRules", LogLevel.Trace); if (!_options.IgnoreDefaultRules) { _rules = Utils.GetDefaultRuleSet(_options.Log); } if (!string.IsNullOrEmpty(_options.CustomRulesPath)) { if (_rules == null) { _rules = new RuleSet(_options.Log); } if (Directory.Exists(_options.CustomRulesPath)) { _rules.AddDirectory(_options.CustomRulesPath); } else if (File.Exists(_options.CustomRulesPath)) { _rules.AddFile(_options.CustomRulesPath); } else { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_INVALID_RULE_PATH, _options.CustomRulesPath)); } } //error check based on ruleset not path enumeration if (_rules == null || _rules.Count() == 0) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_NORULES_SPECIFIED)); } }
/// <summary> /// Intentional as no identified value in calling from DLL at this time /// </summary> /// <returns></returns> public PackRulesResult GetResult() { WriteOnce.SafeLog("PackRules::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Pack Rules")); if (!Utils.CLIExecutionContext) { //requires output format and filepath only supported via CLI use WriteOnce.Error("Command not supported for DLL calls"); throw new Exception("Command not supported for DLL calls"); } PackRulesResult packRulesResult = new PackRulesResult() { AppVersion = Utils.GetVersionString() }; try { RulesVerifier verifier = new RulesVerifier(_rules_path, _options?.Log); verifier.Verify(); if (!verifier.IsVerified) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_RESULTS_FAIL)); } packRulesResult.Rules = new List <Rule>(verifier.CompiledRuleset?.AsEnumerable() ?? new RuleSet(null).AsEnumerable()); packRulesResult.ResultCode = PackRulesResult.ExitCode.Success; } catch (OpException e) { WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(packRulesResult); }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public TagDiffResult GetResult() { WriteOnce.SafeLog("TagDiffCommand::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Tag Diff")); TagDiffResult tagDiffResult = new TagDiffResult() { AppVersion = Utils.GetVersionString() }; //save to quiet analyze cmd and restore WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; try { #region setup analyze calls AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeOptions { SourcePath = _options.SourcePath1, CustomRulesPath = _options.CustomRulesPath, IgnoreDefaultRules = _options.IgnoreDefaultRules, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log }); AnalyzeCommand cmd2 = new AnalyzeCommand(new AnalyzeOptions { SourcePath = _options.SourcePath2, CustomRulesPath = _options.CustomRulesPath, IgnoreDefaultRules = _options.IgnoreDefaultRules, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log }); AnalyzeResult analyze1 = cmd1.GetResult(); AnalyzeResult analyze2 = cmd2.GetResult(); //restore WriteOnce.Verbosity = saveVerbosity; #endregion setup analyze calls bool equalTagsCompare1 = true; bool equalTagsCompare2 = true; //process results for each analyze call before comparing results if (analyze1.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR, _options.SourcePath1)); } else if (analyze2.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR, _options.SourcePath2)); } else if (analyze1.ResultCode == AnalyzeResult.ExitCode.NoMatches || analyze2.ResultCode == AnalyzeResult.ExitCode.NoMatches) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.TAGDIFF_NO_TAGS_FOUND)); } else //compare tag results; assumed (result1&2 == AnalyzeCommand.ExitCode.Success) { int count1 = 0; int sizeTags1 = analyze1.Metadata.UniqueTags.Count; string[] file1Tags = new string[sizeTags1]; foreach (string tag in analyze1.Metadata.UniqueTags.Keys.ToList <string>()) { file1Tags[count1++] = tag; } int count2 = 0; int sizeTags2 = analyze2.Metadata.UniqueTags.Count; string[] file2Tags = new string[sizeTags2]; foreach (string tag in analyze2.Metadata.UniqueTags.Keys.ToList <string>()) { file2Tags[count2++] = tag; } //can't simply compare counts as content may differ; must compare both in directions in two passes a->b; b->a equalTagsCompare1 = CompareTags(file1Tags, file2Tags, ref tagDiffResult, TagDiff.DiffSource.Source1); //reverse order for second pass equalTagsCompare2 = CompareTags(file2Tags, file1Tags, ref tagDiffResult, TagDiff.DiffSource.Source2); //final results bool resultsDiffer = !(equalTagsCompare1 && equalTagsCompare2); if (_arg_tagTestType == TagTestType.Inequality && !resultsDiffer) { tagDiffResult.ResultCode = TagDiffResult.ExitCode.TestFailed; } else if (_arg_tagTestType == TagTestType.Equality && resultsDiffer) { tagDiffResult.ResultCode = TagDiffResult.ExitCode.TestFailed; } else { tagDiffResult.ResultCode = TagDiffResult.ExitCode.TestPassed; } } } catch (OpException e) { WriteOnce.Verbosity = saveVerbosity; WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(tagDiffResult); }
public RuleStatus CheckIntegrity(ConvertedOatRule convertedOatRule) { List <string> errors = new(); // App Inspector checks var rule = convertedOatRule.AppInspectorRule; // Check for null Id if (string.IsNullOrEmpty(rule.Id)) { _logger.LogError(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_NULLID_FAIL), rule.Name); errors.Add(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULES_NULLID_FAIL, rule.Name)); } //applicability if (rule.AppliesTo != null) { string[] languages = _options.LanguageSpecs.GetNames(); // Check for unknown language foreach (string lang in rule.AppliesTo) { if (!string.IsNullOrEmpty(lang)) { if (!languages.Any(x => x.Equals(lang, StringComparison.CurrentCultureIgnoreCase))) { _logger.LogError(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_LANGUAGE_FAIL), rule.Id ?? ""); errors.Add(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULES_LANGUAGE_FAIL, rule.Id ?? "")); } } } } foreach (var pattern in rule.FileRegexes ?? Array.Empty <string>()) { try { _ = new Regex(pattern, RegexOptions.Compiled); } catch (Exception e) { _logger?.LogError(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_REGEX_FAIL), rule.Id ?? "", pattern ?? "", e.Message); errors.Add(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULES_REGEX_FAIL, rule.Id ?? "", pattern ?? "", e.Message)); } } //valid search pattern foreach (SearchPattern searchPattern in rule.Patterns ?? Array.Empty <SearchPattern>()) { if (searchPattern.PatternType == PatternType.RegexWord || searchPattern.PatternType == PatternType.Regex) { try { if (string.IsNullOrEmpty(searchPattern.Pattern)) { throw new ArgumentException(); } _ = new Regex(searchPattern.Pattern); } catch (Exception e) { _logger?.LogError(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_REGEX_FAIL), rule.Id ?? "", searchPattern.Pattern ?? "", e.Message); errors.Add(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULES_REGEX_FAIL, rule.Id ?? "", searchPattern.Pattern ?? "", e.Message)); } } } foreach (var condition in rule.Conditions ?? Array.Empty <SearchCondition>()) { if (condition.SearchIn is null) { _logger?.LogError("SearchIn is null in {ruleId}", rule.Id); errors.Add(string.Format("SearchIn is null in {0}", rule.Id)); } else if (condition.SearchIn.StartsWith("finding-region")) { var parSplits = condition.SearchIn.Split(new char[] { ')', '(' }); if (parSplits.Length == 3) { var splits = parSplits[1].Split(','); if (splits.Length == 2) { if (int.TryParse(splits[0], out int int1) && int.TryParse(splits[1], out int int2)) { if (int1 > 0 && int2 < 0) { _logger?.LogError("The finding region must have a negative number or 0 for the lines before and a positive number or 0 for lines after. {0}", rule.Id); errors.Add(string.Format("The finding region must have a negative number or 0 for the lines before and a positive number or 0 for lines after. {0}", rule.Id)); } } } else { _logger?.LogError("Improperly specified finding region. {0}", rule.Id); errors.Add(string.Format("Improperly specified finding region. {0}", rule.Id)); } } else { _logger?.LogError("Improperly specified finding region. {0}", rule.Id); errors.Add(string.Format("Improperly specified finding region. {0}", rule.Id)); } } } if (rule.Tags?.Length == 0) { _logger?.LogError("Rule must specify tags. {0}", rule.Id); errors.Add(string.Format("Rule must specify tags. {0}", rule.Id)); } return(new RuleStatus() { RulesId = rule.Id, RulesName = rule.Name, Errors = errors, OatIssues = _analyzer.EnumerateRuleIssues(convertedOatRule) }); }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public TagDiffResult GetResult() { WriteOnce.SafeLog("TagDiffCommand::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Tag Diff")); TagDiffResult tagDiffResult = new() { AppVersion = Common.Utils.GetVersionString() }; //save to quiet analyze cmd and restore WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; try { if (_options is null) { throw new ArgumentNullException("_options"); } AnalyzeCommand cmd1 = new(new AnalyzeOptions() { SourcePath = _options.SourcePath1, CustomRulesPath = _options.CustomRulesPath, IgnoreDefaultRules = _options.IgnoreDefaultRules, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log, TagsOnly = true, ConfidenceFilters = _options.ConfidenceFilters, FileTimeOut = _options.FileTimeOut, ProcessingTimeOut = _options.ProcessingTimeOut, NoFileMetadata = true, NoShowProgress = true, ScanUnknownTypes = _options.ScanUnknownTypes, SingleThread = _options.SingleThread, }); AnalyzeCommand cmd2 = new(new AnalyzeOptions() { SourcePath = _options.SourcePath2, CustomRulesPath = _options.CustomRulesPath, IgnoreDefaultRules = _options.IgnoreDefaultRules, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log, TagsOnly = true, ConfidenceFilters = _options.ConfidenceFilters, FileTimeOut = _options.FileTimeOut, ProcessingTimeOut = _options.ProcessingTimeOut, NoFileMetadata = true, NoShowProgress = true, ScanUnknownTypes = _options.ScanUnknownTypes, SingleThread = _options.SingleThread, }); AnalyzeResult analyze1 = cmd1.GetResult(); AnalyzeResult analyze2 = cmd2.GetResult(); //restore WriteOnce.Verbosity = saveVerbosity; //process results for each analyze call before comparing results if (analyze1.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR, string.Join(',', _options.SourcePath1))); } else if (analyze2.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.FormatString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR, string.Join(',', _options.SourcePath2))); } else if (analyze1.ResultCode == AnalyzeResult.ExitCode.NoMatches || analyze2.ResultCode == AnalyzeResult.ExitCode.NoMatches) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.TAGDIFF_NO_TAGS_FOUND)); } else //compare tag results; assumed (result1&2 == AnalyzeCommand.ExitCode.Success) { var list1 = analyze1.Metadata.UniqueTags ?? new List <string>(); var list2 = analyze2.Metadata.UniqueTags ?? new List <string>(); var removed = list1.Except(list2); var added = list2.Except(list1); foreach (var add in added) { tagDiffResult.TagDiffList.Add(new TagDiff() { Source = TagDiff.DiffSource.Source2, Tag = add }); } foreach (var remove in removed) { tagDiffResult.TagDiffList.Add(new TagDiff() { Source = TagDiff.DiffSource.Source1, Tag = remove }); } if (tagDiffResult.TagDiffList.Count > 0) { tagDiffResult.ResultCode = _arg_tagTestType == TagTestType.Inequality ? TagDiffResult.ExitCode.TestPassed : TagDiffResult.ExitCode.TestFailed; } else { tagDiffResult.ResultCode = _arg_tagTestType == TagTestType.Inequality ? TagDiffResult.ExitCode.TestFailed : TagDiffResult.ExitCode.TestPassed; } return(tagDiffResult); } } catch (OpException e) { WriteOnce.Verbosity = saveVerbosity; WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } } }
/// <summary> /// Main entry from CLI /// </summary> /// <returns></returns> public TagTestResult GetResult() { WriteOnce.SafeLog("TagTestCommand::Run", LogLevel.Trace); WriteOnce.Operation(MsgHelp.FormatString(MsgHelp.ID.CMD_RUNNING, "Tag Test")); TagTestResult tagTestResult = new TagTestResult() { AppVersion = Utils.GetVersionString() }; //init based on true or false present argument value WriteOnce.ConsoleVerbosity saveVerbosity = WriteOnce.Verbosity; AnalyzeResult.ExitCode analyzeCmdResult = AnalyzeResult.ExitCode.CriticalError; try { //setup analyze call with silent option AnalyzeCommand cmd1 = new AnalyzeCommand(new AnalyzeOptions { SourcePath = _options.SourcePath, IgnoreDefaultRules = true, CustomRulesPath = _options.CustomRulesPath, FilePathExclusions = _options.FilePathExclusions, ConsoleVerbosityLevel = "none", Log = _options.Log }); //get and perform initial analyze on results AnalyzeResult analyze1 = cmd1.GetResult(); //restore WriteOnce.Verbosity = saveVerbosity; if (analyze1.ResultCode == AnalyzeResult.ExitCode.CriticalError) { throw new OpException(MsgHelp.GetString(MsgHelp.ID.CMD_CRITICAL_FILE_ERR)); } else if (analyzeCmdResult == AnalyzeResult.ExitCode.NoMatches) { WriteOnce.General(MsgHelp.FormatString(MsgHelp.ID.TAGTEST_RESULTS_TEST_TYPE, _arg_tagTestType.ToString()), false, WriteOnce.ConsoleVerbosity.Low); tagTestResult.ResultCode = _arg_tagTestType == TagTestType.RulesPresent ? TagTestResult.ExitCode.TestFailed : TagTestResult.ExitCode.TestPassed; } else //assumed (result == AnalyzeCommand.ExitCode.MatchesFound) { tagTestResult.ResultCode = TagTestResult.ExitCode.TestPassed; int count = 0; int sizeTags = analyze1.Metadata.UniqueTags.Count; string[] tagsFound = new string[sizeTags]; foreach (string tag in analyze1.Metadata.UniqueTags.Keys.ToList <string>()) { tagsFound[count++] = tag; } foreach (Rule rule in _rulesSet) { //supports both directions by generalizing string[] testList1 = _arg_tagTestType == TagTestType.RulesNotPresent ? rule.Tags : tagsFound; string[] testList2 = _arg_tagTestType == TagTestType.RulesNotPresent ? tagsFound : rule.Tags; foreach (string t in testList2) { if (TagTest(testList1, t)) { tagTestResult.TagsStatusList.Add(new TagStatus() { Tag = t, Detected = true }); } else { tagTestResult.ResultCode = TagTestResult.ExitCode.TestFailed; tagTestResult.TagsStatusList.Add(new TagStatus() { Tag = t, Detected = false }); } } } } } catch (OpException e) { WriteOnce.Verbosity = saveVerbosity; WriteOnce.Error(e.Message); //caught for CLI callers with final exit msg about checking log or throws for DLL callers throw; } return(tagTestResult); }