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
// Copyright (C) 2022 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

//! Top level file processing function.

use anyhow::Result;
use std::path;
use tracing::event;
use tracing::instrument;
use tracing::Level;

use crate::cmd::OutputFormat;
use crate::resolver;
use crate::workflow::Workflow;

/// Process the provided file.
/// Returns true if any outdated entities were found, false otherwise.
#[instrument(level="info", fields(filename = ?filename.as_ref().display()))]
pub async fn process_file(
    dryrun: bool,
    output_format: OutputFormat,
    resolver: &resolver::Server,
    filename: impl AsRef<path::Path>,
) -> Result<bool> {
    let filename = filename.as_ref();
    let mut workflow = match Workflow::new(filename).await {
        Ok(entities) => entities,
        Err(e) => {
            event!(
                Level::ERROR,
                error = ?e,
                filename = ?filename,
            );
            return Err(e);
        }
    };
    workflow.resolve_entities(resolver).await;
    let dryrunmsg = if dryrun { " (dryrun)" } else { "" };
    let mut any_outdated = false;
    for entity in &workflow.entities {
        if let Some(ref latest) = entity.latest {
            if &entity.version != latest {
                any_outdated = true;
                match output_format {
                    OutputFormat::Standard => {
                        println!(
                            "{}: update {} from {} to {}{}",
                            filename.display(),
                            entity.resource,
                            entity.version,
                            latest,
                            dryrunmsg
                        );
                    }
                    OutputFormat::GithubWarning => {
                        println!(
                            "::warning file={}::update {} from {} to {}",
                            filename.display(),
                            entity.resource,
                            entity.version,
                            latest,
                        );
                    }
                }
            }
        }
    }
    if !dryrun {
        match workflow.update_file().await {
            Ok(true) => {
                event!(
                    Level::INFO,
                    filename = ?filename,
                    "updated"
                );
            }
            Ok(false) => {
                event!(
                    Level::INFO,
                    filename = ?filename,
                    "unchanged"
                );
            }
            Err(e) => {
                event!(
                    Level::ERROR,
                    error = ?e,
                    filename = ?filename,
                    "error writing updated file"
                );
            }
        }
    }
    Ok(any_outdated)
}