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
use wasmer_backend_api::types::{DeployAppVersionsSortBy, GetDeployAppVersionsVars};

use crate::{
    commands::{app::util::AppIdentOpts, AsyncCliCommand},
    config::WasmerEnv,
    opts::ListFormatOpts,
};

/// List versions of an app.
#[derive(clap::Parser, Debug)]
pub struct CmdAppVersionList {
    #[clap(flatten)]
    pub env: WasmerEnv,

    #[allow(missing_docs)]
    #[clap(flatten)]
    pub fmt: ListFormatOpts,

    /// Get all versions of the app.
    /// Overrides pagination flags (--max, --offset).
    #[clap(short = 'a', long)]
    all: bool,

    /// Pagination offset - get versions after this offset.
    ///
    /// See also: --max, --before, --after
    #[clap(long)]
    offset: Option<u32>,

    /// Maximum number of items to return.
    ///
    /// See also: --offset, --before, --after
    #[clap(long)]
    max: Option<u32>,

    /// Pagination cursor - get versions before this version.
    ///
    /// See also: --max, --offset, --after
    #[clap(long)]
    before: Option<String>,

    /// Pagination cursor - get versions after this version.
    ///
    /// See also: --max, --offset, --before
    #[clap(long)]
    after: Option<String>,

    #[clap(long)]
    sort: Option<Sort>,

    #[clap(flatten)]
    #[allow(missing_docs)]
    pub ident: AppIdentOpts,
}

#[derive(Clone, Copy, clap::ValueEnum, Debug)]
enum Sort {
    Newest,
    Oldest,
}

impl From<Sort> for DeployAppVersionsSortBy {
    fn from(val: Sort) -> Self {
        match val {
            Sort::Newest => DeployAppVersionsSortBy::Newest,
            Sort::Oldest => DeployAppVersionsSortBy::Oldest,
        }
    }
}

#[async_trait::async_trait]
impl AsyncCliCommand for CmdAppVersionList {
    type Output = ();

    async fn run_async(self) -> Result<(), anyhow::Error> {
        let client = self.env.client()?;
        let (_ident, app) = self.ident.load_app(&client).await?;

        let versions = if self.all {
            wasmer_backend_api::query::all_app_versions(&client, app.owner.global_name, app.name)
                .await?
        } else {
            let vars = GetDeployAppVersionsVars {
                owner: app.owner.global_name,
                name: app.name,
                offset: self.offset.map(|x| x as i32),
                before: self.before,
                after: self.after,
                first: self.max.map(|x| x as i32),
                last: None,
                sort_by: self.sort.map(|x| x.into()),
            };

            let versions =
                wasmer_backend_api::query::get_deploy_app_versions(&client, vars.clone()).await?;

            versions
                .edges
                .into_iter()
                .flatten()
                .filter_map(|edge| edge.node)
                .collect::<Vec<_>>()
        };

        println!("{}", self.fmt.format.render(&versions));

        Ok(())
    }
}