Skip to content

Commit

Permalink
debug flag
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmindlin committed Jun 16, 2024
1 parent 4516465 commit ac6e4e8
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 11 deletions.
108 changes: 108 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ colored = "2"
fantoccini = "0.19.3"
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
clap = { version = "4.5.7", features = ["derive"] }

# The profile that 'cargo dist' will build with
[profile.dist]
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Eventually Scout installation will come bundled with the necessary pre-reqs. For

See [Releases](https://github.com/maxmindlin/scout-lang/releases) for up to date Scout binary installation instructions.

# Usage

The `scout` binary ran with a filename will read and interpret a script file. Without a script will start the REPL. Run `scout --help` to see additional options.

# License

Scout is dual-licensed with MIT & Apache 2.0, at your option.
Scout is dual-licensed with MIT & Apache 2.0, at your option.
34 changes: 24 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{env, fs, process::Command, sync::Arc};
use std::{fs, process::Command, sync::Arc};

use clap::Parser as CLIParser;
use futures::lock::Mutex;
use repl::run_repl;
use scout_interpreter::{env::Env, eval, ScrapeResultsPtr};
Expand All @@ -8,16 +9,23 @@ use scout_parser::{ast::NodeKind, Parser};

mod repl;

#[derive(CLIParser, Debug)]
struct Args {
file: Option<String>,

#[arg(long)]
debug: bool,
}

async fn run(
args: Vec<String>,
file: Option<String>,
crawler: &fantoccini::Client,
results: ScrapeResultsPtr,
) -> Result<(), Box<dyn std::error::Error>> {
match args.len() {
1 => run_repl(crawler, results).await,
2 => {
let filename = &args[1];
let contents = fs::read_to_string(filename)?;
match file {
None => run_repl(crawler, results).await,
Some(f) => {
let contents = fs::read_to_string(f)?;

let lex = Lexer::new(&contents);
let mut parser = Parser::new(lex);
Expand All @@ -34,12 +42,13 @@ async fn run(
Err(e) => Err(format!("Parser error: {:#?}", e).into()),
}
}
_ => Err("Unsupported number of args".into()),
}
}

#[tokio::main]
async fn main() {
let args = Args::parse();

let child = Command::new("geckodriver")
.arg("--port")
.arg("4444")
Expand All @@ -51,14 +60,19 @@ async fn main() {
// sleep to allow driver to start
std::thread::sleep(std::time::Duration::from_millis(50));

let mut caps = serde_json::map::Map::new();
if !args.debug {
let opts = serde_json::json!({ "args": ["--headless"] });
caps.insert("moz:firefoxOptions".into(), opts);
}
let crawler = fantoccini::ClientBuilder::native()
.capabilities(caps)
.connect("http://localhost:4444")
.await
.expect("error starting browser");

let results = ScrapeResultsPtr::default();
let args: Vec<String> = env::args().collect();
if let Err(e) = run(args, &crawler, results.clone()).await {
if let Err(e) = run(args.file, &crawler, results.clone()).await {
println!("Error: {}", e);
}
let json_results = results.lock().await.to_json();
Expand Down

0 comments on commit ac6e4e8

Please sign in to comment.