From b63c629b81c63839292e6f0e77f18d711a38107b Mon Sep 17 00:00:00 2001 From: Jon Carstens Date: Mon, 18 Nov 2024 10:54:04 -0700 Subject: [PATCH 1/2] Support options from task and config Previously, if you attempted to mix options from the config file or via task switches, they would clobber each other producing mixed results. But it wasn't clear this behavior was not supported. This changes to support reading from config file _and_ mix task switches merging the options together. The task switches take precedence. It also defaults to using the config file if it is present, but leaves the switch so that `--no-config` could be used to explicitly ignore the config file instead --- lib/mix/tasks/sobelow.ex | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/mix/tasks/sobelow.ex b/lib/mix/tasks/sobelow.ex index 34cf37f..fd5b0cc 100644 --- a/lib/mix/tasks/sobelow.ex +++ b/lib/mix/tasks/sobelow.ex @@ -121,7 +121,7 @@ defmodule Mix.Tasks.Sobelow do {opts, _, _} = OptionParser.parse(argv, aliases: @aliases, switches: @switches) root = Keyword.get(opts, :root, ".") - config = Keyword.get(opts, :config, false) + config = Keyword.get(opts, :config, true) conf_file = root <> "/.sobelow-conf" conf_file? = config && File.exists?(conf_file) @@ -134,15 +134,16 @@ defmodule Mix.Tasks.Sobelow do opts = if conf_file? do - {:ok, opts} = File.read!(conf_file) |> Code.string_to_quoted() - opts + {:ok, file_opts} = File.read!(conf_file) |> Code.string_to_quoted() + # CLI args take precedence + Keyword.merge(file_opts, opts) else opts end {verbose, diff, details, private, strict, skip, mark_skip_all, clear_skip, router, exit_on, format, ignored, ignored_files, all_details, out, threshold, - version} = get_opts(opts, root, conf_file?) + version} = get_opts(opts, root) set_env(:verbose, verbose) @@ -214,7 +215,7 @@ defmodule Mix.Tasks.Sobelow do Application.put_env(:sobelow, key, value) end - defp get_opts(opts, root, conf_file?) do + defp get_opts(opts, root) do verbose = Keyword.get(opts, :verbose, false) details = Keyword.get(opts, :details, nil) all_details = Keyword.get(opts, :all_details) @@ -249,22 +250,18 @@ defmodule Mix.Tasks.Sobelow do format = out_format(out, format) - {ignored, ignored_files} = - if conf_file? do - {Keyword.get(opts, :ignore, []), - Keyword.get(opts, :ignore_files, []) |> Enum.map(&Path.expand(&1, root))} - else - ignored = - Keyword.get(opts, :ignore, "") - |> String.split(",") - - ignored_files = - Keyword.get(opts, :ignore_files, "") - |> String.split(",") - |> Enum.reject(fn file -> file == "" end) - |> Enum.map(&Path.expand(&1, root)) + ignored = + case Keyword.get(opts, :ignore, []) do + ignore_str when is_binary(ignore_str) -> String.split(ignore_str, ",") + ignore -> ignore + end - {ignored, ignored_files} + ignored_files = + case Keyword.get(opts, :ignore_files, []) do + ignore_files when is_list(ignore_files) -> + Enum.map(ignore_files, &Path.expand(&1, root)) + ignore_files_str when is_binary(ignore_files_str) -> + for i <- String.split(ignore_files_str, ",", trim: true), do: Path.expand(i, root) end threshold = From ba6d73ae2fff0189d28b4555a5f20b14e227f7b2 Mon Sep 17 00:00:00 2001 From: Jon Carstens Date: Mon, 18 Nov 2024 10:57:02 -0700 Subject: [PATCH 2/2] Fix compiler warning for function call --- lib/sobelow/finding_log.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sobelow/finding_log.ex b/lib/sobelow/finding_log.ex index 702ec69..65bebb8 100644 --- a/lib/sobelow/finding_log.ex +++ b/lib/sobelow/finding_log.ex @@ -110,7 +110,7 @@ defmodule Sobelow.FindingLog do [mod, _] = String.split(finding.type, ":", parts: 2) %{ - ruleId: Sobelow.get_mod(mod).id, + ruleId: Sobelow.get_mod(mod).id(), message: %{ text: finding.type },