gr/cli/
release.rs

1use clap::Parser;
2
3use crate::{cmds::release::ReleaseAssetListCliArgs, remote::ListRemoteCliArgs};
4
5use super::common::ListArgs;
6
7#[derive(Parser)]
8pub struct ReleaseCommand {
9    #[clap(subcommand)]
10    pub subcommand: ReleaseSubcommand,
11}
12
13#[derive(Parser)]
14pub enum ReleaseSubcommand {
15    #[clap(about = "List releases")]
16    List(ListArgs),
17    #[clap(subcommand, about = "Release assets operations")]
18    Assets(ReleaseAssetSubcommand),
19}
20
21#[derive(Parser)]
22pub enum ReleaseAssetSubcommand {
23    #[clap(about = "List release assets")]
24    List(ListAssets),
25}
26
27#[derive(Parser)]
28pub struct ListAssets {
29    /// Release ID (Github) or Release Tag (Gitlab)
30    #[clap()]
31    release_id: String,
32    #[command(flatten)]
33    list_args: ListArgs,
34}
35
36impl From<ReleaseCommand> for ReleaseOptions {
37    fn from(options: ReleaseCommand) -> Self {
38        match options.subcommand {
39            ReleaseSubcommand::List(options) => options.into(),
40            ReleaseSubcommand::Assets(subcommand) => match subcommand {
41                ReleaseAssetSubcommand::List(options) => ReleaseOptions::Assets(options.into()),
42            },
43        }
44    }
45}
46
47impl From<ListArgs> for ReleaseOptions {
48    fn from(args: ListArgs) -> Self {
49        ReleaseOptions::List(args.into())
50    }
51}
52
53impl From<ReleaseAssetSubcommand> for ReleaseAssetOptions {
54    fn from(subcommand: ReleaseAssetSubcommand) -> Self {
55        match subcommand {
56            ReleaseAssetSubcommand::List(options) => ReleaseAssetOptions::List(options.into()),
57        }
58    }
59}
60
61impl From<ListAssets> for ReleaseAssetOptions {
62    fn from(args: ListAssets) -> Self {
63        ReleaseAssetOptions::List(args.into())
64    }
65}
66
67impl From<ListAssets> for ReleaseAssetListCliArgs {
68    fn from(args: ListAssets) -> Self {
69        ReleaseAssetListCliArgs::builder()
70            .id(args.release_id)
71            .list_args(args.list_args.into())
72            .build()
73            .unwrap()
74    }
75}
76
77pub enum ReleaseOptions {
78    List(ListRemoteCliArgs),
79    Assets(ReleaseAssetOptions),
80}
81
82pub enum ReleaseAssetOptions {
83    List(ReleaseAssetListCliArgs),
84}
85
86#[cfg(test)]
87mod test {
88    use crate::cli::{Args, Command};
89
90    use super::*;
91
92    #[test]
93    fn test_release_cli_list() {
94        let args = Args::parse_from(vec![
95            "gr",
96            "rl",
97            "list",
98            "--from-page",
99            "1",
100            "--to-page",
101            "2",
102        ]);
103        let list_args = match args.command {
104            Command::Release(ReleaseCommand {
105                subcommand: ReleaseSubcommand::List(options),
106            }) => {
107                assert_eq!(options.from_page, Some(1));
108                assert_eq!(options.to_page, Some(2));
109                options
110            }
111            _ => panic!("Expected ReleaseCommand"),
112        };
113        let options: ReleaseOptions = list_args.into();
114        match options {
115            ReleaseOptions::List(args) => {
116                assert_eq!(args.from_page, Some(1));
117                assert_eq!(args.to_page, Some(2));
118            }
119            _ => panic!("Expected ReleaseOptions::List"),
120        }
121    }
122
123    #[test]
124    fn test_release_asset_cli_list() {
125        let args = Args::parse_from(vec![
126            "gr",
127            "rl",
128            "assets",
129            "list",
130            "1",
131            "--from-page",
132            "1",
133            "--to-page",
134            "2",
135        ]);
136        let list_args = match args.command {
137            Command::Release(ReleaseCommand {
138                subcommand: ReleaseSubcommand::Assets(ReleaseAssetSubcommand::List(options)),
139            }) => {
140                assert_eq!("1".to_string(), options.release_id);
141                assert_eq!(options.list_args.from_page, Some(1));
142                assert_eq!(options.list_args.to_page, Some(2));
143                options
144            }
145            _ => panic!("Expected ReleaseAssetSubcommand::List"),
146        };
147        let options: ReleaseAssetOptions = list_args.into();
148        match options {
149            ReleaseAssetOptions::List(args) => {
150                assert_eq!("1".to_string(), args.id);
151                assert_eq!(args.list_args.from_page, Some(1));
152                assert_eq!(args.list_args.to_page, Some(2));
153            }
154        }
155    }
156}