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

161 lines
4.5 KiB
Rust

mod change_password;
mod constants;
mod error;
mod generate_manifest;
mod set_permissions;
mod setup_networking;
mod setup_peach;
mod setup_peach_deb;
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,
}
#[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>,
}
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
}
}
fn main() {
// 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
)
}
},
}
}
}