forc_migrate/cli/
mod.rs

1//! The command line interface for `forc migrate`.
2mod commands;
3mod shared;
4
5use anyhow::Result;
6use clap::{Parser, Subcommand};
7use forc_tracing::{init_tracing_subscriber, LevelFilter, TracingSubscriberOptions};
8
9use self::commands::{check, run, show};
10
11use check::Command as CheckCommand;
12use run::Command as RunCommand;
13use show::Command as ShowCommand;
14
15fn help() -> &'static str {
16    Box::leak(
17        format!(
18            "Examples:\n{}{}{}",
19            show::examples(),
20            check::examples(),
21            run::examples(),
22        )
23        .trim_end()
24        .to_string()
25        .into_boxed_str(),
26    )
27}
28
29/// Forc plugin for migrating Sway projects to the next breaking change version of Sway.
30#[derive(Debug, Parser)]
31#[clap(
32    name = "forc-migrate",
33    after_help = help(),
34    version
35)]
36pub(crate) struct Opt {
37    /// The command to run
38    #[clap(subcommand)]
39    command: ForcMigrate,
40}
41
42impl Opt {
43    fn silent(&self) -> bool {
44        match &self.command {
45            ForcMigrate::Show(_) => true,
46            ForcMigrate::Check(command) => command.check.silent,
47            ForcMigrate::Run(command) => command.run.silent,
48        }
49    }
50}
51
52#[derive(Subcommand, Debug)]
53enum ForcMigrate {
54    Show(ShowCommand),
55    Check(CheckCommand),
56    Run(RunCommand),
57}
58
59pub fn run_cli() -> Result<()> {
60    let opt = Opt::parse();
61
62    let tracing_options = TracingSubscriberOptions {
63        silent: Some(opt.silent()),
64        log_level: Some(LevelFilter::INFO),
65        ..Default::default()
66    };
67
68    init_tracing_subscriber(tracing_options);
69
70    match opt.command {
71        ForcMigrate::Show(command) => show::exec(command),
72        ForcMigrate::Check(command) => check::exec(command),
73        ForcMigrate::Run(command) => run::exec(command),
74    }
75}