summaryrefslogtreecommitdiff
path: root/src/bin/obnam.rs
blob: 2dbbaa2e57055d857de7050656a4cd79b5208aff (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
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
use directories_next::ProjectDirs;
use log::{debug, error, info, LevelFilter};
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Config, Logger, Root};
use obnam::client::ClientConfig;
use obnam::cmd::{
    backup, get_chunk, init, list, list_files, restore, show_config, show_generation,
};
use std::path::{Path, PathBuf};
use structopt::StructOpt;

const QUALIFIER: &str = "";
const ORG: &str = "";
const APPLICATION: &str = "obnam";

fn main() -> anyhow::Result<()> {
    let opt = Opt::from_args();
    let config = load_config_without_passwords(&opt)?;
    setup_logging(&config.config().log)?;

    info!("client starts");
    debug!("{:?}", opt);
    debug!("configuration: {:#?}", config);

    let cfgname = config_filename(&opt);
    let result = if let Command::Init {
        insecure_passphrase,
    } = opt.cmd
    {
        init(config.config(), &cfgname, insecure_passphrase)
    } else {
        let config = load_config_with_passwords(&opt)?;
        match opt.cmd {
            Command::Init {
                insecure_passphrase: _,
            } => panic!("this cannot happen"),
            Command::Backup => backup(&config),
            Command::List => list(&config),
            Command::ShowGeneration { gen_id } => show_generation(&config, &gen_id),
            Command::ListFiles { gen_id } => list_files(&config, &gen_id),
            Command::Restore { gen_id, to } => restore(&config, &gen_id, &to),
            Command::GetChunk { chunk_id } => get_chunk(&config, &chunk_id),
            Command::Config => show_config(&config),
        }
    };

    if let Err(ref e) = result {
        error!("command failed: {}", e);
        result?
    }

    info!("client ends successfully");
    Ok(())
}

fn load_config_with_passwords(opt: &Opt) -> Result<ClientConfig, anyhow::Error> {
    Ok(ClientConfig::read_with_passwords(&config_filename(opt))?)
}

fn load_config_without_passwords(opt: &Opt) -> Result<ClientConfig, anyhow::Error> {
    Ok(ClientConfig::read_without_passwords(&config_filename(opt))?)
}

fn config_filename(opt: &Opt) -> PathBuf {
    match opt.config {
        None => default_config(),
        Some(ref filename) => filename.to_path_buf(),
    }
}

fn default_config() -> PathBuf {
    if let Some(dirs) = ProjectDirs::from(QUALIFIER, ORG, APPLICATION) {
        dirs.config_dir().join("obnam.yaml")
    } else {
        panic!("can't figure out the configuration directory");
    }
}

#[derive(Debug, StructOpt)]
#[structopt(name = "obnam-backup", about = "Simplistic backup client")]
struct Opt {
    #[structopt(long, short, parse(from_os_str))]
    config: Option<PathBuf>,

    #[structopt(subcommand)]
    cmd: Command,
}

#[derive(Debug, StructOpt)]
enum Command {
    Init {
        #[structopt(long)]
        insecure_passphrase: Option<String>,
    },
    Backup,
    List,
    ListFiles {
        #[structopt(default_value = "latest")]
        gen_id: String,
    },
    Restore {
        #[structopt()]
        gen_id: String,

        #[structopt(parse(from_os_str))]
        to: PathBuf,
    },
    ShowGeneration {
        #[structopt(default_value = "latest")]
        gen_id: String,
    },
    GetChunk {
        #[structopt()]
        chunk_id: String,
    },
    Config,
}

fn setup_logging(filename: &Path) -> anyhow::Result<()> {
    let logfile = FileAppender::builder().build(filename)?;

    let config = Config::builder()
        .appender(Appender::builder().build("obnam", Box::new(logfile)))
        .logger(Logger::builder().build("obnam", LevelFilter::Debug))
        .build(Root::builder().appender("obnam").build(LevelFilter::Debug))?;

    log4rs::init_config(config)?;

    Ok(())
}