wasmer_cli/commands/app/version/
list.rs

1use wasmer_backend_api::types::{DeployAppVersionsSortBy, GetDeployAppVersionsVars};
2
3use crate::{
4    commands::{AsyncCliCommand, app::util::AppIdentOpts},
5    config::WasmerEnv,
6    opts::ListFormatOpts,
7};
8
9/// List versions of an app.
10#[derive(clap::Parser, Debug)]
11pub struct CmdAppVersionList {
12    #[clap(flatten)]
13    pub env: WasmerEnv,
14
15    #[allow(missing_docs)]
16    #[clap(flatten)]
17    pub fmt: ListFormatOpts,
18
19    /// Get all versions of the app.
20    /// Overrides pagination flags (--max, --offset).
21    #[clap(short = 'a', long)]
22    all: bool,
23
24    /// Pagination offset - get versions after this offset.
25    ///
26    /// See also: --max, --before, --after
27    #[clap(long)]
28    offset: Option<u32>,
29
30    /// Maximum number of items to return.
31    ///
32    /// See also: --offset, --before, --after
33    #[clap(long)]
34    max: Option<u32>,
35
36    /// Pagination cursor - get versions before this version.
37    ///
38    /// See also: --max, --offset, --after
39    #[clap(long)]
40    before: Option<String>,
41
42    /// Pagination cursor - get versions after this version.
43    ///
44    /// See also: --max, --offset, --before
45    #[clap(long)]
46    after: Option<String>,
47
48    #[clap(long)]
49    sort: Option<Sort>,
50
51    #[clap(flatten)]
52    #[allow(missing_docs)]
53    pub ident: AppIdentOpts,
54}
55
56#[derive(Clone, Copy, clap::ValueEnum, Debug)]
57enum Sort {
58    Newest,
59    Oldest,
60}
61
62impl From<Sort> for DeployAppVersionsSortBy {
63    fn from(val: Sort) -> Self {
64        match val {
65            Sort::Newest => DeployAppVersionsSortBy::Newest,
66            Sort::Oldest => DeployAppVersionsSortBy::Oldest,
67        }
68    }
69}
70
71#[async_trait::async_trait]
72impl AsyncCliCommand for CmdAppVersionList {
73    type Output = ();
74
75    async fn run_async(self) -> Result<(), anyhow::Error> {
76        let client = self.env.client()?;
77        let (_ident, app) = self.ident.load_app(&client).await?;
78
79        let versions = if self.all {
80            wasmer_backend_api::query::all_app_versions(&client, app.owner.global_name, app.name)
81                .await?
82        } else {
83            let vars = GetDeployAppVersionsVars {
84                owner: app.owner.global_name,
85                name: app.name,
86                offset: self.offset.map(|x| x as i32),
87                before: self.before,
88                after: self.after,
89                first: self.max.map(|x| x as i32),
90                last: None,
91                sort_by: self.sort.map(|x| x.into()),
92            };
93
94            let versions =
95                wasmer_backend_api::query::get_deploy_app_versions(&client, vars.clone()).await?;
96
97            versions
98                .edges
99                .into_iter()
100                .flatten()
101                .filter_map(|edge| edge.node)
102                .collect::<Vec<_>>()
103        };
104
105        println!("{}", self.fmt.format.render(&versions));
106
107        Ok(())
108    }
109}