gr/cmds/
my.rs

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
use std::{io::Write, sync::Arc};

use crate::{
    api_traits::RemoteProject,
    cli::my::MyOptions,
    config::ConfigProperties,
    remote::{self, CacheType},
    Result,
};

use super::{
    common::{self, get_user},
    gist, merge_request,
    project::{ProjectListBodyArgs, ProjectListCliArgs},
};

pub fn execute(
    options: MyOptions,
    config: Arc<dyn ConfigProperties>,
    domain: String,
    path: String,
) -> Result<()> {
    match options {
        MyOptions::MergeRequest(cli_args) => {
            merge_request::list_merge_requests(domain, path, config, cli_args)
        }
        MyOptions::Project(cli_args) => {
            let user = get_user(&domain, &path, &config, &cli_args.list_args)?;
            let remote = remote::get_project(
                domain,
                path,
                config,
                Some(&cli_args.list_args.get_args.cache_args),
                CacheType::File,
            )?;
            let from_to_args = remote::validate_from_to_page(&cli_args.list_args)?;
            let body_args = ProjectListBodyArgs::builder()
                .from_to_page(from_to_args)
                .user(Some(user))
                .stars(cli_args.stars)
                .build()?;
            if cli_args.list_args.num_pages {
                return common::num_project_pages(remote, body_args, std::io::stdout());
            }
            if cli_args.list_args.num_resources {
                return common::num_project_resources(remote, body_args, std::io::stdout());
            }
            list_user_projects(remote, body_args, cli_args, std::io::stdout())
        }
        MyOptions::Gist(cli_args) => {
            let remote = remote::get_gist(
                domain,
                path,
                config,
                Some(&cli_args.list_args.get_args.cache_args),
                CacheType::File,
            )?;
            if cli_args.list_args.num_pages {
                return common::num_user_gists(remote, std::io::stdout());
            }
            if cli_args.list_args.num_resources {
                return common::num_user_gist_resources(remote, std::io::stdout());
            }
            let from_to_args = remote::validate_from_to_page(&cli_args.list_args)?;
            let body_args = gist::GistListBodyArgs::builder()
                .body_args(from_to_args)
                .build()?;
            gist::list_user_gists(remote, body_args, cli_args, std::io::stdout())
        }
    }
}

fn list_user_projects<W: Write>(
    remote: Arc<dyn RemoteProject>,
    body_args: ProjectListBodyArgs,
    cli_args: ProjectListCliArgs,
    writer: W,
) -> Result<()> {
    common::list_user_projects(remote, body_args, cli_args, writer)
}

#[cfg(test)]
mod tests {

    use crate::cmds::project::{Member, Project, ProjectListCliArgs};

    use self::remote::ListRemoteCliArgs;

    use super::*;

    struct MockGitlab {
        projects: Vec<Project>,
    }

    impl MockGitlab {
        fn new(projects: Vec<Project>) -> Self {
            MockGitlab { projects }
        }
    }

    impl RemoteProject for MockGitlab {
        fn list(&self, _args: ProjectListBodyArgs) -> Result<Vec<Project>> {
            Ok(self.projects.clone())
        }

        fn get_project_data(
            &self,
            _id: Option<i64>,
            _path: Option<&str>,
        ) -> Result<crate::io::CmdInfo> {
            todo!()
        }

        fn get_project_members(&self) -> Result<crate::io::CmdInfo> {
            todo!()
        }

        fn get_url(&self, _option: crate::cli::browse::BrowseOptions) -> String {
            todo!()
        }

        fn num_pages(&self, _args: ProjectListBodyArgs) -> Result<Option<u32>> {
            todo!()
        }

        fn num_resources(
            &self,
            _args: ProjectListBodyArgs,
        ) -> Result<Option<crate::api_traits::NumberDeltaErr>> {
            todo!()
        }
    }

    #[test]
    fn test_list_current_user_projects() {
        let projects = vec![Project::new(1, "main"), Project::new(2, "dev")];
        let user_id = 1;
        let cli_args = ProjectListCliArgs::builder()
            .list_args(ListRemoteCliArgs::builder().build().unwrap())
            .build()
            .unwrap();
        let body_args = ProjectListBodyArgs::builder()
            .from_to_page(None)
            .user(Some(
                Member::builder()
                    .id(user_id)
                    .name("jordi".to_string())
                    .username("jordilin".to_string())
                    .build()
                    .unwrap(),
            ))
            .build()
            .unwrap();
        let mut buffer = Vec::new();
        assert!(list_user_projects(
            Arc::new(MockGitlab::new(projects)),
            body_args,
            cli_args,
            &mut buffer,
        )
        .is_ok());
        assert_eq!(
            "ID|Default Branch|URL|Created at\n1|main||\n2|dev||\n",
            String::from_utf8(buffer).unwrap()
        );
    }
}