Skip to content

Commit ec711cb

Browse files
authored
remove -d and -t from touch (nushell#6629)
* remove -d and -t from touch * remove unused test import
1 parent f2ad7fa commit ec711cb

File tree

2 files changed

+3
-888
lines changed

2 files changed

+3
-888
lines changed

crates/nu-command/src/filesystem/touch.rs

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
use std::fs::OpenOptions;
22
use std::path::Path;
33

4-
use chrono::{DateTime, Datelike, Local};
4+
use chrono::{DateTime, Local};
55
use filetime::FileTime;
66

77
use nu_engine::CallExt;
88
use nu_protocol::ast::Call;
99
use nu_protocol::engine::{Command, EngineState, Stack};
1010
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape};
1111

12-
use crate::parse_date_from_string;
13-
14-
enum AddYear {
15-
Full,
16-
FirstDigits,
17-
}
18-
1912
#[derive(Clone)]
2013
pub struct Touch;
2114

@@ -35,18 +28,6 @@ impl Command for Touch {
3528
SyntaxShape::Filepath,
3629
"the path of the file you want to create",
3730
)
38-
.named(
39-
"timestamp",
40-
SyntaxShape::String,
41-
"change the file or directory time to a timestamp. Format: [[CC]YY]MMDDhhmm[.ss]\n\n If neither YY or CC is given, the current year will be assumed. If YY is specified, but CC is not, CC will be derived as follows:\n \tIf YY is between [69, 99], CC is 19\n \tIf YY is between [00, 68], CC is 20\n Note: It is expected that in a future version of this standard the default century inferred from a 2-digit year will change",
42-
Some('t'),
43-
)
44-
.named(
45-
"date",
46-
SyntaxShape::String,
47-
"change the file or directory time to a date",
48-
Some('d'),
49-
)
5031
.named(
5132
"reference",
5233
SyntaxShape::String,
@@ -85,8 +66,6 @@ impl Command for Touch {
8566
) -> Result<PipelineData, ShellError> {
8667
let mut change_mtime: bool = call.has_flag("modified");
8768
let mut change_atime: bool = call.has_flag("access");
88-
let use_stamp: bool = call.has_flag("timestamp");
89-
let use_date: bool = call.has_flag("date");
9069
let use_reference: bool = call.has_flag("reference");
9170
let no_create: bool = call.has_flag("no-create");
9271
let target: String = call.req(engine_state, stack, 0)?;
@@ -105,111 +84,6 @@ impl Command for Touch {
10584
date = Some(Local::now());
10685
}
10786

108-
if use_stamp || use_date {
109-
let (val, span) = if use_stamp {
110-
let stamp: Option<Spanned<String>> =
111-
call.get_flag(engine_state, stack, "timestamp")?;
112-
let (stamp, span) = match stamp {
113-
Some(stamp) => (stamp.item, stamp.span),
114-
None => {
115-
return Err(ShellError::MissingParameter(
116-
"timestamp".to_string(),
117-
call.head,
118-
));
119-
}
120-
};
121-
122-
// Checks for the seconds stamp and removes the '.' delimiter if any
123-
let (val, has_sec): (String, bool) = match stamp.split_once('.') {
124-
Some((dtime, sec)) => match sec.parse::<u8>() {
125-
Ok(sec) if sec < 60 => (format!("{}{}", dtime, sec), true),
126-
_ => {
127-
return Err(ShellError::UnsupportedInput(
128-
"input has an invalid timestamp".to_string(),
129-
span,
130-
))
131-
}
132-
},
133-
None => (stamp.to_string(), false),
134-
};
135-
136-
let size = val.len();
137-
138-
// Each stamp is a 2 digit number and the whole stamp must not be less than 4 or greater than 7 pairs
139-
if (size % 2 != 0 || !(8..=14).contains(&size)) || val.parse::<u64>().is_err() {
140-
return Err(ShellError::UnsupportedInput(
141-
"input has an invalid timestamp".to_string(),
142-
span,
143-
));
144-
}
145-
146-
let add_year: Option<AddYear> = if has_sec {
147-
match size {
148-
10 => Some(AddYear::Full),
149-
12 => Some(AddYear::FirstDigits),
150-
14 => None,
151-
_ => {
152-
return Err(ShellError::UnsupportedInput(
153-
"input has an invalid timestamp".to_string(),
154-
span,
155-
))
156-
}
157-
}
158-
} else {
159-
match size {
160-
8 => Some(AddYear::Full),
161-
10 => Some(AddYear::FirstDigits),
162-
12 => None,
163-
_ => {
164-
return Err(ShellError::UnsupportedInput(
165-
"input has an invalid timestamp".to_string(),
166-
span,
167-
))
168-
}
169-
}
170-
};
171-
172-
if let Some(add_year) = add_year {
173-
let year = Local::now().year();
174-
match add_year {
175-
AddYear::Full => (format!("{}{}", year, val), span),
176-
AddYear::FirstDigits => {
177-
// Compliance with the Unix version of touch
178-
let yy = val[0..2]
179-
.parse::<u8>()
180-
.expect("should be a valid 2 digit number");
181-
let mut year = 20;
182-
if (69..=99).contains(&yy) {
183-
year = 19;
184-
}
185-
(format!("{}{}", year, val), span)
186-
}
187-
}
188-
} else {
189-
(val, span)
190-
}
191-
} else {
192-
let date_string: Option<Spanned<String>> =
193-
call.get_flag(engine_state, stack, "date")?;
194-
match date_string {
195-
Some(date_string) => (date_string.item, date_string.span),
196-
None => {
197-
return Err(ShellError::MissingParameter("date".to_string(), call.head));
198-
}
199-
}
200-
};
201-
202-
date = if let Ok(parsed_date) = parse_date_from_string(&val, span) {
203-
Some(parsed_date.into())
204-
} else {
205-
let flag = if use_stamp { "timestamp" } else { "date" };
206-
return Err(ShellError::UnsupportedInput(
207-
format!("input has an invalid {}", flag),
208-
span,
209-
));
210-
};
211-
}
212-
21387
if use_reference {
21488
let reference: Option<Spanned<String>> =
21589
call.get_flag(engine_state, stack, "reference")?;
@@ -336,11 +210,6 @@ impl Command for Touch {
336210
example: "touch -m fixture.json",
337211
result: None,
338212
},
339-
Example {
340-
description: "Creates files d and e and set its last modified time to a timestamp",
341-
example: "touch -m -t 201908241230.30 d e",
342-
result: None,
343-
},
344213
Example {
345214
description: "Changes the last modified time of files a, b and c to a date",
346215
example: r#"touch -m -d "yesterday" a b c"#,
@@ -356,11 +225,6 @@ impl Command for Touch {
356225
example: r#"touch -a -d "August 24, 2019; 12:30:30" fixture.json"#,
357226
result: None,
358227
},
359-
Example {
360-
description: "Changes both last modified and accessed time of a, b and c to a timestamp only if they exist",
361-
example: r#"touch -c -t 201908241230.30 a b c"#,
362-
result: None,
363-
},
364228
]
365229
}
366230
}

0 commit comments

Comments
 (0)