Skip to main content

mz_deploy/cli/commands/
clean.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Clean command — delete the project's `target/` build directory.
11
12use crate::cli::CliError;
13use crate::config::Settings;
14use crate::log;
15use crate::types::BUILD_DIR;
16use std::fmt;
17use std::path::PathBuf;
18
19#[derive(serde::Serialize)]
20struct CleanResult {
21    path: PathBuf,
22    removed: bool,
23}
24
25impl fmt::Display for CleanResult {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        if self.removed {
28            write!(f, "  \u{2713} Removed {}", self.path.display())
29        } else {
30            write!(
31                f,
32                "  \u{2713} Nothing to clean ({} not present)",
33                self.path.display()
34            )
35        }
36    }
37}
38
39/// Delete the project's `target/` directory.
40///
41/// Idempotent: succeeds (with a "nothing to clean" message) when `target/`
42/// does not exist.
43pub fn run(settings: &Settings) -> Result<(), CliError> {
44    let target = settings.directory.join(BUILD_DIR);
45    let removed = match std::fs::remove_dir_all(&target) {
46        Ok(()) => true,
47        Err(e) if e.kind() == std::io::ErrorKind::NotFound => false,
48        Err(e) => return Err(CliError::Io(e)),
49    };
50    log::output(&CleanResult {
51        path: target,
52        removed,
53    });
54    Ok(())
55}