peach-workspace/peach-config/src/main.rs

207 lines
5.9 KiB
Rust

mod change_password;
mod constants;
mod error;
mod generate_manifest;
mod publish_address;
mod set_permissions;
mod setup_networking;
mod setup_peach;
mod setup_peach_deb;
mod status;
mod update;
mod utils;
use clap::arg_enum;
use log::error;
use serde::{Deserialize, Serialize};
use structopt::StructOpt;
#[derive(StructOpt, Debug)]
#[structopt(
name = "peach-config",
about = "a CLI tool for updating, installing and configuring PeachCloud"
)]
struct Opt {
//#[structopt(short, long)]
//verbose: bool,
// SUBCOMMANDS
#[structopt(subcommand)]
commands: Option<PeachConfig>,
}
#[derive(StructOpt, Debug)]
#[structopt(name = "peach-config", about = "about")]
enum PeachConfig {
/// Prints json manifest of peach configurations
#[structopt(name = "manifest")]
Manifest,
/// Idempotent setup of PeachCloud
#[structopt(name = "setup")]
Setup(SetupOpts),
/// Updates all PeachCloud microservices
#[structopt(name = "update")]
Update(UpdateOpts),
/// Changes the password for the peach-web interface
#[structopt(name = "changepassword")]
ChangePassword(ChangePasswordOpts),
/// Updates file permissions on PeachCloud device
#[structopt(name = "permissions")]
SetPermissions,
/// Returns sbot id if sbot is running
#[structopt(name = "whoami")]
WhoAmI,
/// Publish domain and port.
/// It takes an address argument of the form host:port
#[structopt(name = "publish-address")]
PublishAddress(PublishAddressOpts),
}
#[derive(StructOpt, Debug)]
struct SetupOpts {
/// Setup i2c configurations
#[structopt(short, long)]
i2c: bool,
/// Optionally select which model of real-time-clock is being used,
/// {ds1307, ds3231}
#[structopt(short, long)]
rtc: Option<RtcOption>,
/// Run peach-config in non-interactive mode
#[structopt(short, long)]
no_input: bool,
/// Use the default en_US.UTF-8 locale for compatability
#[structopt(short, long)]
default_locale: bool,
}
#[derive(StructOpt, Debug)]
pub struct UpdateOpts {
/// Only update other microservices and not peach-config
#[structopt(short, long)]
microservices: bool,
/// Only update peach-config and not other microservices
#[structopt(short, long = "--self")]
self_only: bool,
/// List microservices which are available for updating
#[structopt(short, long)]
list: bool,
}
#[derive(StructOpt, Debug)]
pub struct ChangePasswordOpts {
/// Optional argument to specify password as CLI argument
/// if not specified, this command asks for user input for the passwords
#[structopt(short, long)]
password: Option<String>,
}
#[derive(StructOpt, Debug)]
pub struct PublishAddressOpts {
/// Specify address in the form domain:port
#[structopt(short, long)]
address: String,
}
arg_enum! {
/// enum options for real-time clock choices
#[derive(Debug)]
#[allow(non_camel_case_types)]
#[allow(clippy::enum_variant_names)]
#[derive(Serialize, Deserialize)]
pub enum RtcOption {
DS1307,
DS3231
}
}
async fn run() {
// initialize the logger
env_logger::init();
// parse cli arguments
let opt = Opt::from_args();
// switch based on subcommand
if let Some(subcommand) = opt.commands {
match subcommand {
PeachConfig::Setup(cfg) => {
match setup_peach::setup_peach(cfg.no_input, cfg.default_locale, cfg.i2c, cfg.rtc) {
Ok(_) => {}
Err(err) => {
error!("peach-config encountered an error: {}", err)
}
}
}
PeachConfig::Manifest => match generate_manifest::generate_manifest() {
Ok(_) => {}
Err(err) => {
error!(
"peach-config encountered an error generating manifest: {}",
err
)
}
},
PeachConfig::Update(opts) => match update::update(opts) {
Ok(_) => {}
Err(err) => {
error!("peach-config encountered an error during update: {}", err)
}
},
PeachConfig::ChangePassword(opts) => {
match change_password::set_peach_web_password(opts) {
Ok(_) => {}
Err(err) => {
error!(
"peach-config encountered an error during password update: {}",
err
)
}
}
}
PeachConfig::SetPermissions => match set_permissions::set_permissions() {
Ok(_) => {}
Err(err) => {
error!(
"peach-config ecountered an error updating file permissions: {}",
err
)
}
},
PeachConfig::WhoAmI => match status::whoami().await {
Ok(sbot_id) => {
println!("{:?}", sbot_id);
{}
}
Err(err) => {
error!("sbot whoami encountered an error: {}", err)
}
},
PeachConfig::PublishAddress(opts) => {
match publish_address::publish_address(opts.address).await {
Ok(_) => {}
Err(err) => {
error!(
"peach-config encountered an error during publish address: {}",
err
)
}
}
}
}
}
}
// Enable an async main function and execute the `run()` function,
// catching any errors and printing them to `stderr` before exiting the
// process.
#[async_std::main]
async fn main() {
run().await;
}