forc_migrate/cli/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! The command line interface for `forc migrate`.
mod commands;
mod shared;

use anyhow::Result;
use clap::{Parser, Subcommand};
use forc_tracing::{init_tracing_subscriber, LevelFilter, TracingSubscriberOptions};

use self::commands::{check, run, show};

use check::Command as CheckCommand;
use run::Command as RunCommand;
use show::Command as ShowCommand;

fn help() -> &'static str {
    Box::leak(
        format!(
            "Examples:\n{}{}{}",
            show::examples(),
            check::examples(),
            run::examples(),
        )
        .trim_end()
        .to_string()
        .into_boxed_str(),
    )
}

/// Forc plugin for migrating Sway projects to the next breaking change version of Sway.
#[derive(Debug, Parser)]
#[clap(
    name = "forc-migrate",
    after_help = help(),
    version
)]
pub(crate) struct Opt {
    /// The command to run
    #[clap(subcommand)]
    command: ForcMigrate,
}

impl Opt {
    fn silent(&self) -> bool {
        match &self.command {
            ForcMigrate::Show(_) => true,
            ForcMigrate::Check(command) => command.check.silent,
            ForcMigrate::Run(command) => command.run.silent,
        }
    }
}

#[derive(Subcommand, Debug)]
enum ForcMigrate {
    Show(ShowCommand),
    Check(CheckCommand),
    Run(RunCommand),
}

pub fn run_cli() -> Result<()> {
    let opt = Opt::parse();

    let tracing_options = TracingSubscriberOptions {
        silent: Some(opt.silent()),
        log_level: Some(LevelFilter::INFO),
        ..Default::default()
    };

    init_tracing_subscriber(tracing_options);

    match opt.command {
        ForcMigrate::Show(command) => show::exec(command),
        ForcMigrate::Check(command) => check::exec(command),
        ForcMigrate::Run(command) => run::exec(command),
    }
}