Skip to content

Commit 83362e4

Browse files
committed
added disk-space
1 parent 7c3ae97 commit 83362e4

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

tmc-langs-cli/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ base64 = "0.13"
2626
schemars = "0.8"
2727
thiserror = "1"
2828
toml = "0.5"
29+
heim = { version = "0.1.0-beta.3", features = ["disk"] }
30+
smol = "1"
2931

3032
[dev-dependencies]
3133
tempfile = "3"

tmc-langs-cli/src/app.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ pub fn create_app() -> App<'static, 'static> {
6262

6363
.subcommand(create_core_app()) // "core"
6464

65+
.subcommand(
66+
SubCommand::with_name("disk-space")
67+
.about("Returns the amount of free disk space in megabytes left on the partition that contains the given path")
68+
.arg(Arg::with_name("path")
69+
.help("A path in the partition that should be inspected.")
70+
.required(true)
71+
.takes_value(true))
72+
)
73+
6574
.subcommand(SubCommand::with_name("extract-project")
6675
.about("Extracts an exercise from a ZIP archive. If the output-path is a project root, the plugin's student file policy will be used to avoid overwriting student files")
6776
.long_about(SCHEMA_NULL)
@@ -676,8 +685,7 @@ fn create_settings_app() -> App<'static, 'static> {
676685
),
677686
)
678687
.subcommand(
679-
SubCommand::with_name("list")
680-
.about("Prints every key=value pair in the settings file."),
688+
SubCommand::with_name("list").about("Prints every key=value pair in the settings file"),
681689
)
682690
.subcommand(
683691
SubCommand::with_name("move-projects-dir")

tmc-langs-cli/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use clap::{ArgMatches, Error, ErrorKind};
1010
use config::ProjectsConfig;
1111
use config::{CourseConfig, Credentials, Exercise, TmcConfig};
1212
use error::{InvalidTokenError, SandboxTestError};
13+
use heim::disk;
1314
use output::{
1415
CombinedCourseData, ErrorData, Kind, Output, OutputData, OutputResult, Status, Warnings,
1516
};
@@ -273,6 +274,27 @@ fn run_app(matches: ArgMatches, pretty: bool, warnings: &mut Vec<anyhow::Error>)
273274
}
274275
}
275276
}
277+
("disk-space", Some(matches)) => {
278+
let path = matches.value_of("path").unwrap();
279+
let path = Path::new(path);
280+
281+
let usage = smol::block_on(disk::usage(path)).with_context(|| {
282+
format!("Failed to get disk usage from path {}", path.display())
283+
})?;
284+
let free = usage.free().get::<heim::units::information::megabyte>();
285+
286+
let output = Output::OutputData(OutputData {
287+
status: Status::Finished,
288+
message: Some(format!(
289+
"calculated free disk space for partition containing {}",
290+
path.display()
291+
)),
292+
result: OutputResult::ExecutedCommand,
293+
percent_done: 1.0,
294+
data: Some(free),
295+
});
296+
print_output(&output, pretty, &warnings)?
297+
}
276298
("extract-project", Some(matches)) => {
277299
let archive_path = matches.value_of("archive-path").unwrap();
278300
let archive_path = Path::new(archive_path);

0 commit comments

Comments
 (0)