blob: ba6f272c36e68a20cf695647261474f33fda85ca [file]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::process;
use clap::Parser;
use crate::ensure_installed;
use crate::find_command;
use crate::run_command;
#[derive(Parser)]
pub struct CommandLint {
#[arg(long, help = "Automatically apply lint suggestions.")]
fix: bool,
}
impl CommandLint {
pub fn run(self) {
run_command(make_clippy_cmd(self.fix));
run_command(make_format_cmd(self.fix));
run_command(make_taplo_cmd(self.fix));
run_command(make_typos_cmd());
run_command(make_hawkeye_cmd(self.fix));
run_command(make_doc_cmd());
}
}
fn make_format_cmd(fix: bool) -> process::Command {
let mut cmd = find_command("cargo");
cmd.args(["+nightly", "fmt", "--all"]);
if !fix {
cmd.arg("--check");
}
cmd
}
fn make_clippy_cmd(fix: bool) -> process::Command {
let mut cmd = find_command("cargo");
cmd.args([
"+nightly",
"clippy",
"--tests",
"--all-features",
"--all-targets",
"--workspace",
]);
if fix {
cmd.args(["--allow-staged", "--allow-dirty", "--fix"]);
} else {
cmd.args(["--", "-D", "warnings"]);
}
cmd
}
fn make_doc_cmd() -> process::Command {
let mut cmd = find_command("cargo");
cmd.env("RUSTDOCFLAGS", "-D warnings --cfg docsrs");
cmd.args([
"+nightly",
"doc",
"--workspace",
"--all-features",
"--no-deps",
]);
cmd
}
fn make_hawkeye_cmd(fix: bool) -> process::Command {
ensure_installed("hawkeye", "hawkeye");
let mut cmd = find_command("hawkeye");
if fix {
cmd.args(["format"]);
} else {
cmd.args(["check"]);
}
cmd
}
fn make_typos_cmd() -> process::Command {
ensure_installed("typos", "typos-cli");
find_command("typos")
}
fn make_taplo_cmd(fix: bool) -> process::Command {
ensure_installed("taplo", "taplo-cli");
let mut cmd = find_command("taplo");
if fix {
cmd.args(["format"]);
} else {
cmd.args(["format", "--check"]);
}
cmd
}