File tree Expand file tree Collapse file tree 2 files changed +36
-7
lines changed Expand file tree Collapse file tree 2 files changed +36
-7
lines changed Original file line number Diff line number Diff line change @@ -62,3 +62,27 @@ impl Display for Cookies {
62
62
)
63
63
}
64
64
}
65
+
66
+ /// Override cookies from environment variables
67
+ pub const LEETCODE_CSRF_ENV : & str = "LEETCODE_CSRF" ;
68
+ pub const LEETCODE_SESSION_ENV : & str = "LEETCODE_SESSION" ;
69
+ pub const LEETCODE_SITE_ENV : & str = "LEETCODE_SITE" ;
70
+
71
+ impl Cookies {
72
+ /// Load cookies from environment variables, overriding any existing values
73
+ /// if the environment variables are set.
74
+ pub fn with_env_override ( mut self ) -> Self {
75
+ if let Ok ( csrf) = std:: env:: var ( LEETCODE_CSRF_ENV ) {
76
+ self . csrf = csrf;
77
+ }
78
+ if let Ok ( session) = std:: env:: var ( LEETCODE_SESSION_ENV ) {
79
+ self . session = session;
80
+ }
81
+ if let Ok ( site) = std:: env:: var ( LEETCODE_SITE_ENV ) {
82
+ if let Ok ( leetcode_site) = LeetcodeSite :: from_str ( & site) {
83
+ self . site = leetcode_site;
84
+ }
85
+ }
86
+ self
87
+ }
88
+ }
Original file line number Diff line number Diff line change @@ -46,14 +46,19 @@ impl Config {
46
46
47
47
let s = fs:: read_to_string ( & conf) ?;
48
48
match toml:: from_str :: < Config > ( & s) {
49
- Ok ( config) => match config. cookies . site {
50
- cookies:: LeetcodeSite :: LeetcodeCom => Ok ( config) ,
51
- cookies:: LeetcodeSite :: LeetcodeCn => {
52
- let mut config = config;
53
- config. sys . urls = sys:: Urls :: new_with_leetcode_cn ( ) ;
54
- Ok ( config)
49
+ Ok ( mut config) => {
50
+ // Override config.cookies with environment variables
51
+ config. cookies = config. cookies . with_env_override ( ) ;
52
+
53
+ match config. cookies . site {
54
+ cookies:: LeetcodeSite :: LeetcodeCom => Ok ( config) ,
55
+ cookies:: LeetcodeSite :: LeetcodeCn => {
56
+ let mut config = config;
57
+ config. sys . urls = sys:: Urls :: new_with_leetcode_cn ( ) ;
58
+ Ok ( config)
59
+ }
55
60
}
56
- } ,
61
+ }
57
62
Err ( e) => {
58
63
let tmp = Self :: root ( ) ?. join ( "leetcode.tmp.toml" ) ;
59
64
Self :: write_default ( tmp) ?;
You can’t perform that action at this time.
0 commit comments