cli.ex (1185B)
1 defmodule Credo.CLI do 2 @moduledoc """ 3 `Credo.CLI` is the entrypoint for both the Mix task and the escript. 4 """ 5 6 alias Credo.Execution 7 8 @doc """ 9 Runs Credo with the given `argv` and exits the process. 10 11 See `Credo.run/1` if you want to run Credo programmatically. 12 """ 13 def main(argv \\ []) do 14 Credo.Application.start(nil, nil) 15 16 {options, _argv_rest, _errors} = OptionParser.parse(argv, strict: [watch: :boolean]) 17 18 if options[:watch] do 19 run_to_watch(argv) 20 else 21 run_to_halt(argv) 22 end 23 end 24 25 @doc false 26 @deprecated "Use Credo.run/1 instead" 27 def run(argv) do 28 Credo.run(argv) 29 end 30 31 defp run_to_watch(argv) do 32 Credo.Watcher.run(argv) 33 34 receive do 35 _ -> nil 36 end 37 end 38 39 defp run_to_halt(argv) do 40 argv 41 |> Credo.run() 42 |> halt_if_exit_status_assigned() 43 end 44 45 defp halt_if_exit_status_assigned(%Execution{mute_exit_status: true}) do 46 # Skip if exit status is muted 47 end 48 49 defp halt_if_exit_status_assigned(exec) do 50 exec 51 |> Execution.get_exit_status() 52 |> halt_if_failed() 53 end 54 55 defp halt_if_failed(0), do: nil 56 defp halt_if_failed(exit_status), do: exit({:shutdown, exit_status}) 57 end