golem-cli 1.3.1

Command line interface for Golem.
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Copyright 2024-2025 Golem Cloud
//
// Licensed under the Golem Source License v1.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://license.golem.cloud/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::context::check_http_response_success;
use crate::log::{log_action, LogColorize, LogIndent};
use crate::model::app::InitialComponentFile;
use anyhow::{anyhow, bail, Context};
use async_trait::async_trait;
use async_zip::tokio::write::ZipFileWriter;
use async_zip::{Compression, ZipEntryBuilder};
use golem_common::model::{ComponentFilePathWithPermissions, ComponentFilePathWithPermissionsList};
use itertools::Itertools;
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use tokio::fs::File;
use tokio_stream::wrappers::ReadDirStream;
use tokio_stream::StreamExt;
use url::Url;

#[derive(Debug, Clone)]
struct LoadedFile {
    content: Vec<u8>,
    target: ComponentFilePathWithPermissions,
}

#[derive(Debug, Clone)]
pub struct HashedFile {
    pub hash_hex: String,
    pub target: ComponentFilePathWithPermissions,
}

#[derive(Debug)]
pub struct ComponentFilesArchive {
    pub archive_path: PathBuf,
    pub properties: ComponentFilePathWithPermissionsList,
    _temp_dir: TempDir, // archive_path is only valid as long as this is alive
}

pub struct IfsFileManager {
    client: reqwest::Client,
}

impl IfsFileManager {
    pub fn new(client: reqwest::Client) -> Self {
        Self { client }
    }

    pub async fn build_files_archive(
        &self,
        component_files: &[InitialComponentFile],
    ) -> anyhow::Result<ComponentFilesArchive> {
        let file_processor = FileLoader {
            client: self.client.clone(),
        };

        log_action("Creating", "IFS archive");
        let _indent = LogIndent::new();

        validate_unique_targets(component_files)?;

        let temp_dir = tempfile::Builder::new()
            .prefix("golem-cli-zip")
            .tempdir()
            .with_context(|| "Error creating temporary dir for IFS archive")?;
        let zip_file_path = temp_dir.path().join("data.zip");
        let zip_file = File::create(&zip_file_path)
            .await
            .with_context(|| "Error creating zip file for IFS archive")?;
        let mut zip_writer = ZipFileWriter::with_tokio(zip_file);

        let mut successfully_added: Vec<ComponentFilePathWithPermissions> =
            Vec::with_capacity(component_files.len());

        for component_file in component_files {
            for LoadedFile { content, target } in self
                .process_component_file(&file_processor, component_file)
                .await?
            {
                // zip files do not like absolute paths. Convert the absolute component path to relative path from the root of the zip file
                let zip_entry_name = target.path.to_string();
                let builder =
                    ZipEntryBuilder::new(zip_entry_name.clone().into(), Compression::Deflate);

                log_action(
                    "Adding",
                    format!(
                        "entry {} to IFS archive",
                        zip_entry_name.log_color_highlight()
                    ),
                );

                zip_writer
                    .write_entry_whole(builder, &content)
                    .await
                    .with_context(|| {
                        anyhow!("Error writing zip entry for IFS archive {}", zip_entry_name)
                    })?;

                successfully_added.push(target);
            }
        }

        zip_writer.close().await.with_context(|| {
            anyhow!(
                "Error closing zip file for IFS archive {}",
                zip_file_path.display()
            )
        })?;

        let properties = ComponentFilePathWithPermissionsList {
            values: successfully_added,
        };

        Ok(ComponentFilesArchive {
            _temp_dir: temp_dir,
            archive_path: zip_file_path,
            properties,
        })
    }

    pub async fn collect_file_hashes(
        &self,
        component_name: &str,
        component_files: &[InitialComponentFile],
    ) -> anyhow::Result<Vec<HashedFile>> {
        if component_files.is_empty() {
            return Ok(Vec::new());
        }

        let file_processor = FileHasher {
            client: self.client.clone(),
        };

        log_action(
            "Calculating hashes",
            format!(
                "for manifest IFS files, component: {}",
                component_name.log_color_highlight()
            ),
        );
        let _indent = LogIndent::new();

        validate_unique_targets(component_files)?;

        let mut hashes = Vec::with_capacity(component_files.len());
        for component_file in component_files {
            hashes.extend(
                self.process_component_file(&file_processor, component_file)
                    .await?,
            );
        }

        Ok(hashes)
    }

    async fn process_component_file<R>(
        &self,
        file_processor: &dyn FileProcessor<R>,
        component_file: &InitialComponentFile,
    ) -> anyhow::Result<Vec<R>> {
        let scheme = component_file.source.as_url().scheme();
        match scheme {
            "file" | "" => {
                self.process_local_path(file_processor, component_file)
                    .await
            }
            "http" | "https" => self
                .process_remote_file(file_processor, component_file)
                .await
                .map(|f| vec![f]),
            _ => Err(anyhow!(
                "Unsupported scheme '{}' for IFS file: {}",
                scheme,
                component_file.source.as_url()
            )),
        }
    }

    async fn process_local_path<R>(
        &self,
        file_processor: &dyn FileProcessor<R>,
        component_file: &InitialComponentFile,
    ) -> anyhow::Result<Vec<R>> {
        // if it's a directory, we need to recursively load all files and combine them with their target paths and permissions.
        let source_path = PathBuf::from(component_file.source.as_url().path());

        let mut results: Vec<R> = vec![];
        let mut queue: VecDeque<(PathBuf, ComponentFilePathWithPermissions)> =
            vec![(source_path, component_file.target.clone())].into();

        while let Some((path, target)) = queue.pop_front() {
            if path.is_dir() {
                let read_dir = tokio::fs::read_dir(&path)
                    .await
                    .with_context(|| anyhow!("Error reading directory: {}", path.display()))?;
                let mut read_dir_stream = ReadDirStream::new(read_dir);

                while let Some(entry) = read_dir_stream.next().await {
                    let entry = entry.context("Error reading directory entry")?;
                    let next_path = entry.path();

                    let file_name = entry.file_name().into_string().map_err(|_| {
                        anyhow!(
                            "Error converting file name to string: Contains non-unicode data"
                                .to_string(),
                        )
                    })?;

                    let mut new_target = target.clone();
                    new_target
                        .extend_path(file_name.as_str())
                        .map_err(|err| anyhow!("Error extending path: {err}"))?;

                    queue.push_back((next_path, new_target));
                }
            } else {
                results.push(file_processor.process_local_file(&path, &target).await?);
            }
        }
        Ok(results)
    }

    async fn process_remote_file<R>(
        &self,
        file_processor: &dyn FileProcessor<R>,
        component_file: &InitialComponentFile,
    ) -> anyhow::Result<R> {
        file_processor
            .process_remote_file(component_file.source.as_url(), &component_file.target)
            .await
    }
}

#[async_trait]
trait FileProcessor<R> {
    async fn process_local_file(
        &self,
        path: &Path,
        target: &ComponentFilePathWithPermissions,
    ) -> anyhow::Result<R>;

    async fn process_remote_file(
        &self,
        url: &Url,
        target: &ComponentFilePathWithPermissions,
    ) -> anyhow::Result<R>;
}

struct FileLoader {
    client: reqwest::Client,
}

#[async_trait]
impl FileProcessor<LoadedFile> for FileLoader {
    async fn process_local_file(
        &self,
        path: &Path,
        target: &ComponentFilePathWithPermissions,
    ) -> anyhow::Result<LoadedFile> {
        log_action(
            "Loading",
            format!(
                "local IFS file: {}",
                path.display().to_string().log_color_highlight()
            ),
        );

        let content = tokio::fs::read(&path)
            .await
            .with_context(|| anyhow!("Error reading local IFS file: {}", path.display()))?;

        Ok(LoadedFile {
            content,
            target: target.clone(),
        })
    }

    async fn process_remote_file(
        &self,
        url: &Url,
        target: &ComponentFilePathWithPermissions,
    ) -> anyhow::Result<LoadedFile> {
        log_action(
            "Downloading",
            format!("remote IFS file: {}", url.as_str().log_color_highlight()),
        );

        let response = self
            .client
            .get(url.clone())
            .send()
            .await
            .with_context(|| anyhow!("Failed to download remote IFS file: {}", url))?;

        let response = check_http_response_success(response).await?;

        let bytes = response
            .bytes()
            .await
            .with_context(|| anyhow!("Failed to download remote IFS file: {}", url))?;

        Ok(LoadedFile {
            content: bytes.into(),
            target: target.clone(),
        })
    }
}

struct FileHasher {
    client: reqwest::Client,
}

#[async_trait]
impl FileProcessor<HashedFile> for FileHasher {
    async fn process_local_file(
        &self,
        path: &Path,
        target: &ComponentFilePathWithPermissions,
    ) -> anyhow::Result<HashedFile> {
        log_action(
            "Calculating hash",
            format!(
                "for local IFS file: {}",
                path.display().to_string().log_color_highlight()
            ),
        );

        let mut hasher = blake3::Hasher::new();
        hasher
            .update_reader(
                std::fs::File::open(path)
                    .with_context(|| anyhow!("Error reading local IFS file: {}", path.display()))?,
            )
            .with_context(|| anyhow!("Failed to hash local IFS file: {}", path.display()))?;

        Ok(HashedFile {
            hash_hex: hasher.finalize().to_hex().to_string(),
            target: target.clone(),
        })
    }

    async fn process_remote_file(
        &self,
        url: &Url,
        target: &ComponentFilePathWithPermissions,
    ) -> anyhow::Result<HashedFile> {
        log_action(
            "Calculating hash",
            format!(
                "for remote IFS file: {}",
                url.as_str().log_color_highlight()
            ),
        );
        let response = self
            .client
            .get(url.clone())
            .send()
            .await
            .with_context(|| anyhow!("Failed to stream remote IFS file: {}", url))?;

        let response = check_http_response_success(response).await?;

        let mut hasher = blake3::Hasher::new();
        let mut stream = response.bytes_stream();
        while let Some(chunk) = stream.next().await {
            let bytes =
                chunk.with_context(|| anyhow!("Failed to stream remote IFS file: {}", url))?;
            hasher.update(&bytes);
        }

        Ok(HashedFile {
            hash_hex: hasher.finalize().to_hex().to_string(),
            target: target.clone(),
        })
    }
}

// TODO: add this to manifest validation (too or instead of doing it here)?
fn validate_unique_targets(component_files: &[InitialComponentFile]) -> anyhow::Result<()> {
    let non_unique_target_paths = component_files
        .iter()
        .map(|file| file.target.path.as_path())
        .counts()
        .into_iter()
        .filter(|&(_, count)| count > 1)
        .map(|(path, _)| path)
        .collect::<Vec<_>>();

    if !non_unique_target_paths.is_empty() {
        bail!(
            "Found duplicated IFS targets: {}",
            non_unique_target_paths.into_iter().join(", ")
        );
    }

    Ok(())
}