sn_testnet_deploy/
safe.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
// Copyright (c) 2023, MaidSafe.
// All rights reserved.
//
// This SAFE Network Software is licensed under the BSD-3-Clause license.
// Please see the LICENSE file for more details.

use crate::{
    error::{Error, Result},
    run_external_command,
};
use regex::Regex;
use std::{
    net::SocketAddr,
    path::{Path, PathBuf},
};
use tokio::{fs::File as TokioFile, io::AsyncWriteExt};

pub struct SafeClient {
    pub binary_path: PathBuf,
    pub working_directory_path: PathBuf,
}

impl SafeClient {
    pub fn new(binary_path: PathBuf, working_directory_path: PathBuf) -> SafeClient {
        SafeClient {
            binary_path,
            working_directory_path,
        }
    }

    pub fn download_files(&self, peer_multiaddr: &str) -> Result<()> {
        run_external_command(
            self.binary_path.clone(),
            self.working_directory_path.clone(),
            vec![
                "--peer".to_string(),
                peer_multiaddr.to_string(),
                "files".to_string(),
                "download".to_string(),
            ],
            false,
            false,
        )?;
        Ok(())
    }

    pub fn upload_file(&self, peer_multiaddr: &str, path: &Path) -> Result<String> {
        let output = run_external_command(
            self.binary_path.clone(),
            self.working_directory_path.clone(),
            vec![
                "--peer".to_string(),
                peer_multiaddr.to_string(),
                "files".to_string(),
                "upload".to_string(),
                path.to_string_lossy().to_string(),
            ],
            false,
            false,
        )?;

        let re = Regex::new(r"Uploaded .+ to ([a-fA-F0-9]+)")?;
        for line in &output {
            if let Some(captures) = re.captures(line) {
                return Ok(captures[1].to_string());
            }
        }

        Err(Error::SafeCmdError(
            "could not obtain hex address of uploaded file".to_string(),
        ))
    }

    pub fn wallet_get_faucet(&self, peer_multiaddr: &str, faucet_addr: SocketAddr) -> Result<()> {
        run_external_command(
            self.binary_path.clone(),
            self.working_directory_path.clone(),
            vec![
                "--peer".to_string(),
                peer_multiaddr.to_string(),
                "wallet".to_string(),
                "get-faucet".to_string(),
                faucet_addr.to_string(),
            ],
            false,
            false,
        )?;
        Ok(())
    }
}

pub struct SafeBinaryRepository;

impl SafeBinaryRepository {
    pub async fn download(&self, binary_archive_url: &str, dest_path: &Path) -> Result<()> {
        let response = reqwest::get(binary_archive_url).await?;

        if !response.status().is_success() {
            return Err(Error::SafeBinaryDownloadError);
        }

        let mut dest = TokioFile::create(dest_path).await?;
        let content = response.bytes().await?;
        dest.write_all(&content).await?;

        Ok(())
    }
}