@@ -28,27 +28,45 @@ impl StringOrVec {
2828#[ derive( Debug , Default , Deserialize ) ]
2929pub struct Config {
3030 pub github_api_proxy : Option < StringOrVec > ,
31+ pub gitlab_api_proxy : Option < StringOrVec > ,
32+ pub codeberg_api_proxy : Option < StringOrVec > ,
3133 pub remove_old : Option < bool > ,
3234 pub output_dir : Option < PathBuf > ,
3335}
3436
3537static CONFIG : OnceLock < Config > = OnceLock :: new ( ) ;
36- static PROXIES : OnceLock < Vec < String > > = OnceLock :: new ( ) ;
38+ static GITHUB_PROXIES : OnceLock < Vec < String > > = OnceLock :: new ( ) ;
39+ static GITLAB_PROXIES : OnceLock < Vec < String > > = OnceLock :: new ( ) ;
40+ static CODEBERG_PROXIES : OnceLock < Vec < String > > = OnceLock :: new ( ) ;
3741
3842pub fn init ( ) {
3943 let config = load_config ( ) ;
4044 let _ = CONFIG . set ( config) ;
4145}
4246
43- pub fn set_proxies ( proxies : Vec < String > ) {
47+ pub fn set_github_proxies ( proxies : Vec < String > ) {
4448 if !proxies. is_empty ( ) {
45- let _ = PROXIES . set ( proxies) ;
49+ let _ = GITHUB_PROXIES . set ( proxies) ;
50+ }
51+ }
52+
53+ pub fn set_gitlab_proxies ( proxies : Vec < String > ) {
54+ if !proxies. is_empty ( ) {
55+ let _ = GITLAB_PROXIES . set ( proxies) ;
56+ }
57+ }
58+
59+ pub fn set_codeberg_proxies ( proxies : Vec < String > ) {
60+ if !proxies. is_empty ( ) {
61+ let _ = CODEBERG_PROXIES . set ( proxies) ;
4662 }
4763}
4864
4965pub fn get ( ) -> & ' static Config {
5066 static DEFAULT : Config = Config {
5167 github_api_proxy : None ,
68+ gitlab_api_proxy : None ,
69+ codeberg_api_proxy : None ,
5270 remove_old : None ,
5371 output_dir : None ,
5472 } ;
@@ -80,20 +98,38 @@ fn try_load_config(path: PathBuf) -> Option<Config> {
8098 . and_then ( |content| toml:: from_str ( & content) . ok ( ) )
8199}
82100
83- pub fn get_proxies ( ) -> Vec < String > {
84- if let Some ( proxies) = PROXIES . get ( ) {
101+ pub fn get_github_proxies ( ) -> Vec < String > {
102+ get_forge_proxies ( & GITHUB_PROXIES , "GITHUB_API_PROXY" , |c| {
103+ c. github_api_proxy . as_ref ( )
104+ } )
105+ }
106+
107+ pub fn get_gitlab_proxies ( ) -> Vec < String > {
108+ get_forge_proxies ( & GITLAB_PROXIES , "GITLAB_API_PROXY" , |c| {
109+ c. gitlab_api_proxy . as_ref ( )
110+ } )
111+ }
112+
113+ pub fn get_codeberg_proxies ( ) -> Vec < String > {
114+ get_forge_proxies ( & CODEBERG_PROXIES , "CODEBERG_API_PROXY" , |c| {
115+ c. codeberg_api_proxy . as_ref ( )
116+ } )
117+ }
118+
119+ fn get_forge_proxies (
120+ cli_proxies : & OnceLock < Vec < String > > ,
121+ env_var : & str ,
122+ config_field : impl FnOnce ( & Config ) -> Option < & StringOrVec > ,
123+ ) -> Vec < String > {
124+ if let Some ( proxies) = cli_proxies. get ( ) {
85125 return proxies. clone ( ) ;
86126 }
87127
88- if let Ok ( s) = std:: env:: var ( "GITHUB_API_PROXY" ) {
128+ if let Ok ( s) = std:: env:: var ( env_var ) {
89129 return parse_proxies ( & s) ;
90130 }
91131
92- get ( )
93- . github_api_proxy
94- . as_ref ( )
95- . map ( |v| v. to_vec ( ) )
96- . unwrap_or_default ( )
132+ config_field ( get ( ) ) . map ( |v| v. to_vec ( ) ) . unwrap_or_default ( )
97133}
98134
99135pub fn get_remove_old ( cli_value : Option < bool > ) -> bool {
@@ -131,13 +167,3 @@ fn parse_proxies(s: &str) -> Vec<String> {
131167 . filter ( |s| !s. is_empty ( ) )
132168 . collect ( )
133169}
134-
135- pub fn build_api_url ( path : & str , proxy : Option < & str > ) -> String {
136- match proxy {
137- Some ( proxy) => {
138- let proxy = proxy. trim_end_matches ( '/' ) ;
139- format ! ( "{}{}" , proxy, path)
140- }
141- None => format ! ( "https://api.github.com{}" , path) ,
142- }
143- }
0 commit comments