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

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

#[derive(Subcommand, Debug, Serialize, Deserialize)]
pub enum ExecCommand {
    /// Execute shell command
    Run {
        /// Command arguments
        args: Vec<String>,
    },
    /// Execute shell 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,
    },
}

#[derive(Subcommand, Debug, Serialize, Deserialize)]
pub enum QueryCommand {
    /// List running commands
    Ps,
    /// Show process logs
    Logs,
    /// Stop all processes
    Exit,
}

/// IPC messages
#[derive(Debug, Serialize, Deserialize)]
pub enum Message {
    Connect,
    ExecCommand(ExecCommand),
    QueryCommand(QueryCommand),
    PsInfo(ProcInfo),
    LogLine(LogLine),
    Ok,
    Err(String),
}

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

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

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