/* This file is part of jellything (https://codeberg.org/metamuffin/jellything) which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin */ pub mod migrate; use anyhow::anyhow; use clap::{Parser, Subcommand, ValueEnum}; use jellybase::{CONF, SECRETS}; use jellyclient::Instance; use jellycommon::user::CreateSessionParams; use log::{error, info}; use migrate::migrate; use std::{fmt::Debug, path::PathBuf}; #[derive(Parser)] struct Args { /// Path to global jellything config #[arg(short = 'C', long)] config: Option, #[clap(subcommand)] action: Action, } #[derive(Subcommand)] enum Action { /// Initialize a new jellything instance Init { /// Base path of the instance, must either be absolute or relative to the servers pwd base_path: PathBuf, #[arg(short, long)] brand: String, #[arg(short, long)] hostname: String, }, Migrate { database: PathBuf, mode: MigrateMode, save_location: PathBuf, }, Reimport { /// Custom hostname, the config's is used by default #[arg(long)] hostname: Option, /// Disable TLS. Dont use this. #[arg(long)] no_tls: bool, }, } #[derive(Debug, Clone, Copy, PartialEq, ValueEnum)] enum MigrateMode { Import, Export, } fn main() -> anyhow::Result<()> { env_logger::builder() .filter_level(log::LevelFilter::Info) .parse_env("LOG") .init(); let args = Args::parse(); match args.action { Action::Init { .. } => { // info!("creating new instance..."); // std::fs::create_dir_all(path.join("library"))?; // std::fs::create_dir_all(path.join("cache"))?; // std::fs::create_dir_all(path.join("assets"))?; // std::fs::create_dir_all(path.join("media"))?; // File::create_new(path.join("assets/front.htm"))? // .write_fmt(format_args!("

My very own jellything instance

"))?; // // TODO: dont fill that // serde_yaml::to_writer( // File::create_new(path.join("config.yaml"))?, // &GlobalConfig { // brand: brand.clone(), // hostname, // slogan: "Creative slogan here".to_string(), // asset_path: path.join("assets"), // cache_path: path.join("cache"), // library_path: path.join("library"), // database_path: path.join("database"), // temp_path: "/tmp".into(), // login_expire: 10, // ..Default::default() // }, // )?; // serde_json::to_writer( // File::create_new(path.join("library/directory.json"))?, // &Node { // public: NodePublic { // kind: Some(NodeKind::Collection), // title: Some("My Library".to_string()), // ..Default::default() // }, // private: NodePrivate { // ..Default::default() // }, // }, // )?; // info!("{brand:?} is ready!"); // warn!("please add an admin password to login."); error!("init is currently disabled"); Ok(()) } a @ Action::Migrate { .. } => migrate(a), Action::Reimport { hostname, no_tls } => tokio::runtime::Builder::new_multi_thread() .enable_all() .build() .unwrap() .block_on(async move { let inst = Instance::new(hostname.unwrap_or(CONF.hostname.clone()), !no_tls); info!("login"); let session = inst .login(CreateSessionParams { drop_permissions: None, expire: None, password: SECRETS .admin_password .clone() .ok_or(anyhow!("admin account required"))?, username: CONF .admin_username .clone() .ok_or(anyhow!("admin account required"))?, }) .await?; session.reimport().await?; Ok(()) }), } } // fn ok_or_warn(r: Result) -> Option { // match r { // Ok(t) => Some(t), // Err(e) => { // warn!("{e:?}"); // None // } // } // }