Skip to content

Commit

Permalink
Ignoring dotfiles, USAGE is now SUMMARY
Browse files Browse the repository at this point in the history
SUMMARY is a better description of the help string that
should displayed during a help function.  Renaming.

Ignoring any file or directory that starts with a dot.
  • Loading branch information
toumorokoshi committed Jul 19, 2019
1 parent 4ebd4a7 commit cc72e94
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 18 deletions.
Empty file added example/.directory/bar
Empty file.
Empty file added example/.foo
Empty file.
2 changes: 1 addition & 1 deletion example/file_example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# USAGE: hey'
# SUMMARY: hey'
# START HELP
# this is an example of a simple script written in python.
# END HELP
Expand Down
2 changes: 1 addition & 1 deletion example/source_example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SOURCE
# USAGE: an example for sourcing variables
# SUMMARY: an example for sourcing variables
# START HELP
# This is an example of a script that is sourced, rather
# than executed. This means that anything you run in here
Expand Down
2 changes: 1 addition & 1 deletion src/commands/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn help(root: &str, mut _args: Peekable<Iter<String>>) -> io::Result<String>
commands_with_help.push(format!(
" {}: {}",
escape_slashes(&command),
escape_slashes(&script.usage_string)
escape_slashes(&script.summary_string)
))
}
return Ok(format!(
Expand Down
12 changes: 6 additions & 6 deletions src/commands/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Script {
pub help_string: String,
/// the string that should be used for
/// usage information
pub usage_string: String,
pub summary_string: String,
}

impl Script {
Expand All @@ -33,7 +33,7 @@ impl Script {
let mut buffer = BufReader::new(body);
let mut should_source = false;
let mut help_string = String::new();
let mut usage_string = String::new();
let mut summary_string = String::new();
let mut line = String::new();
let mut consuming_help = false;
loop {
Expand All @@ -59,9 +59,9 @@ impl Script {
should_source = true;
} else if line.starts_with("# START HELP") {
consuming_help = true;
} else if line.starts_with("# USAGE: ") {
} else if line.starts_with("# SUMMARY: ") {
// 9 = prefix, -1 strips newline
usage_string.push_str(&line[9..(line.len() - 1)]);
summary_string.push_str(&line[11..(line.len() - 1)]);
} else if !line.starts_with("#!") {
// if a shebang is encountered, we skip.
// as it can indicate the command to run the script with.
Expand All @@ -74,7 +74,7 @@ impl Script {
path,
should_source,
help_string,
usage_string,
summary_string,
}
}

Expand Down Expand Up @@ -137,4 +137,4 @@ impl Script {
}
}
}
}
}
12 changes: 6 additions & 6 deletions src/commands/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_should_source() {
"# SOURCE
cd /tmp/
",
)) as Box<Read>,
)) as Box<dyn Read>,
);
assert_eq!(script.should_source, true);
}
Expand All @@ -25,7 +25,7 @@ fn test_should_not_source() {
"#!/usr/bin/env bash
echo foo
",
)) as Box<Read>,
)) as Box<dyn Read>,
);
assert_eq!(script.should_source, false);
}
Expand All @@ -40,7 +40,7 @@ fn test_help() {
# foo bar baz
# END HELP
",
)) as Box<Read>,
)) as Box<dyn Read>,
);
assert_eq!(&script.help_string, "foo bar baz\n");
}
Expand All @@ -51,12 +51,12 @@ fn test_usage() {
String::from("./example/foo"),
Box::new(Cursor::new(
"#!/usr/bin/env bash
# USAGE: this is the usage
# SUMMARY: this is the usage
# START HELP
# foo bar baz
# END HELP
",
)) as Box<Read>,
)) as Box<dyn Read>,
);
assert_eq!(&script.usage_string, "this is the usage");
assert_eq!(&script.summary_string, "this is the usage");
}
10 changes: 10 additions & 0 deletions src/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ pub fn scan_directory(
/// returns if this directory should be considered by tome
pub fn is_tome_script_directory(dir: &Path) -> bool {
let mut tomeignore_location = dir.to_path_buf();
// ignore dot directories
if tomeignore_location
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.starts_with(".")
{
return false;
}
tomeignore_location.push(".tomeignore");
return !tomeignore_location.exists();
}
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{env::args, fs, path::PathBuf};

mod commands;
mod directory;
mod script;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -100,13 +101,23 @@ pub fn execute(raw_args: Vec<String>) -> Result<String, String> {
TargetType::Directory => match command_type {
CommandType::Completion => {
let mut result = vec![];
let mut paths: Vec<_> = fs::read_dir(target.to_str().unwrap_or("")).unwrap().map(|r| r.unwrap()).collect();
let mut paths: Vec<_> = fs::read_dir(target.to_str().unwrap_or(""))
.unwrap()
.map(|r| r.unwrap())
.collect();
paths.sort_by_key(|f| f.path());
for path_buf in paths {
let path = path_buf.path();
if path.is_dir() && !directory::is_tome_script_directory(&path) {
continue;
}
if path.is_file()
&& !script::is_tome_script(
path_buf.file_name().to_str().unwrap_or_default(),
)
{
continue;
}
result.push(path.file_name().unwrap().to_str().unwrap_or("").to_owned());
}
result.join(" ").to_owned()
Expand Down
4 changes: 4 additions & 0 deletions src/script.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// used to determine if the file is a valid script or not
pub fn is_tome_script(filename: &str) -> bool {
return !filename.starts_with(".");
}
5 changes: 3 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ fn test_use_arg() {
#[test]
fn test_help_page() {
let result = execute(_vec_str(vec!["tome", EXAMPLE_DIR])).unwrap();
assert_eq!(result.matches("'\''").count(), 1);
assert_eq!(result.matches("'").count(), 3);
println!("{}", result);
assert_eq!(result.matches("'\\''").count(), 1);
assert_eq!(result.matches("'").count(), 5);
assert!(result.contains("echo -e"));
}

0 comments on commit cc72e94

Please sign in to comment.