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
//! CLI for publishing already-compiled Move modules to Aptos.
//!
//! # Setup
//!
//! To use this CLI, you may install this crate via Cargo:
//!
//! ```bash
//! cargo install aptos-publish
//! ```
//!
//! You should also have an Aptos CLI configuration file, which can be created using:
//!
//! ```bash
//! aptos init
//! ```
//!
//! # Usage
//!
//! Run the following command to publish the module in the current directory:
//!
//! ```bash
//! aptos-publish
//! ```

use anyhow::Result;
use aptos_cli_common::{MovePackageDir, TransactionOptions, TransactionSummary};
use async_trait::async_trait;
use clap::Parser;
use clitool::CliTool;
use move_package::compilation::compiled_package::{CompiledPackage, OnDiskCompiledPackage};

/// Command to publish a Move module to Aptos
#[derive(Debug, Parser)]
#[clap(name = "aptos-upload", about, version)]
pub struct AptosUploadTool {
    #[clap(flatten)]
    pub move_options: MovePackageDir,
    #[clap(flatten)]
    pub txn_options: TransactionOptions,
    /// Name of the package.
    pub package_name: String,
}

#[async_trait]
impl CliTool<TransactionSummary> for AptosUploadTool {
    async fn execute(self) -> Result<TransactionSummary> {
        let package: CompiledPackage = OnDiskCompiledPackage::from_path(
            &self
                .move_options
                .output_dir
                .unwrap_or_else(|| self.move_options.package_dir.join("build"))
                .join(&self.package_name),
        )?
        .into_compiled_package()?;
        aptos_publish::publish(&package, &self.txn_options).await
    }
}