Skip to content

refactor(config): make config more simple #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 24, 2023
Merged
11 changes: 0 additions & 11 deletions .cargo-husky/hooks/pre-commit

This file was deleted.

60 changes: 41 additions & 19 deletions Cargo.lock

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

10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ path = "src/bin/lc.rs"

[package]
name = "leetcode-cli"
version = "0.3.13"
authors = ["clearloop <cdr.today@foxmail.com>"]
version = "0.4.0"
authors = ["clearloop <tianyi.gc@gmail.com>"]
edition = "2021"
description = "Leet your code in command-line."
repository = "https://github.com/clearloop/leetcode-cli"
@@ -32,6 +32,7 @@ serde_json = "1.0.82"
toml = "0.5.9"
regex = "1.6.0"
scraper = "0.13.0"
anyhow = "1.0.71"

[dependencies.diesel]
version = "2.0.3"
@@ -41,11 +42,6 @@ features = ["sqlite"]
version = "0.11.11"
features = ["gzip", "json"]

[dev-dependencies.cargo-husky]
version = "1.5.0"
default-features = false
features = ["precommit-hook", "user-hooks"]

[features]
pym = ["pyo3"]

44 changes: 27 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# leetcode-cli
![Rust](https://github.com/clearloop/leetcode-cli/workflows/Rust/badge.svg)

![Rust](https://github.com/clearloop/leetcode-cli/workflows/leetcode-cli/badge.svg)
[![crate](https://img.shields.io/crates/v/leetcode-cli.svg)](https://crates.io/crates/leetcode-cli)
[![doc](https://img.shields.io/badge/current-docs-brightgreen.svg)](https://docs.rs/leetcode-cli/)
[![downloads](https://img.shields.io/crates/d/leetcode-cli.svg)](https://crates.io/crates/leetcode-cli)
[![gitter](https://img.shields.io/gitter/room/odditypark/leetcode-cli)](https://gitter.im/Odditypark/leetcode-cli)
[![telegram](https://img.shields.io/badge/telegram-blue?logo=telegram)](https://t.me/+U_5si6PhWykxZTI1)
[![LICENSE](https://img.shields.io/crates/l/leetcode-cli.svg)](https://choosealicense.com/licenses/mit/)

## Installing

```sh
# Required dependencies:
#
#
# gcc
# libssl-dev
# libdbus-1-dev
@@ -48,14 +49,24 @@ SUBCOMMANDS:

## Example

For example, given this config (can be found in `~/.leetcode/leetcode.toml`, it can be generated automatically with command: `leetcode list` if you are a new user):
For example, given this config (could be found at `~/.leetcode/leetcode.toml`):

```toml
[code]
lang = "rust"
editor = "emacs"
editor = emacs
# Optional parameter
editor_args = ['-nw']
editor-args = ['-nw']
lang = 'rust'

[cookies]
csrf = '<your-leetcode-csrf-token>'
session = '<your-leetcode-session-key>'

[storage]
cache = 'Problems'
code = 'code'
root = '~/.leetcode'
scripts = 'scripts'
```

#### 1. <kbd>pick</kbd>
@@ -144,7 +155,8 @@ leetcode exec 1

## Cookies

The cookie plugin of leetcode-cli can work on OSX and [Linux][#1]. **If you are on a different platform, there are problems with caching the cookies**, you can manually input your LeetCode Cookies to the configuration file.
The cookie plugin of leetcode-cli can work on OSX and [Linux][#1]. **If you are on a different platform, there are problems with caching the cookies**,
you can manually input your LeetCode Cookies to the configuration file.

```toml
[cookies]
@@ -154,7 +166,6 @@ session = "..."

For Example, using Chrome (after logging in to LeetCode):


#### Step 1

Open Chrome and navigate to the link below:
@@ -166,6 +177,7 @@ chrome://settings/cookies/detail?site=leetcode.com
#### Step 2

Copy `Content` from `LEETCODE_SESSION` and `csrftoken` to `session` and `csrf` in your configuration file, respectively:

```toml
[cookies]
csrf = "${csrftoken}"
@@ -189,13 +201,13 @@ import json;

def plan(sps, stags):
##
# `print` in python is supported,
# if you want to know the data structures of these two args,
# `print` in python is supported,
# if you want to know the data structures of these two args,
# just print them
##
problems = json.loads(sps)
tags = json.loads(stags)

ret = []
tm = {}
for tag in tags:
@@ -215,17 +227,15 @@ Then run `list` with the filter that you just wrote:
leetcode list -p plan1
```

And that's it! Enjoy!
That's it! Enjoy!

## Contributions

## PR

[PRs][pr] are more than welcome!
Feel free to add your names and emails in the `authors` field of `Cargo.toml` !

## LICENSE

MIT


[pr]: https://github.com/clearloop/leetcode-cli/pulls
[#1]: https://github.com/clearloop/leetcode-cli/issues/1
41 changes: 14 additions & 27 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@ use self::models::*;
use self::schemas::{problems::dsl::*, tags::dsl::*};
use self::sql::*;
use crate::helper::test_cases_path;
use crate::{cfg, err::Error, plugins::LeetCode};
use crate::{config::Config, err::Error, plugins::LeetCode};
use anyhow::anyhow;
use colored::Colorize;
use diesel::prelude::*;
use reqwest::Response;
@@ -309,33 +310,14 @@ impl Cache {
json.insert("data_input", test_case);

let url = match run {
Run::Test => conf
.sys
.urls
.get("test")
.ok_or(Error::NoneError)?
.replace("$slug", &p.slug),
Run::Test => conf.sys.urls.test(&p.slug),
Run::Submit => {
json.insert("judge_type", "large".to_string());
conf.sys
.urls
.get("submit")
.ok_or(Error::NoneError)?
.replace("$slug", &p.slug)
conf.sys.urls.submit(&p.slug)
}
};

Ok((
json,
[
url,
conf.sys
.urls
.get("problems")
.ok_or(Error::NoneError)?
.replace("$slug", &p.slug),
],
))
Ok((json, [url, conf.sys.urls.problem(&p.slug)]))
}

/// TODO: The real delay
@@ -361,15 +343,20 @@ impl Cache {
) -> Result<VerifyResult, Error> {
trace!("Exec problem filter —— Test or Submit");
let (json, [url, refer]) = self.pre_run_code(run.clone(), rfid, test_case).await?;
trace!("Pre run code result {:?}, {:?}, {:?}", json, url, refer);
trace!("Pre run code result {:#?}, {}, {}", json, url, refer);

let run_res: RunCode = self
let text = self
.0
.clone()
.run_code(json.clone(), url.clone(), refer.clone())
.await?
.json() // does not require LEETCODE_SESSION (very oddly)
.text()
.await?;

let run_res: RunCode = serde_json::from_str(&text).map_err(|e| {
anyhow!("json error: {e}, plz double check your session and csrf config.")
})?;

trace!("Run code result {:#?}", run_res);

// Check if leetcode accepted the Run request
@@ -397,7 +384,7 @@ impl Cache {

/// New cache
pub fn new() -> Result<Self, Error> {
let conf = cfg::locate()?;
let conf = Config::locate()?;
let mut c = conn(conf.storage.cache()?);
diesel::sql_query(CREATE_PROBLEMS_IF_NOT_EXISTS).execute(&mut c)?;
diesel::sql_query(CREATE_TAGS_IF_NOT_EXISTS).execute(&mut c)?;
1 change: 1 addition & 0 deletions src/cache/models.rs
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ impl Problem {
_ => "Unknown",
}
}

pub fn desc_comment(&self, conf: &Config) -> String {
let mut res = String::new();
let comment_leading = &conf.code.comment_leading;
Loading