shell_compose/
command.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
use crate::{DispatcherError, Job, JobId, LogLine, ProcInfo};
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Cli;

/// Shared commands with background service
#[derive(Subcommand, Debug, Serialize, Deserialize)]
pub enum ExecCommand {
    /// Execute command
    Run {
        /// Command arguments
        args: Vec<String>,
    },
    /// Execute command with cron schedule
    Runat {
        /// Cron expression
        at: String,
        /// Command arguments
        args: Vec<String>,
    },
    /// Start service
    Start {
        /// Service name
        service: String,
    },
    /// Start service group
    Up {
        /// Service group name
        group: String,
    },
}

/// Additional commands
#[derive(Subcommand, Debug, Serialize, Deserialize)]
pub enum CliCommand {
    /// Stop service group
    Down {
        /// Service group name
        group: String,
    },
    /// Stop job
    Stop {
        /// Job id
        job_id: JobId,
    },
    /// List processes
    Ps,
    /// List active jobs
    Jobs,
    /// Show process logs
    Logs {
        /// Job id or service name
        job_or_service: Option<String>,
        // --tail: Option<usize>,
    },
    /// Stop all processes
    Exit,
}

/// IPC messages
#[derive(Debug, Serialize, Deserialize)]
pub enum Message {
    // cli <-> Listener
    Connect,
    // cli -> Listener
    ExecCommand(ExecCommand),
    CliCommand(CliCommand),
    // cli <- Listener
    PsInfo(ProcInfo),
    JobInfo(Job),
    LogLine(LogLine),
    Ok,
    JobsStarted(Vec<JobId>),
    Err(String),
}

impl From<ExecCommand> for Message {
    fn from(cmd: ExecCommand) -> Self {
        Message::ExecCommand(cmd)
    }
}

impl From<CliCommand> for Message {
    fn from(cmd: CliCommand) -> Self {
        Message::CliCommand(cmd)
    }
}

/// Convert execution result into response message
impl From<Result<(), DispatcherError>> for Message {
    fn from(res: Result<(), DispatcherError>) -> Self {
        if let Err(e) = res {
            Message::Err(format!("{e}"))
        } else {
            Message::Ok
        }
    }
}