aboutsummaryrefslogtreecommitdiff
path: root/tool/src/main.rs
blob: 68513d177e6d78c28d178ab0f9cc6da0389dfae5 (plain)
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
/*
    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 <metamuffin.org>
*/

pub mod add;
pub mod migrate;

use add::add;
use anyhow::anyhow;
use clap::{Parser, Subcommand, ValueEnum};
use jellybase::{CONF, SECRETS};
use jellyclient::Instance;
use jellycommon::user::CreateSessionParams;
use log::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<PathBuf>,
    #[clap(subcommand)]
    action: Action,
}

#[derive(Subcommand)]
enum Action {
    Add {
        #[arg(short, long)]
        id: Option<String>,
        #[arg(short, long)]
        media: Option<PathBuf>,
        #[arg(short, long)]
        library_path: Option<PathBuf>,
    },
    Migrate {
        database: PathBuf,
        mode: MigrateMode,
        save_location: PathBuf,
    },
    Reimport {
        /// Custom hostname, the config's is used by default
        #[arg(long)]
        hostname: Option<String>,
        /// 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 {
        a @ Action::Add { .. } => tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .unwrap()
            .block_on(add(a)),
        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(())
            }),
    }
}