Skip to content

Commit

Permalink
feat: add all single-letter curl options (with tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoverth committed Jun 18, 2024
1 parent c8489e5 commit c9c379f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ctow"
version = "0.1.5"
version = "1.0.0"
edition = "2021"
license = "GPL-3.0-or-later"
keywords = ["curl", "wget", "conversion"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ctow can be used in two modes: command with arguments and as a command line inte

You can simply pass `ctow [curl command]`, and ctow will print the converted command to stdout, with no formatting or anything when successful, so the output can be piped and manipulated into a command like `wget $(ctow [...])`, etc

Do note that when passing in a curl command, you do not explicitly need to include the leading `curl`, as it is discarded anyway.
Do note that when passing in a curl command, you do not explicitly need to include the leading `curl`, as it is discarded anyway. The options that curl uses (e.g. `-A User-Agent-Here`) __needs__ to have a space between the flag and its argument (this is a TODO).

### Command-Line Interface

Expand Down
89 changes: 89 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,27 @@ pub fn convert_arg(arg: &str) -> Result<String, Errors> {
} else {
// else, replace the curl with the wget
match arg.split(' ').collect::<Vec<&str>>()[0] {
"-b" => Ok(arg.replace("-b ", "--load-cookies=")),
"-c" => Ok(arg.replace("-c ", "--save-cookies=")),
"-d" => Ok(arg.replace("-d ", "--post-data=")),
"-e" => Ok(arg.replace("-e ", "--header=\"Referer: ") + "\""),
"-g" => Ok(arg.replace("-g", "--no-glob")),
"-k" => Ok(arg.replace("-k", "--no-check-certificate")),
"-m" => Ok(arg.replace("-m ", "--timeout=")),
"-o" => Ok(arg.replace("-o ", "--output-document=")),
"-r" => Ok(arg.replace("-r ", "--header=\"Range: bytes=") + "\""),
"-s" => Ok(arg.replace("-s", "--quiet")),
"-u" => Ok(arg.replace("-u ", "--user=")),
"-z" => Ok(arg.replace("-z ", "--header=\"If-Modified-Since: ") + "\""),

"-A" => Ok(arg.replace("-A ", "--header=\"User-Agent: ") + "\""),
"-C" => Ok(arg.replace("-C ", "--start-pos=")),
"-E" => Ok(arg.replace("-E ", "--certificate=")),
"-H" => Ok(arg.replace("-H ", "--header '").replace('\\', "\\\\") + "'"),
"-I" => Ok(arg.replace("-I", "--method=HEAD")),
"-T" => Ok(arg.replace("-T ", "--method=PUT --body-file=")),
"-X" => Ok(arg.replace("-X ", "--method=")),

"--compressed" => Ok(arg.replace("--compressed", "--compression=auto")),
"--connect-timeout" => Ok(arg.replace("--connect-timeout ", "--timeout=")),
"--retry" => Ok(arg.replace("--retry ", "--tries=")),
Expand All @@ -132,14 +152,83 @@ mod test {

#[test]
fn test_convert_args() {
// in this current format, ctow does not support -<option><var> (e.g. -bLOAD-COOKIE) there
// has to be a space in between option and var
let test_args = vec![
//"-bLOAD-COOKIE",
"-b LOAD-COOKIE",
//"-cSAVE-COOKIE",
"-c SAVE-COOKIE",
//"-dPOST-DATA",
"-d POST-DATA",
//"-eREFERER",
"-e REFERER",
"-g",
"-k",
//"-m999",
"-m 9999",
//"-oOUTPUT",
"-o OUTPUT",
//"-r1-2",
"-r 2-3",
"-s",
//"-uUSER",
"-u USER",
//"-zMODIFIED",
"-z MODIFIED",

//"-AAGENT",
"-A AGENT",
//"-C1",
"-C 2",
//"-ECERT",
"-E CERT",
"-H User-Agent: Example",
"-I",
//"-TBODY",
"-T BODY",
//"-XMETHOD",
"-X METHOD",

"--compressed",
"--connect-timeout 5",
"--retry 3",
];
let result_args = vec![
//"--load-cookies=LOAD-COOKIE",
"--load-cookies=LOAD-COOKIE",
//"--save-cookies=SAVE-COOKIE",
"--save-cookies=SAVE-COOKIE",
//"--post-data=POST-DATA",
"--post-data=POST-DATA",
//"--header=\"Referer: REFERER\"",
"--header=\"Referer: REFERER\"",
"--no-glob",
"--no-check-certificate",
//"--timeout=999",
"--timeout=9999",
//"--output-document=OUTPUT",
"--output-document=OUTPUT",
//"--header=\"Range: bytes=1-2\"",
"--header=\"Range: bytes=2-3\"",
"--quiet",
//"--user=USER",
"--user=USER",
//"--header=\"If-Modified-Since: MODIFIED\"",
"--header=\"If-Modified-Since: MODIFIED\"",

//"--header=\"User-Agent: AGENT\"",
"--header=\"User-Agent: AGENT\"",
//"--start-pos=1",
"--start-pos=2",
//"--certificate=CERT",
"--certificate=CERT",
"--header 'User-Agent: Example'",
"--method=HEAD",
//"--method=PUT --body-file=BODY",
"--method=PUT --body-file=BODY",
"--method=METHOD",

"--compression=auto",
"--timeout=5",
"--tries=3",
Expand Down

0 comments on commit c9c379f

Please sign in to comment.