summaryrefslogtreecommitdiff
path: root/src/debian.rs
blob: 15b87ed23d4725cbbc4ca1d42af03d3199fdb52a (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
use crate::errors::BumperError;
use log::debug;
use std::path::{Path, PathBuf};
use std::process::Command;

pub struct Debian {
    dirname: PathBuf,
}

impl Debian {
    pub fn new(dirname: &Path) -> Result<Self, BumperError> {
        let changelog = dirname.join("debian/changelog");
        debug!("does {} exist? {}", changelog.display(), changelog.exists());
        if changelog.exists() {
            return Ok(Self {
                dirname: dirname.to_path_buf(),
            });
        }
        Err(BumperError::UnknownProjectKind(dirname.to_path_buf()))
    }

    pub fn name(&self) -> Result<String, BumperError> {
        let output = Command::new("dpkg-parsechangelog")
            .arg("-SSource")
            .current_dir(&self.dirname)
            .output()
            .map_err(|err| BumperError::ParseChangelogInvoke(self.dirname.to_path_buf(), err))?;
        if output.status.success() {
            let name = String::from_utf8_lossy(&output.stdout).into_owned();
            Ok(name.trim_end().to_string())
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
            Err(BumperError::ParseChangelog(
                self.dirname.to_path_buf(),
                stderr,
            ))
        }
    }

    pub fn set_version(&mut self, version: &str) -> Result<String, BumperError> {
        let version = format!("{}-1", version);
        self.dch(&["-v", &version, ""])?;
        self.dch(&["-r", ""])?;
        Ok(version)
    }

    fn dch(&self, args: &[&str]) -> Result<(), BumperError> {
        let output = Command::new("dch")
            .args(args)
            .current_dir(&self.dirname)
            .output()
            .map_err(|err| BumperError::DchInvoke(self.dirname.to_path_buf(), err))?;
        if output.status.success() {
            Ok(())
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
            Err(BumperError::Dch(self.dirname.to_path_buf(), stderr))
        }
    }
}