github_workflow_update/
processor.rs

1// Copyright (C) 2022 Leandro Lisboa Penz <lpenz@lpenz.org>
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4
5//! Top level file processing function.
6
7use anyhow::Result;
8use std::path;
9use tracing::event;
10use tracing::instrument;
11use tracing::Level;
12
13use crate::cmd::OutputFormat;
14use crate::resolver;
15use crate::workflow::Workflow;
16
17/// Process the provided file.
18/// Returns true if any outdated entities were found, false otherwise.
19#[instrument(level="info", fields(filename = ?filename.as_ref().display()))]
20pub async fn process_file(
21    dryrun: bool,
22    output_format: OutputFormat,
23    resolver: &resolver::Server,
24    filename: impl AsRef<path::Path>,
25) -> Result<bool> {
26    let filename = filename.as_ref();
27    let mut workflow = match Workflow::new(filename).await {
28        Ok(entities) => entities,
29        Err(e) => {
30            event!(
31                Level::ERROR,
32                error = ?e,
33                filename = ?filename,
34            );
35            return Err(e);
36        }
37    };
38    workflow.resolve_entities(resolver).await;
39    let dryrunmsg = if dryrun { " (dryrun)" } else { "" };
40    let mut any_outdated = false;
41    for entity in &workflow.entities {
42        if let Some(ref latest) = entity.latest {
43            if &entity.version != latest {
44                any_outdated = true;
45                match output_format {
46                    OutputFormat::Standard => {
47                        println!(
48                            "{}: update {} from {} to {}{}",
49                            filename.display(),
50                            entity.resource,
51                            entity.version,
52                            latest,
53                            dryrunmsg
54                        );
55                    }
56                    OutputFormat::GithubWarning => {
57                        println!(
58                            "::warning file={}::update {} from {} to {}",
59                            filename.display(),
60                            entity.resource,
61                            entity.version,
62                            latest,
63                        );
64                    }
65                }
66            }
67        }
68    }
69    if !dryrun {
70        match workflow.update_file().await {
71            Ok(true) => {
72                event!(
73                    Level::INFO,
74                    filename = ?filename,
75                    "updated"
76                );
77            }
78            Ok(false) => {
79                event!(
80                    Level::INFO,
81                    filename = ?filename,
82                    "unchanged"
83                );
84            }
85            Err(e) => {
86                event!(
87                    Level::ERROR,
88                    error = ?e,
89                    filename = ?filename,
90                    "error writing updated file"
91                );
92            }
93        }
94    }
95    Ok(any_outdated)
96}