Skip to content

Commit 387c1a3

Browse files
authored
allow file arguments to be - to use stdin/out (#9)
1 parent 76e041d commit 387c1a3

File tree

3 files changed

+36
-27
lines changed

3 files changed

+36
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "godot-package-manager"
3-
version = "0.3.1"
3+
version = "0.4.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/config_file.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use crate::package::Package;
22
use serde::{Deserialize, Serialize};
33
use std::collections::HashMap;
4-
use std::fs::write;
5-
use std::path::PathBuf;
64

75
#[derive(Debug, Default)]
86
/// The config file: parsed from godot.package, usually.
@@ -21,13 +19,12 @@ struct PackageLock {
2119
impl ConfigFile {
2220
/// Creates a new [ConfigFile] from the given path.
2321
/// Panics if the file doesn't exist, or the file cant be parsed as toml, hjson or yaml.
24-
pub fn new(path: PathBuf) -> Self {
22+
pub fn new(contents: &String) -> Self {
2523
#[derive(Debug, Deserialize, Default)]
2624
#[serde(default)]
2725
struct W {
2826
packages: HashMap<String, String>,
2927
}
30-
let contents = &std::fs::read_to_string(path).expect("The config file should exist");
3128
#[rustfmt::skip]
3229
let cfg: W = if let Ok(w) = deser_hjson::from_str(contents) { w }
3330
else if let Ok(w) = serde_yaml::from_str(contents) { w }
@@ -45,22 +42,18 @@ impl ConfigFile {
4542

4643
/// Creates a lockfile for this config file.
4744
/// note: Lockfiles are currently unused.
48-
pub fn lock(&mut self, path: PathBuf) {
49-
write(
50-
path,
51-
serde_json::to_string(
52-
&self
53-
.collect()
54-
.into_iter()
55-
.filter_map(|p| {
56-
p.is_installed()
57-
.then_some((p.name.clone(), PackageLock::new(p)))
58-
})
59-
.collect::<HashMap<String, PackageLock>>(),
60-
)
61-
.unwrap(),
45+
pub fn lock(&mut self) -> String {
46+
serde_json::to_string(
47+
&self
48+
.collect()
49+
.into_iter()
50+
.filter_map(|p| {
51+
p.is_installed()
52+
.then_some((p.name.clone(), PackageLock::new(p)))
53+
})
54+
.collect::<HashMap<String, PackageLock>>(),
6255
)
63-
.expect("Writing lock file should work");
56+
.unwrap()
6457
}
6558

6659
/// Iterates over all the packages (and their deps) in this config file.

src/main.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::package::Package;
66
use clap::Parser;
77
use config_file::ConfigFile;
88
use std::env::current_dir;
9-
use std::fs::{create_dir, read_dir, remove_dir};
10-
use std::io::Result;
9+
use std::fs::{create_dir, read_dir, remove_dir, write};
10+
use std::io::{stdin, Read, Result};
1111
use std::panic;
1212
use std::path::{Path, PathBuf};
1313

@@ -24,15 +24,15 @@ struct Args {
2424
default_value = "godot.package",
2525
global = true
2626
)]
27-
/// Specify the location of the package configuration file (https://github.com/godot-package-manager#godotpackage).
27+
/// Specify the location of the package configuration file (https://github.com/godot-package-manager#godotpackage). If -, read from stdin.
2828
config_file: PathBuf,
2929
#[arg(
3030
short = 'l',
3131
long = "lock-file",
3232
default_value = "godot.lock",
3333
global = true
3434
)]
35-
/// Specify the location of the lock file
35+
/// Specify the location of the lock file. If -, print to stdout.
3636
lock_file: PathBuf,
3737
}
3838

@@ -68,14 +68,30 @@ fn main() {
6868
else { println!("unknown"); };
6969
}));
7070
let args = Args::parse();
71-
let mut cfg_file = ConfigFile::new(args.config_file);
71+
let mut contents = String::from("");
72+
if args.config_file == Path::new("-") {
73+
let bytes = stdin()
74+
.read_to_string(&mut contents)
75+
.expect("Stdin read should be ok");
76+
if bytes == 0 {
77+
panic!("Stdin should not be empty");
78+
};
79+
} else {
80+
contents =
81+
std::fs::read_to_string(args.config_file).expect("Reading config file should be ok");
82+
};
83+
let mut cfg_file = ConfigFile::new(&contents);
7284
match args.action {
7385
Actions::Update => update(&mut cfg_file),
7486
Actions::Purge => purge(&mut cfg_file),
7587
Actions::Tree => tree(&cfg_file),
7688
}
77-
cfg_file.lock(args.lock_file);
78-
println!("Finished");
89+
let lockfile = cfg_file.lock();
90+
if args.lock_file == Path::new("-") {
91+
println!("{lockfile}");
92+
} else {
93+
write(args.lock_file, lockfile).expect("Writing lock file should be ok");
94+
}
7995
}
8096

8197
fn update(cfg: &mut ConfigFile) {

0 commit comments

Comments
 (0)