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
//! Up task execution.
use crate::exec::cmd_log;
use crate::exec::LivDuct;
use crate::generate;
use crate::log;
use crate::opts::GenerateGitConfig;
use crate::opts::LinkOptions;
use crate::opts::UpdateSelfOptions;
use crate::tasks;
use crate::tasks::defaults::DefaultsConfig;
use crate::tasks::git::GitConfig;
use crate::tasks::ResolveEnv;
use crate::tasks::TaskError as E;
use camino::Utf8Path;
use camino::Utf8PathBuf;
use color_eyre::eyre::eyre;
use color_eyre::eyre::Result;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use std::collections::HashMap;
use std::fmt;
use std::fmt::Display;
use std::fs;
use std::process::Output;
use std::string::String;
use std::time::Duration;
use std::time::Instant;
use tracing::debug;
use tracing::info;
use tracing::trace;
use tracing::Level;

/// Possible statuses an asynchronously running task can have.
#[derive(Debug)]
pub enum TaskStatus {
    /// Skipped.
    Incomplete,
    /// Skipped.
    Skipped,
    /// Completed successfully.
    Passed,
    /// Completed unsuccessfully.
    Failed(E),
}

/// A task's state.
#[derive(Debug)]
pub struct Task {
    /// Task name.
    pub name: String,
    /// Path to the task config on disk.
    pub path: Utf8PathBuf,
    /// The parsed task config file contents.
    pub config: TaskConfig,
    /// When the task was started.
    pub start_time: Instant,
    /// Current task status.
    pub status: TaskStatus,
}

/// Configuration a task can have, a `~/.config/up/tasks/<name>.yaml` will deserialize to this
/// struct.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TaskConfig {
    /// Task name, defaults to file name (minus extension) if unset.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Set of Constraints that will cause the task to be run.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub constraints: Option<HashMap<String, String>>,
    /// Tasks that must have been executed beforehand.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub requires: Option<Vec<String>>,
    /// Whether to run this by default, or only if required.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_run: Option<bool>,
    /// Run library: up-rs library to use for this task. Either use this or
    /// `run_cmd` + `run_if_cmd`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_lib: Option<String>,
    /**
    Run if command: only run the `run_cmd` if this command passes (returns exit code 0).

    The task will be skipped if exit code 204 is returned (HTTP 204 means "No Content").
    Any other exit code means the command failed to run.
    */
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_if_cmd: Option<Vec<String>>,
    /**
    Run command: command to run to perform the update.

    The task will be marked as skipped if exit code 204 is returned (HTTP 204 means "No Content").
    Any other exit code means the command failed to run.
    */
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_cmd: Option<Vec<String>>,
    /// Description of the task.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Set to true to prompt for superuser privileges before running.
    /// This will allow all subtasks that up executes in this iteration.
    #[serde(default = "default_false")]
    pub needs_sudo: bool,
    // This field must be the last one in order for the yaml serializer in the generate functions
    // to be able to serialise it properly.
    /// Set of data provided to the Run library.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<serde_yaml::Value>,
}

/// Used for serde defaults above.
const fn default_false() -> bool {
    false
}

/// Shell commands we run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandType {
    /// `run_if_cmd` field in the yaml.
    RunIf,
    /// `run_cmd` field in the yaml.
    Run,
}

impl Display for CommandType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Run => write!(f, "run command"),
            Self::RunIf => write!(f, "run_if command"),
        }
    }
}

impl Task {
    /// Parse a Task from a path to a task config file.
    pub fn from(path: &Utf8Path) -> Result<Self> {
        let start_time = Instant::now();
        let s = fs::read_to_string(path).map_err(|e| E::ReadFile {
            path: path.to_owned(),
            source: e,
        })?;
        trace!("Task '{path}' contents: <<<{s}>>>");
        let config = serde_yaml::from_str::<TaskConfig>(&s).map_err(|e| E::InvalidYaml {
            path: path.to_owned(),
            source: e,
        })?;
        let name = match &config.name {
            Some(n) => n.clone(),
            None => path
                .file_stem()
                .ok_or_else(|| eyre!("Task had no path."))?
                .to_owned(),
        };
        let task = Self {
            name,
            path: path.to_owned(),
            config,
            start_time,
            status: TaskStatus::Incomplete,
        };
        debug!("Task '{name}': {task:?}", name = &task.name);
        Ok(task)
    }

    /// Run a task.
    pub fn run<F>(&mut self, env_fn: F, env: &HashMap<String, String>, task_tempdir: &Utf8Path)
    where
        F: Fn(&str) -> Result<String, E>,
    {
        match self.try_run(env_fn, env, task_tempdir) {
            Ok(status) => self.status = status,
            Err(e) => self.status = TaskStatus::Failed(e),
        }
    }

    /// Try to run the task.
    pub fn try_run<F>(
        &mut self,
        env_fn: F,
        env: &HashMap<String, String>,
        task_tempdir: &Utf8Path,
    ) -> Result<TaskStatus, E>
    where
        F: Fn(&str) -> Result<String, E>,
    {
        let name = &self.name;
        info!("Running");

        if let Some(mut cmd) = self.config.run_if_cmd.clone() {
            debug!("Running run_if command.");
            for s in &mut cmd {
                *s = env_fn(s)?;
            }
            // TODO(gib): Allow choosing how to validate run_if_cmd output (stdout, zero exit
            // code, non-zero exit code).
            if !self.run_command(CommandType::RunIf, &cmd, env, task_tempdir)? {
                debug!("Skipping task as run_if command failed.");
                return Ok(TaskStatus::Skipped);
            }
        } else {
            debug!("You haven't specified a run_if command, so it will always be run",);
        }

        if let Some(lib) = &self.config.run_lib {
            let maybe_data = self.config.data.clone();

            let status = match lib.as_str() {
                "link" => {
                    let data: LinkOptions =
                        parse_task_config(maybe_data, &self.name, false, env_fn)?;
                    tasks::link::run(data, task_tempdir)
                }

                "git" => {
                    let data: Vec<GitConfig> =
                        parse_task_config(maybe_data, &self.name, false, env_fn)?;
                    tasks::git::run(&data)
                }

                "generate_git" => {
                    let data: Vec<GenerateGitConfig> =
                        parse_task_config(maybe_data, &self.name, false, env_fn)?;
                    generate::git::run(&data)
                }

                "defaults" => {
                    let data: DefaultsConfig =
                        parse_task_config(maybe_data, &self.name, false, env_fn)?;
                    tasks::defaults::run(data, task_tempdir)
                }

                "self" => {
                    let data: UpdateSelfOptions =
                        parse_task_config(maybe_data, &self.name, true, env_fn)?;
                    tasks::update_self::run(&data)
                }

                _ => Err(eyre!("This run_lib is invalid or not yet implemented.")),
            }
            .map_err(|e| E::TaskError {
                name: self.name.clone(),
                lib: lib.to_string(),
                source: e,
            })?;
            return Ok(status);
        }

        if let Some(mut cmd) = self.config.run_cmd.clone() {
            debug!("Running '{name}' run command.");
            for s in &mut cmd {
                *s = env_fn(s)?;
            }
            if self.run_command(CommandType::Run, &cmd, env, task_tempdir)? {
                return Ok(TaskStatus::Passed);
            }
            return Ok(TaskStatus::Skipped);
        }

        Err(E::MissingCmd {
            name: self.name.clone(),
        })
    }

    // TODO(gib): Error should include an easy way to see the task logs.
    /**
    Run a command.
    If the `command_type` is `RunIf`, then `Ok(false)` may be returned if the command was skipped.
    */
    pub fn run_command(
        &self,
        command_type: CommandType,
        cmd: &[String],
        env: &HashMap<String, String>,
        task_tempdir: &Utf8Path,
    ) -> Result<bool, E> {
        let now = Instant::now();
        let task_output_file = task_tempdir.join("task_stdout_stderr.txt");

        let output = cmd_log(
            Level::DEBUG,
            cmd.first().ok_or(E::EmptyCmd)?,
            cmd.get(1..).unwrap_or(&[]),
        )
        .dir(task_tempdir)
        .full_env(env)
        .stderr_path(&task_output_file)
        .unchecked()
        .run_with_path(&task_output_file);

        let output = output.map_err(|e| {
            let suggestion = match e.kind() {
                std::io::ErrorKind::PermissionDenied => format!(
                    "\n Suggestion: Try making the file executable with `chmod +x {path}`",
                    path = cmd.first().map_or("", String::as_str)
                ),
                _ => String::new(),
            };
            E::CmdFailed {
                command_type,
                name: self.name.clone(),
                cmd: cmd.into(),
                source: e,
                suggestion,
            }
        })?;

        let elapsed_time = now.elapsed();
        let command_result = match output.status.code() {
            Some(0) => Ok(true),
            Some(204) => Ok(false),
            Some(code) => Err(E::CmdNonZero {
                name: self.name.clone(),
                command_type,
                cmd: cmd.to_owned(),
                output_file: task_output_file,
                code,
            }),
            None => Err(E::CmdTerminated {
                command_type,
                name: self.name.clone(),
                cmd: cmd.to_owned(),
                output_file: task_output_file,
            }),
        };
        self.log_command_output(command_type, command_result.is_ok(), &output, elapsed_time);
        command_result
    }

    /// Logs command output (as `debug` if it passed, or as `error` otherwise).
    pub fn log_command_output(
        &self,
        command_type: CommandType,
        command_success: bool,
        output: &Output,
        elapsed_time: Duration,
    ) {
        let name = &self.name;
        let level = if command_success {
            Level::DEBUG
        } else {
            Level::ERROR
        };

        // TODO(gib): How do we separate out the task output?
        // TODO(gib): Document error codes.
        log!(
            level,
            "Task '{name}' {command_type} ran in {elapsed_time:?} with {}",
            output.status
        );
        if !output.stdout.is_empty() {
            log!(
                level,
                "Task '{name}' {command_type} stdout:\n<<<\n{}>>>\n",
                String::from_utf8_lossy(&output.stdout),
            );
        }
        if !output.stderr.is_empty() {
            log!(
                level,
                "Task '{name}' {command_type} command stderr:\n<<<\n{}>>>\n",
                String::from_utf8_lossy(&output.stderr),
            );
        }
    }
}

/// Convert a task's `data:` block into a task config.
/// Set `has_default` to `true` if the task should fall back to `Default::default()`, or `false` if
/// it should error when no value was passed.
fn parse_task_config<F, T: ResolveEnv + Default + for<'de> serde::Deserialize<'de>>(
    maybe_data: Option<serde_yaml::Value>,
    task_name: &str,
    has_default: bool,
    env_fn: F,
) -> Result<T, E>
where
    F: Fn(&str) -> Result<String, E>,
{
    let data = match maybe_data {
        Some(data) => data,
        None if has_default => return Ok(T::default()),
        None => {
            return Err(E::TaskDataRequired {
                task: task_name.to_owned(),
            });
        }
    };

    let mut raw_opts: T =
        serde_yaml::from_value(data).map_err(|e| E::DeserializeError { source: e })?;
    raw_opts.resolve_env(env_fn)?;
    Ok(raw_opts)
}