1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use std::path;
16 
17 use clap::Parser;
18 use cmd_runner::{
19     cargo_workspace::{CargoOptions, CargoWorkspaceSubcommand, FormatterOptions},
20     license_checker::LicenseSubcommand,
21 };
22 use license::LICENSE_CHECKER;
23 use xshell::Shell;
24 
25 mod license;
26 
27 #[derive(clap::Parser)]
28 struct Cli {
29     #[clap(subcommand)]
30     subcommand: Subcommand,
31 }
32 
33 #[derive(clap::Subcommand, Debug, Clone)]
34 enum Subcommand {
35     VerifyCi {
36         #[command(flatten)]
37         cargo_options: CargoOptions,
38     },
39     #[command(flatten)]
40     CargoWorkspace(CargoWorkspaceSubcommand),
41     #[command(flatten)]
42     License(LicenseSubcommand),
43 }
44 
main() -> anyhow::Result<()>45 fn main() -> anyhow::Result<()> {
46     let args = Cli::parse();
47     let root_dir = path::Path::new(
48         &std::env::var_os("CARGO_MANIFEST_DIR")
49             .expect("Must be run via Cargo to establish root directory"),
50     )
51     .parent()
52     .expect("Workspace directory should exist")
53     .to_path_buf();
54     let sh = Shell::new()?;
55     sh.change_dir(&root_dir);
56     match args.subcommand {
57         Subcommand::VerifyCi { cargo_options } => {
58             cargo_options.check_workspace(&sh, "common")?;
59             FormatterOptions { reformat: false }.check_format(&sh)?;
60             LICENSE_CHECKER.check(&root_dir)?;
61         }
62         Subcommand::CargoWorkspace(workspace) => workspace.run("common", &sh)?,
63         Subcommand::License(license) => license.run(&LICENSE_CHECKER, &root_dir)?,
64     }
65     Ok(())
66 }
67