1use crate::{DispatcherError, Job, JobId, LogLine, ProcInfo, RestartPolicy};
2use clap::{Parser, Subcommand};
3use serde::{Deserialize, Serialize};
4use std::path::PathBuf;
5
6#[derive(Parser, Debug)]
7#[command(version, about, long_about = None)]
8pub struct Cli;
9
10#[derive(Subcommand, Debug, Serialize, Deserialize)]
12pub enum ExecCommand {
13 Run {
15 args: Vec<String>,
17 #[arg(short, long, value_name = "POLICY")]
19 restart: Option<RestartPolicy>,
20 },
21 Runat {
23 at: String,
25 args: Vec<String>,
27 },
28 Start {
30 service: String,
32 args: Vec<String>,
34 #[arg(short, long, value_name = "POLICY")]
36 restart: Option<RestartPolicy>,
37 },
38 Up {
40 group: String,
42 },
43}
44
45#[derive(Subcommand, Debug, Serialize, Deserialize)]
47pub enum CliCommand {
48 Down {
50 group: String,
52 },
53 Stop {
55 job_id: JobId,
57 },
58 Ps,
60 Jobs,
62 Logs {
64 job_or_service: Option<String>,
66 },
68 Exit,
70}
71
72#[derive(Debug, Serialize, Deserialize)]
74pub enum Message {
75 Connect,
77 ExecCommand(ExecCommand, PathBuf),
79 CliCommand(CliCommand),
80 PsInfo(Vec<ProcInfo>),
82 JobInfo(Vec<Job>),
83 LogLine(LogLine),
84 Ok,
85 JobsStarted(Vec<JobId>),
86 Err(String),
87}
88
89impl From<ExecCommand> for Message {
90 fn from(cmd: ExecCommand) -> Self {
91 let cwd = std::env::current_dir().unwrap_or(".".into());
92 Message::ExecCommand(cmd, cwd)
93 }
94}
95
96impl From<CliCommand> for Message {
97 fn from(cmd: CliCommand) -> Self {
98 Message::CliCommand(cmd)
99 }
100}
101
102impl From<Result<(), DispatcherError>> for Message {
104 fn from(res: Result<(), DispatcherError>) -> Self {
105 if let Err(e) = res {
106 Message::Err(format!("{e}"))
107 } else {
108 Message::Ok
109 }
110 }
111}