orion-accessor 0.6.0

Unified accessor layer for HTTP, Git, and local resources with redirect rules, proxy control, and env-aware templating.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::prelude::*;
use getset::{Getters, Setters, WithSetters};
use home::home_dir;

///
/// 支持通过SSH和HTTPS协议访问Git仓库
///
/// # Token认证示例
///

#[derive(Clone, Debug, Serialize, Deserialize, Default, Getters, Setters, WithSetters)]
#[getset(get = "pub", set = "pub")]
#[serde(rename = "git")]
pub struct GitRepository {
    repo: String,
    /// 额外资源标识,例如特定子模块或配置文件
    #[serde(rename = "res", skip_serializing_if = "Option::is_none")]
    resource: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tag: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    branch: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    rev: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    path: Option<String>,
    // 新增:SSH私钥路径
    #[serde(skip_serializing_if = "Option::is_none")]
    ssh_key: Option<String>,
    // 新增:SSH密钥密码
    #[serde(skip_serializing_if = "Option::is_none")]
    ssh_passphrase: Option<String>,
    // 新增:Token认证(用于HTTPS协议)
    #[serde(skip_serializing_if = "Option::is_none")]
    token: Option<String>,
    // 新增:用户名(用于Token认证)
    #[serde(skip_serializing_if = "Option::is_none")]
    username: Option<String>,
}

impl PartialEq for GitRepository {
    fn eq(&self, other: &Self) -> bool {
        self.repo == other.repo
    }
}
impl EnvEvalable<GitRepository> for GitRepository {
    fn env_eval(self, dict: &EnvDict) -> GitRepository {
        Self {
            repo: self.repo.env_eval(dict),
            resource: self.resource.env_eval(dict),
            tag: self.tag.env_eval(dict),
            branch: self.branch.env_eval(dict),
            rev: self.rev.env_eval(dict),
            path: self.path.env_eval(dict),
            ssh_key: self.ssh_key.env_eval(dict),
            ssh_passphrase: self.ssh_passphrase.env_eval(dict),
            token: self.token.env_eval(dict),
            username: self.username.env_eval(dict),
        }
    }
}

impl GitRepository {
    fn set_option_field(
        mut self,
        slot: impl FnOnce(&mut Self) -> &mut Option<String>,
        value: Option<String>,
    ) -> Self {
        *slot(&mut self) = value;
        self
    }

    pub fn from<S: Into<String>>(repo: S) -> Self {
        Self {
            repo: repo.into(),
            ..Default::default()
        }
    }
    pub fn with_tag<S: Into<String>>(self, tag: S) -> Self {
        self.with_opt_tag(Some(tag.into()))
    }
    pub fn with_opt_tag(self, tag: Option<String>) -> Self {
        self.set_option_field(|repo| &mut repo.tag, tag)
    }
    pub fn with_branch<S: Into<String>>(self, branch: S) -> Self {
        self.with_opt_branch(Some(branch.into()))
    }
    pub fn with_opt_branch(self, branch: Option<String>) -> Self {
        self.set_option_field(|repo| &mut repo.branch, branch)
    }
    pub fn with_rev<S: Into<String>>(mut self, rev: S) -> Self {
        self.rev = Some(rev.into());
        self
    }
    pub fn with_path<S: Into<String>>(mut self, path: S) -> Self {
        self.path = Some(path.into());
        self
    }
    // 新增:设置SSH私钥
    pub fn with_ssh_key<S: Into<String>>(mut self, ssh_key: S) -> Self {
        self.ssh_key = Some(ssh_key.into());
        self
    }
    // 新增:设置SSH密钥密码
    pub fn with_ssh_passphrase<S: Into<String>>(mut self, ssh_passphrase: S) -> Self {
        self.ssh_passphrase = Some(ssh_passphrase.into());
        self
    }
    // 新增:设置Token认证
    pub fn with_token<S: Into<String>>(self, token: S) -> Self {
        self.with_opt_token(Some(token.into()))
    }
    // 新增:设置用户名(用于Token认证)
    pub fn with_username<S: Into<String>>(self, username: S) -> Self {
        self.with_opt_username(Some(username.into()))
    }
    // 新增:设置Token认证(可选)
    pub fn with_opt_token(self, token: Option<String>) -> Self {
        self.set_option_field(|repo| &mut repo.token, token)
    }
    // 新增:设置用户名(可选)
    pub fn with_opt_username(self, username: Option<String>) -> Self {
        self.set_option_field(|repo| &mut repo.username, username)
    }

    /// 为GitHub设置Token认证(便捷方法)
    /// GitHub使用用户名+Token作为密码的方式
    pub fn with_github_token<S: Into<String>>(self, token: S) -> Self {
        let token = token.into();
        self.with_opt_username(Some("git".to_string()))
            .with_opt_token(Some(token))
    }

    /// 为GitLab设置Token认证(便捷方法)
    /// GitLab可以使用"oauth2"作为用户名,Token作为密码
    pub fn with_gitlab_token<S: Into<String>>(self, token: S) -> Self {
        let token = token.into();
        self.with_opt_username(Some("oauth2".to_string()))
            .with_opt_token(Some(token))
    }

    /// 为Gitea设置Token认证(便捷方法)
    /// Gitea可以使用Token作为密码
    pub fn with_gitea_token<S: Into<String>>(self, token: S) -> Self {
        let token = token.into();
        self.with_opt_username(Some("git".to_string()))
            .with_opt_token(Some(token))
    }

    /// 从环境变量读取Token认证
    ///
    /// # Arguments
    /// * `env_var` - 环境变量名,例如 "GITHUB_TOKEN"
    pub fn with_env_token(self, env_var: &str) -> Self {
        match std::env::var(env_var) {
            Ok(token) => self.with_opt_token(Some(token)),
            Err(_) => self,
        }
    }

    /// 从环境变量读取GitHub Token认证
    pub fn with_github_env_token(self) -> Self {
        self.with_env_token("GITHUB_TOKEN")
    }

    /// 从环境变量读取GitLab Token认证
    pub fn with_gitlab_env_token(self) -> Self {
        match std::env::var("GITLAB_TOKEN") {
            Ok(token) => self
                .with_opt_username(Some("oauth2".to_string()))
                .with_opt_token(Some(token)),
            Err(_) => self,
        }
    }

    /// 从环境变量读取Gitea Token认证
    pub fn with_gitea_env_token(self) -> Self {
        self.with_env_token("GITEA_TOKEN")
    }

    /// 从~/.git-credentials文件读取token
    pub fn with_git_credentials(mut self) -> Self {
        if let Some(credentials) = Self::read_git_credentials() {
            for (url, username, token) in credentials {
                if self.repo.starts_with(&url) {
                    self = self
                        .with_opt_username(Some(username))
                        .with_opt_token(Some(token));
                    break;
                }
            }
        }
        self
    }
    /// 读取~/.git-credentials文件
    pub fn read_git_credentials() -> Option<Vec<(String, String, String)>> {
        let home = home_dir()?;
        let credentials_path = home.join(".git-credentials");
        Self::read_git_credentials_at(&credentials_path)
    }

    fn read_git_credentials_at(path: &Path) -> Option<Vec<(String, String, String)>> {
        use std::fs;
        use std::io::{BufRead, BufReader};

        if !path.exists() {
            return None;
        }

        let file = fs::File::open(path).ok()?;
        let reader = BufReader::new(file);
        let mut credentials = Vec::new();

        for line in reader.lines().map_while(Result::ok) {
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }

            if let Ok(url) = url::Url::parse(line) {
                let host = url.host_str()?;
                let scheme = url.scheme();
                let path = url.path();

                let base_url = format!("{scheme}://{host}{path}");

                let username = url.username();
                if !username.is_empty()
                    && let Some(password) = url.password()
                {
                    credentials.push((base_url, username.to_string(), password.to_string()));
                }
            }
        }

        if credentials.is_empty() {
            None
        } else {
            Some(credentials)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_git_repository_from() {
        let repo = GitRepository::from("https://github.com/user/repo.git");
        assert_eq!(repo.repo(), "https://github.com/user/repo.git");
        assert!(repo.tag().is_none());
        assert!(repo.branch().is_none());
        assert!(repo.token().is_none());
    }

    #[test]
    fn test_git_repository_with_tag() {
        let repo = GitRepository::from("https://github.com/user/repo.git").with_tag("v1.0.0");
        assert_eq!(repo.tag().as_ref(), Some(&"v1.0.0".to_string()));
    }

    #[test]
    fn test_git_repository_with_opt_tag() {
        let repo1 = GitRepository::from("https://github.com/user/repo.git")
            .with_opt_tag(Some("v1.0.0".to_string()));
        assert_eq!(repo1.tag().as_ref(), Some(&"v1.0.0".to_string()));

        let repo2 = GitRepository::from("https://github.com/user/repo.git").with_opt_tag(None);
        assert!(repo2.tag().is_none());
    }

    #[test]
    fn test_git_repository_with_branch() {
        let repo = GitRepository::from("https://github.com/user/repo.git").with_branch("main");
        assert_eq!(repo.branch().as_ref(), Some(&"main".to_string()));
    }

    #[test]
    fn test_git_repository_with_opt_branch() {
        let repo1 = GitRepository::from("https://github.com/user/repo.git")
            .with_opt_branch(Some("main".to_string()));
        assert_eq!(repo1.branch().as_ref(), Some(&"main".to_string()));

        let repo2 = GitRepository::from("https://github.com/user/repo.git").with_opt_branch(None);
        assert!(repo2.branch().is_none());
    }

    #[test]
    fn test_git_repository_with_rev() {
        let repo = GitRepository::from("https://github.com/user/repo.git").with_rev("abc123");
        assert_eq!(repo.rev().as_ref(), Some(&"abc123".to_string()));
    }

    #[test]
    fn test_git_repository_with_path() {
        let repo = GitRepository::from("https://github.com/user/repo.git").with_path("subdir");
        assert_eq!(repo.path().as_ref(), Some(&"subdir".to_string()));
    }

    #[test]
    fn test_git_repository_with_ssh_key() {
        let repo = GitRepository::from("git@github.com:user/repo.git").with_ssh_key("/path/to/key");
        assert_eq!(repo.ssh_key().as_ref(), Some(&"/path/to/key".to_string()));
    }

    #[test]
    fn test_git_repository_with_ssh_passphrase() {
        let repo =
            GitRepository::from("git@github.com:user/repo.git").with_ssh_passphrase("secret");
        assert_eq!(repo.ssh_passphrase().as_ref(), Some(&"secret".to_string()));
    }

    #[test]
    fn test_git_repository_with_token() {
        let repo = GitRepository::from("https://github.com/user/repo.git").with_token("token123");
        assert_eq!(repo.token().as_ref(), Some(&"token123".to_string()));
    }

    #[test]
    fn test_git_repository_with_username() {
        let repo = GitRepository::from("https://github.com/user/repo.git").with_username("user");
        assert_eq!(repo.username().as_ref(), Some(&"user".to_string()));
    }

    #[test]
    fn test_git_repository_with_opt_token() {
        let repo1 = GitRepository::from("https://github.com/user/repo.git")
            .with_opt_token(Some("token123".to_string()));
        assert_eq!(repo1.token().as_ref(), Some(&"token123".to_string()));

        let repo2 = GitRepository::from("https://github.com/user/repo.git").with_opt_token(None);
        assert!(repo2.token().is_none());
    }

    #[test]
    fn test_git_repository_with_opt_username() {
        let repo1 = GitRepository::from("https://github.com/user/repo.git")
            .with_opt_username(Some("user".to_string()));
        assert_eq!(repo1.username().as_ref(), Some(&"user".to_string()));

        let repo2 = GitRepository::from("https://github.com/user/repo.git").with_opt_username(None);
        assert!(repo2.username().is_none());
    }

    #[test]
    fn test_git_repository_with_github_token() {
        let repo =
            GitRepository::from("https://github.com/user/repo.git").with_github_token("ghp_token");
        assert_eq!(repo.username().as_ref(), Some(&"git".to_string()));
        assert_eq!(repo.token().as_ref(), Some(&"ghp_token".to_string()));
    }

    #[test]
    fn test_git_repository_with_gitlab_token() {
        let repo = GitRepository::from("https://gitlab.com/user/repo.git")
            .with_gitlab_token("glpat_token");
        assert_eq!(repo.username().as_ref(), Some(&"oauth2".to_string()));
        assert_eq!(repo.token().as_ref(), Some(&"glpat_token".to_string()));
    }

    #[test]
    fn test_git_repository_with_gitea_token() {
        let repo =
            GitRepository::from("https://gitea.com/user/repo.git").with_gitea_token("gitea_token");
        assert_eq!(repo.username().as_ref(), Some(&"git".to_string()));
        assert_eq!(repo.token().as_ref(), Some(&"gitea_token".to_string()));
    }

    #[test]
    fn test_read_git_credentials_valid_file() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "https://user:token@github.com").unwrap();
        writeln!(temp_file, "https://oauth2:token@gitlab.com/user/repo.git").unwrap();
        writeln!(temp_file, "# This is a comment").unwrap();
        // 移除空字符串的writeln!调用
        temp_file.flush().unwrap();

        let credentials = GitRepository::read_git_credentials_from_path(temp_file.path());
        assert!(credentials.is_some());
        let creds = credentials.unwrap();
        assert_eq!(creds.len(), 2);

        assert_eq!(creds[0].0, "https://github.com/");
        assert_eq!(creds[0].1, "user");
        assert_eq!(creds[0].2, "token");

        assert_eq!(creds[1].0, "https://gitlab.com/user/repo.git");
        assert_eq!(creds[1].1, "oauth2");
        assert_eq!(creds[1].2, "token");
    }

    #[test]
    fn test_read_git_credentials_empty_file() {
        let temp_file = NamedTempFile::new().unwrap();
        let credentials = GitRepository::read_git_credentials_from_path(temp_file.path());
        assert!(credentials.is_none());
    }

    #[test]
    fn test_read_git_credentials_invalid_url() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "invalid-url").unwrap();
        temp_file.flush().unwrap();

        let credentials = GitRepository::read_git_credentials_from_path(temp_file.path());
        assert!(credentials.is_none());
    }

    #[test]
    fn test_git_repository_with_git_credentials() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "https://user:token@github.com/user/repo.git").unwrap();
        temp_file.flush().unwrap();

        let repo = GitRepository::from("https://github.com/user/repo.git")
            .with_git_credentials_from_path(temp_file.path());

        assert_eq!(repo.username().as_ref(), Some(&"user".to_string()));
        assert_eq!(repo.token().as_ref(), Some(&"token".to_string()));
    }

    #[test]
    fn test_git_repository_with_git_credentials_no_match() {
        let mut temp_file = NamedTempFile::new().unwrap();
        writeln!(temp_file, "https://user:token@gitlab.com/user/repo.git").unwrap();
        temp_file.flush().unwrap();

        let repo = GitRepository::from("https://github.com/user/repo.git")
            .with_git_credentials_from_path(temp_file.path());

        assert!(repo.username().is_none());
        assert!(repo.token().is_none());
    }

    // Helper methods for testing
    impl GitRepository {
        fn read_git_credentials_from_path(
            path: &std::path::Path,
        ) -> Option<Vec<(String, String, String)>> {
            Self::read_git_credentials_at(path)
        }

        fn with_git_credentials_from_path(mut self, path: &std::path::Path) -> Self {
            if let Some(credentials) = Self::read_git_credentials_at(path) {
                for (url, username, token) in credentials {
                    if self.repo.starts_with(&url) {
                        self.username = Some(username);
                        self.token = Some(token);
                        break;
                    }
                }
            }
            self
        }
    }
}