aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/mozart/src/commands.rs
blob: c648754fc430c6f8a6027cfea0b276f8bdbe6555 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
pub mod about;
pub mod archive;
pub mod audit;
pub mod browse;
pub mod bump;
pub mod check_platform_reqs;
pub mod clear_cache;
pub mod completion;
pub mod config;
pub mod config_helpers;
pub mod create_project;
pub mod dependency;
pub mod depends;
pub mod diagnose;
pub mod dump_autoload;
pub mod exec;
pub mod fund;
pub mod global;
pub mod init;
pub mod install;
pub mod licenses;
pub mod outdated;
pub mod prohibits;
pub mod reinstall;
pub mod remove;
pub mod repository;
pub mod require;
pub mod run_script;
pub mod search;
pub mod self_update;
pub mod show;
pub mod status;
pub mod suggests;
pub mod update;
pub mod validate;

#[derive(clap::Parser)]
#[command(name = "mozart", about = "A PHP dependency manager")]
pub struct Cli {
    #[command(subcommand)]
    pub command: Option<Commands>,

    /// Display version information
    #[arg(short = 'V', long = "version")]
    pub version: bool,

    /// Increase the verbosity of messages: 1 for normal, 2 for more verbose, 3 for debug
    #[arg(short, long, action = clap::ArgAction::Count, global = true)]
    pub verbose: u8,

    /// Display timing and memory usage information
    #[arg(long, global = true)]
    pub profile: bool,

    /// Disables all plugins
    #[arg(long, global = true)]
    pub no_plugins: bool,

    /// Skips execution of all scripts defined in composer.json
    #[arg(long, global = true)]
    pub no_scripts: bool,

    /// If specified, use the given directory as working directory
    #[arg(short = 'd', long = "working-dir", global = true)]
    pub working_dir: Option<String>,

    /// Prevent use of the cache
    #[arg(long, global = true)]
    pub no_cache: bool,

    /// Do not ask any interactive question
    #[arg(short = 'n', long, global = true)]
    pub no_interaction: bool,

    /// Do not output any message
    #[arg(short, long, global = true)]
    pub quiet: bool,

    /// Force ANSI output
    #[arg(long, global = true)]
    pub ansi: bool,

    /// Disable ANSI output
    #[arg(long, global = true)]
    pub no_ansi: bool,
}

#[derive(clap::Subcommand)]
pub enum Commands {
    /// Shows a short information about Mozart
    About(about::AboutArgs),

    /// Creates an archive of this composer package
    Archive(archive::ArchiveArgs),

    /// Checks for security vulnerability advisories for installed packages
    Audit(audit::AuditArgs),

    /// Opens the package's repository URL or homepage in your browser
    #[command(alias = "home")]
    Browse(browse::BrowseArgs),

    /// Increases the lower limit of your composer.json requirements to the currently installed versions
    Bump(bump::BumpArgs),

    /// Check that platform requirements are satisfied
    #[command(name = "check-platform-reqs")]
    CheckPlatformReqs(check_platform_reqs::CheckPlatformReqsArgs),

    /// Clears Mozart's internal package cache
    #[command(name = "clear-cache", alias = "clearcache", alias = "cc")]
    ClearCache(clear_cache::ClearCacheArgs),

    /// Generate shell completion scripts
    Completion(completion::CompletionArgs),

    /// Sets config options
    Config(config::ConfigArgs),

    /// Creates new project from a package into given directory
    #[command(name = "create-project")]
    CreateProject(create_project::CreateProjectArgs),

    /// Shows which packages cause the given package to be installed
    #[command(alias = "why")]
    Depends(depends::DependsArgs),

    /// Diagnoses the system to identify common errors
    Diagnose(diagnose::DiagnoseArgs),

    /// Dumps the autoloader
    #[command(name = "dump-autoload", alias = "dumpautoload")]
    DumpAutoload(dump_autoload::DumpAutoloadArgs),

    /// Executes a vendored binary/script
    Exec(exec::ExecArgs),

    /// Discover how to help fund the maintenance of your dependencies
    Fund(fund::FundArgs),

    /// Allows running commands in the global composer dir ($COMPOSER_HOME)
    Global(global::GlobalArgs),

    /// Creates a basic composer.json file in current directory
    Init(init::InitArgs),

    /// Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json
    #[command(alias = "i")]
    Install(install::InstallArgs),

    /// Shows information about licenses of dependencies
    Licenses(licenses::LicensesArgs),

    /// Shows a list of installed packages that have updates available, including their latest version
    Outdated(outdated::OutdatedArgs),

    /// Shows which packages prevent the given package from being installed
    #[command(alias = "why-not")]
    Prohibits(prohibits::ProhibitsArgs),

    /// Uninstalls and reinstalls the given package names
    Reinstall(reinstall::ReinstallArgs),

    /// Removes a package from the require or require-dev
    #[command(alias = "rm", alias = "uninstall")]
    Remove(remove::RemoveArgs),

    /// Manages repositories
    #[command(alias = "repo")]
    Repository(repository::RepositoryArgs),

    /// Adds required packages to your composer.json and installs them
    #[command(alias = "r")]
    Require(require::RequireArgs),

    /// Runs the scripts defined in composer.json
    #[command(name = "run-script", alias = "run")]
    RunScript(run_script::RunScriptArgs),

    /// Searches for packages
    Search(search::SearchArgs),

    /// Updates Mozart to the latest version
    #[command(name = "self-update", alias = "selfupdate")]
    SelfUpdate(self_update::SelfUpdateArgs),

    /// Shows information about packages
    #[command(alias = "info")]
    Show(show::ShowArgs),

    /// Shows a list of locally modified packages
    Status(status::StatusArgs),

    /// Shows package suggestions
    #[command(
        long_about = "Shows package suggestions.\n\nRead more at https://getcomposer.org/doc/03-cli.md#suggests"
    )]
    Suggests(suggests::SuggestsArgs),

    /// Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file
    #[command(alias = "u", alias = "upgrade")]
    Update(update::UpdateArgs),

    /// Validates a composer.json and composer.lock
    Validate(validate::ValidateArgs),
}

impl Commands {
    pub fn name(&self) -> &'static str {
        match self {
            Commands::About(_) => "about",
            Commands::Archive(_) => "archive",
            Commands::Audit(_) => "audit",
            Commands::Browse(_) => "browse",
            Commands::Bump(_) => "bump",
            Commands::CheckPlatformReqs(_) => "check-platform-reqs",
            Commands::ClearCache(_) => "clear-cache",
            Commands::Completion(_) => "completion",
            Commands::Config(_) => "config",
            Commands::CreateProject(_) => "create-project",
            Commands::Depends(_) => "depends",
            Commands::Diagnose(_) => "diagnose",
            Commands::DumpAutoload(_) => "dump-autoload",
            Commands::Exec(_) => "exec",
            Commands::Fund(_) => "fund",
            Commands::Global(_) => "global",
            Commands::Init(_) => "init",
            Commands::Install(_) => "install",
            Commands::Licenses(_) => "licenses",
            Commands::Outdated(_) => "outdated",
            Commands::Prohibits(_) => "prohibits",
            Commands::Reinstall(_) => "reinstall",
            Commands::Remove(_) => "remove",
            Commands::Repository(_) => "repository",
            Commands::Require(_) => "require",
            Commands::RunScript(_) => "run-script",
            Commands::Search(_) => "search",
            Commands::SelfUpdate(_) => "self-update",
            Commands::Show(_) => "show",
            Commands::Status(_) => "status",
            Commands::Suggests(_) => "suggests",
            Commands::Update(_) => "update",
            Commands::Validate(_) => "validate",
        }
    }
}

#[tracing::instrument(skip(cli), fields(command = cli.command.as_ref().map(|c| c.name()).unwrap_or("none")))]
pub async fn execute(cli: &Cli) -> anyhow::Result<()> {
    let console = mozart_core::console::Console::new(
        cli.verbose,
        cli.quiet,
        cli.ansi,
        cli.no_ansi,
        cli.no_interaction,
    );
    let command = cli.command.as_ref().expect("command must be set");
    match command {
        Commands::About(args) => about::execute(args, cli, &console).await,
        Commands::Archive(args) => archive::execute(args, cli, &console).await,
        Commands::Audit(args) => audit::execute(args, cli, &console).await,
        Commands::Browse(args) => browse::execute(args, cli, &console).await,
        Commands::Bump(args) => bump::execute(args, cli, &console).await,
        Commands::CheckPlatformReqs(args) => {
            check_platform_reqs::execute(args, cli, &console).await
        }
        Commands::ClearCache(args) => clear_cache::execute(args, cli, &console).await,
        Commands::Completion(args) => completion::execute(args, cli, &console).await,
        Commands::Config(args) => config::execute(args, cli, &console).await,
        Commands::CreateProject(args) => create_project::execute(args, cli, &console).await,
        Commands::Depends(args) => depends::execute(args, cli, &console).await,
        Commands::Diagnose(args) => diagnose::execute(args, cli, &console).await,
        Commands::DumpAutoload(args) => dump_autoload::execute(args, cli, &console).await,
        Commands::Exec(args) => exec::execute(args, cli, &console).await,
        Commands::Fund(args) => fund::execute(args, cli, &console).await,
        Commands::Global(args) => global::execute(args, cli, &console).await,
        Commands::Init(args) => init::execute(args, cli, &console).await,
        Commands::Install(args) => install::execute(args, cli, &console).await,
        Commands::Licenses(args) => licenses::execute(args, cli, &console).await,
        Commands::Outdated(args) => outdated::execute(args, cli, &console).await,
        Commands::Prohibits(args) => prohibits::execute(args, cli, &console).await,
        Commands::Reinstall(args) => reinstall::execute(args, cli, &console).await,
        Commands::Remove(args) => remove::execute(args, cli, &console).await,
        Commands::Repository(args) => repository::execute(args, cli, &console).await,
        Commands::Require(args) => require::execute(args, cli, &console).await,
        Commands::RunScript(args) => run_script::execute(args, cli, &console).await,
        Commands::Search(args) => search::execute(args, cli, &console).await,
        Commands::SelfUpdate(args) => self_update::execute(args, cli, &console).await,
        Commands::Show(args) => show::execute(args, cli, &console).await,
        Commands::Status(args) => status::execute(args, cli, &console).await,
        Commands::Suggests(args) => suggests::execute(args, cli, &console).await,
        Commands::Update(args) => update::execute(args, cli, &console).await,
        Commands::Validate(args) => validate::execute(args, cli, &console).await,
    }
}