pijul_org / pijul

Fixing #195

By pmeunier on October 22, 2018
This patch is not signed.
AxsRMkNosiFb85JbiJpFA9idEmvvKf61SDfyQFkZT8JsLndGRGPjxUy5HSXYp7M8SKSbc8p5SNxUhKBCmSzxPqz9
This patch is in the following branches:
latest
master
testing

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145









146
147
148
            Err(ErrorKind::NoHomeDir.into())
use commands::remote::{parse_remote, Remote};
use dirs;
use error::Error;
use libpijul::fs_representation::RepoRoot;
use sequoia_openpgp::parse::Parse;
use sequoia_openpgp::serialize::Serialize;
use sequoia_openpgp::tpk::{CipherSuite, TPKBuilder};
use sequoia_openpgp::TSK;
use std;
use std::collections::BTreeMap;
use std::fs::{create_dir_all, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use thrussh_keys::key::KeyPair;
use toml;

pub const DEFAULT_REMOTE: &'static str = "remote";

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Repository {
    pub address: String,
    pub port: Option<u16>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Meta {
    #[serde(default)]
    pub authors: Vec<String>,
    pub editor: Option<String>,
    pub pull: Option<String>,
    pub push: Option<String>,
    #[serde(default)]
    pub remote: BTreeMap<String, Repository>,
    pub signing_key: Option<String>,
}

impl Meta {
    pub fn load(r: &RepoRoot<impl AsRef<Path>>) -> Result<Meta, Error> {
        let mut str = String::new();
        {
            let mut f = File::open(r.meta_file())?;
            f.read_to_string(&mut str)?;
        }
        Ok(toml::from_str(&str)?)
    }
    pub fn new() -> Meta {
        Meta {
            authors: Vec::new(),
            editor: None,
            pull: None,
            push: None,
            remote: BTreeMap::new(),
            signing_key: None,
        }
    }
    pub fn save(&self, r: &RepoRoot<impl AsRef<Path>>) -> Result<(), Error> {
        let mut f = File::create(r.meta_file())?;
        let s: String = toml::to_string(&self)?;
        f.write_all(s.as_bytes())?;
        Ok(())
    }

    fn parse_remote<'a>(
        &'a self,
        remote: &'a str,
        port: Option<u16>,
        base_path: Option<&'a Path>,
        local_repo_root: Option<&'a Path>,
    ) -> Remote<'a> {
        if let Some(repo) = self.remote.get(remote) {
            parse_remote(
                &repo.address,
                port.or(repo.port),
                base_path,
                local_repo_root,
            )
        } else {
            parse_remote(remote, port, base_path, local_repo_root)
        }
    }

    fn get_remote<'a>(
        &'a self,
        remote: Option<&'a str>,
        default_remote: Option<&'a String>,
        port: Option<u16>,
        base_path: Option<&'a Path>,
        local_repo_root: Option<&'a Path>,
    ) -> Result<Remote<'a>, Error> {
        if let Some(remote) = remote {
            Ok(self.parse_remote(remote, port, base_path, local_repo_root))
        } else if let Some(ref remote) = default_remote {
            Ok(self.parse_remote(remote, port, base_path, local_repo_root))
        } else if self.remote.len() == 1 {
            let remote = self.remote.keys().next().unwrap();
            Ok(self.parse_remote(remote, port, base_path, local_repo_root))
        } else {
            Err(Error::MissingRemoteRepository)
        }
    }

    pub fn pull<'a>(
        &'a self,
        remote: Option<&'a str>,
        port: Option<u16>,
        base_path: Option<&'a Path>,
        local_repo_root: Option<&'a Path>,
    ) -> Result<Remote<'a>, Error> {
        self.get_remote(remote, self.pull.as_ref(), port, base_path, local_repo_root)
    }

    pub fn push<'a>(
        &'a self,
        remote: Option<&'a str>,
        port: Option<u16>,
        base_path: Option<&'a Path>,
        local_repo_root: Option<&'a Path>,
    ) -> Result<Remote<'a>, Error> {
        self.get_remote(remote, self.push.as_ref(), port, base_path, local_repo_root)
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Global {
    pub author: Option<String>,
    pub editor: Option<String>,
    pub signing_key: Option<String>,
}

pub fn global_path() -> Result<PathBuf, Error> {
    if let Ok(var) = std::env::var("PIJUL_CONFIG_DIR") {
        let mut path = PathBuf::new();
        path.push(var);
        Ok(path)
    } else if let Ok(var) = std::env::var("XDG_DATA_HOME") {
        let mut path = PathBuf::new();
        path.push(var);
        path.push("pijul");
        std::fs::create_dir_all(&path)?;
        path.push("config");
        Ok(path)
    } else {
        if let Some(mut path) = dirs::home_dir() {
            path.push(".pijulconfig");
            Ok(path)
        }
        Err(_) => {
            if let Some(mut path) = dirs::home_dir() {
                path.push(".pijulconfig");
                path.push(".pijulconfig");
                Ok(path)
            } else {
                Err(ErrorKind::NoHomeDir.into())
            }
        } else {
            Err(Error::NoHomeDir)
        }