-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.rs
161 lines (143 loc) · 4.45 KB
/
main.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
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
use clap::Parser;
mod commands;
mod config;
mod error;
mod printers;
mod profile;
mod progress;
mod questions;
use crate::error::CliError;
use agama_lib::error::ServiceError;
use agama_lib::manager::ManagerClient;
use agama_lib::progress::ProgressMonitor;
use async_std::task::{self, block_on};
use commands::Commands;
use config::run as run_config_cmd;
use printers::Format;
use profile::run as run_profile_cmd;
use progress::InstallerProgress;
use questions::run as run_questions_cmd;
use std::{
process::{ExitCode, Termination},
thread::sleep,
time::Duration,
};
#[derive(Parser)]
#[command(name = "agama", version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
pub command: Commands,
/// Format output
#[arg(value_enum, short, long, default_value_t = Format::Json)]
pub format: Format,
}
async fn probe() -> anyhow::Result<()> {
let another_manager = build_manager().await?;
let probe = task::spawn(async move { another_manager.probe().await });
show_progress().await?;
Ok(probe.await?)
}
/// Starts the installation process
///
/// Before starting, it makes sure that the manager is idle.
///
/// * `manager`: the manager client.
async fn install(manager: &ManagerClient<'_>, max_attempts: u8) -> anyhow::Result<()> {
if manager.is_busy().await {
println!("Agama's manager is busy. Waiting until it is ready...");
}
if !manager.can_install().await? {
return Err(CliError::ValidationError)?;
}
// Display the progress (if needed) and makes sure that the manager is ready
manager.wait().await?;
let progress = task::spawn(async { show_progress().await });
// Try to start the installation up to max_attempts times.
let mut attempts = 1;
loop {
match manager.install().await {
Ok(()) => break,
Err(e) => {
eprintln!(
"Could not start the installation process: {e}. Attempt {}/{}.",
attempts, max_attempts
);
}
}
if attempts == max_attempts {
eprintln!("Giving up.");
return Err(CliError::InstallationError)?;
}
attempts += 1;
sleep(Duration::from_secs(1));
}
let _ = progress.await;
Ok(())
}
async fn show_progress() -> Result<(), ServiceError> {
// wait 1 second to give other task chance to start, so progress can display something
task::sleep(Duration::from_secs(1)).await;
let conn = agama_lib::connection().await?;
let mut monitor = ProgressMonitor::new(conn).await.unwrap();
let presenter = InstallerProgress::new();
monitor
.run(presenter)
.await
.expect("failed to monitor the progress");
Ok(())
}
async fn wait_for_services(manager: &ManagerClient<'_>) -> Result<(), ServiceError> {
let services = manager.busy_services().await?;
// TODO: having it optional
if !services.is_empty() {
eprintln!("The Agama service is busy. Waiting for it to be available...");
show_progress().await?
}
Ok(())
}
async fn build_manager<'a>() -> anyhow::Result<ManagerClient<'a>> {
let conn = agama_lib::connection().await?;
Ok(ManagerClient::new(conn).await?)
}
async fn run_command(cli: Cli) -> anyhow::Result<()> {
match cli.command {
Commands::Config(subcommand) => {
let manager = build_manager().await?;
block_on(wait_for_services(&manager))?;
block_on(run_config_cmd(subcommand, cli.format))
}
Commands::Probe => {
let manager = build_manager().await?;
block_on(wait_for_services(&manager))?;
block_on(probe())
}
Commands::Profile(subcommand) => Ok(run_profile_cmd(subcommand)?),
Commands::Install => {
let manager = build_manager().await?;
block_on(install(&manager, 3))
}
Commands::Questions(subcommand) => block_on(run_questions_cmd(subcommand)),
_ => unimplemented!(),
}
}
/// Represents the result of execution.
pub enum CliResult {
/// Successful execution.
Ok = 0,
/// Something went wrong.
Error = 1,
}
impl Termination for CliResult {
fn report(self) -> ExitCode {
ExitCode::from(self as u8)
}
}
#[async_std::main]
async fn main() -> CliResult {
let cli = Cli::parse();
if let Err(error) = run_command(cli).await {
eprintln!("{:?}", error);
return CliResult::Error;
}
CliResult::Ok
}