mirror of
https://github.com/mgree/ffs.git
synced 2024-10-27 04:22:43 +03:00
flag for newlines in values
This commit is contained in:
parent
690b24f5a7
commit
c2d811f7e1
@ -3,6 +3,7 @@ pub struct Config {
|
||||
pub timestamp: std::time::SystemTime,
|
||||
pub uid: u32,
|
||||
pub gid: u32,
|
||||
pub add_newlines: bool,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@ -31,6 +32,7 @@ impl Default for Config {
|
||||
timestamp: std::time::SystemTime::now(),
|
||||
uid: 501,
|
||||
gid: 501,
|
||||
add_newlines: false,
|
||||
}
|
||||
}
|
||||
}
|
10
src/json.rs
10
src/json.rs
@ -54,12 +54,12 @@ pub fn fs(config: Config, v: Value) -> FS {
|
||||
while !worklist.is_empty() {
|
||||
let (parent, inum, v) = worklist.pop().unwrap();
|
||||
|
||||
let nl = if config.add_newlines { "\n" } else { "" };
|
||||
let entry = match v {
|
||||
// TODO 2021-06-09 option to add newlines
|
||||
Value::Null => Entry::File("".into()),
|
||||
Value::Bool(b) => Entry::File(format!("{}", b)),
|
||||
Value::Number(n) => Entry::File(format!("{}", n)),
|
||||
Value::String(s) => Entry::File(s),
|
||||
Value::Null => Entry::File(nl.into()),
|
||||
Value::Bool(b) => Entry::File(format!("{}{}", b, nl)),
|
||||
Value::Number(n) => Entry::File(format!("{}{}", n, nl)),
|
||||
Value::String(s) => Entry::File(if s.ends_with('\n') { s } else { s + nl }),
|
||||
Value::Array(vs) => {
|
||||
let mut children = HashMap::new();
|
||||
children.reserve(vs.len());
|
||||
|
13
src/main.rs
13
src/main.rs
@ -38,6 +38,17 @@ fn main() {
|
||||
.long("gid")
|
||||
.takes_value(true),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("NEWLINE-ADD")
|
||||
.help("Add a newline to the end of values that don't already have them (overrides --exact)")
|
||||
.long("newline")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("NEWLINE-EXACT")
|
||||
.help("Render values exactly, with no added newlines (overrides --newline)")
|
||||
.long("exact")
|
||||
.overrides_with("NEWLINE-ADD")
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("MOUNT")
|
||||
.help("Sets the mountpoint")
|
||||
@ -61,6 +72,8 @@ fn main() {
|
||||
.with(fmt_layer)
|
||||
.init();
|
||||
|
||||
config.add_newlines = args.is_present("NEWLINE-ADD");
|
||||
|
||||
let autounmount = args.is_present("AUTOUNMOUNT");
|
||||
|
||||
// TODO 2021-06-08 infer and create mountpoint from filename as possible
|
||||
|
43
tests/basic_object_exact.sh
Executable file
43
tests/basic_object_exact.sh
Executable file
@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
|
||||
fail() {
|
||||
echo FAILED: $1
|
||||
if [ "$MNT" ]
|
||||
then
|
||||
umount "$MNT"
|
||||
rmdir "$MNT"
|
||||
rm -r "$EXP"
|
||||
fi
|
||||
exit 1
|
||||
}
|
||||
|
||||
MNT=$(mktemp -d)
|
||||
EXP=$(mktemp -d)
|
||||
|
||||
# generate files w/o newlines
|
||||
printf "Michael Greenberg" >"${EXP}/name"
|
||||
printf "2" >"${EXP}/eyes"
|
||||
printf "10" >"${EXP}/fingernails"
|
||||
printf "true" >"${EXP}/human"
|
||||
|
||||
ffs --exact "$MNT" ../json/object.json &
|
||||
PID=$!
|
||||
sleep 1
|
||||
cd "$MNT"
|
||||
case $(ls) in
|
||||
(eyes*fingernails*human*name) ;;
|
||||
(*) fail ls;;
|
||||
esac
|
||||
diff "${EXP}/name" "${MNT}/name" || fail name
|
||||
diff "${EXP}/eyes" "${MNT}/eyes" || fail eyes
|
||||
diff "${EXP}/fingernails" "${MNT}/fingernails" || fail fingernails
|
||||
diff "${EXP}/human" "${MNT}/human" || fail human
|
||||
|
||||
cd - >/dev/null 2>&1
|
||||
umount "$MNT" || fail unmount
|
||||
sleep 1
|
||||
|
||||
kill -0 $PID >/dev/null 2>&1 && fail process
|
||||
|
||||
rmdir "$MNT" || fail mount
|
||||
rm -r "$EXP"
|
43
tests/basic_object_newline.sh
Executable file
43
tests/basic_object_newline.sh
Executable file
@ -0,0 +1,43 @@
|
||||
#!/bin/sh
|
||||
|
||||
fail() {
|
||||
echo FAILED: $1
|
||||
if [ "$MNT" ]
|
||||
then
|
||||
umount "$MNT"
|
||||
rmdir "$MNT"
|
||||
rm -r "$EXP"
|
||||
fi
|
||||
exit 1
|
||||
}
|
||||
|
||||
MNT=$(mktemp -d)
|
||||
EXP=$(mktemp -d)
|
||||
|
||||
# generate files w/newlines
|
||||
printf "Michael Greenberg\n" >"${EXP}/name"
|
||||
printf "2\n" >"${EXP}/eyes"
|
||||
printf "10\n" >"${EXP}/fingernails"
|
||||
printf "true\n" >"${EXP}/human"
|
||||
|
||||
ffs --newline "$MNT" ../json/object.json &
|
||||
PID=$!
|
||||
sleep 1
|
||||
cd "$MNT"
|
||||
case $(ls) in
|
||||
(eyes*fingernails*human*name) ;;
|
||||
(*) fail ls;;
|
||||
esac
|
||||
diff "${EXP}/name" "${MNT}/name" || fail name
|
||||
diff "${EXP}/eyes" "${MNT}/eyes" || fail eyes
|
||||
diff "${EXP}/fingernails" "${MNT}/fingernails" || fail fingernails
|
||||
diff "${EXP}/human" "${MNT}/human" || fail human
|
||||
|
||||
cd - >/dev/null 2>&1
|
||||
umount "$MNT" || fail unmount
|
||||
sleep 1
|
||||
|
||||
kill -0 $PID >/dev/null 2>&1 && fail process
|
||||
|
||||
rmdir "$MNT" || fail mount
|
||||
rm -r "$EXP"
|
Loading…
Reference in New Issue
Block a user