summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 5e618c53294e2d3f3edec2c8cedff921a35e187f (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::path::PathBuf;

use miette::IntoDiagnostic;

mod cli;
mod command;
mod config;
mod consts;
mod error;
mod format;
mod fragment;
mod template;
mod util;

use crate::cli::Command;
use crate::command::Command as _;
use crate::error::Error;

fn main() -> miette::Result<()> {
    env_logger::try_init().into_diagnostic()?;

    let args = cli::get_args();

    let cwd = std::env::current_dir()
        .map_err(Error::from)
        .into_diagnostic()?;

    let repository = git2::Repository::open(cwd)
        .map_err(Error::from)
        .into_diagnostic()?;

    let repo_workdir_path = repository
        .workdir()
        .ok_or(Error::NoWorkTree)
        .into_diagnostic()?
        .to_path_buf();

    if let Command::Init = args.command {
        return init(repo_workdir_path);
    }

    let config = crate::config::load(&repo_workdir_path)?;

    if !config.fragment_dir().exists() {
        let fragment_dir_path = {
            let mut fragment_dir_path = repo_workdir_path.clone();
            fragment_dir_path.push(config.fragment_dir());
            fragment_dir_path
        };

        std::fs::create_dir_all(fragment_dir_path)
            .map_err(Error::from)
            .into_diagnostic()?;
    }

    match args.command {
        Command::Init => unreachable!(), // reached above

        Command::New {
            interactive,
            edit,
            format,
            read,
            set,
            git,
        } => crate::command::NewCommand::builder()
            .interactive(interactive)
            .edit(edit)
            .format(format)
            .text(read)
            .set(set)
            .git(git)
            .build()
            .execute(&repo_workdir_path, &config)?,

        Command::VerifyMetadata => crate::command::VerifyMetadataCommand::builder()
            .build()
            .execute(&repo_workdir_path, &config)?,

        Command::Generate(version) => crate::command::GenerateCommand::builder()
            .version(version)
            .build()
            .execute(&repo_workdir_path, &config)?,

        Command::Release { all, allow_dirty } => crate::command::ReleaseCommand::builder()
            .repository(repository)
            .all(all)
            .allow_dirty(allow_dirty)
            .build()
            .execute(&repo_workdir_path, &config)?,

        Command::Show { format, range } => crate::command::Show::builder()
            .format(format)
            .range(range)
            .build()
            .execute(&repo_workdir_path, &config)?,
    }

    Ok(())
}

fn init(repo_workdir_path: PathBuf) -> miette::Result<()> {
    use std::io::Write;

    let unreleased_dir_path = repo_workdir_path
        .join(crate::config::fragment_dir_default())
        .join("unreleased");

    std::fs::create_dir_all(&unreleased_dir_path)
        .map_err(Error::from)
        .into_diagnostic()?;

    std::fs::File::create(unreleased_dir_path.join(".gitkeep"))
        .map_err(Error::from)
        .into_diagnostic()?;

    let mut config_file = std::fs::OpenOptions::new()
        .create(true)
        .append(false)
        .write(true)
        .open(repo_workdir_path.join(crate::config::CONFIG_FILE_NAMES[1]))
        .map_err(Error::from)
        .into_diagnostic()?;

    write!(&mut config_file, "{}", crate::config::DEFAULT_CONFIG)
        .map_err(Error::from)
        .into_diagnostic()?;

    config_file
        .sync_all()
        .map_err(Error::from)
        .into_diagnostic()?;

    let mut template_file = std::fs::OpenOptions::new()
        .create(true)
        .append(false)
        .write(true)
        .open({
            repo_workdir_path
                .join(crate::config::fragment_dir_default())
                .join(crate::config::template_path_default())
        })
        .map_err(Error::from)
        .into_diagnostic()?;

    write!(&mut template_file, "{}", crate::consts::DEFAULT_TEMPLATE)
        .map_err(Error::from)
        .into_diagnostic()?;

    template_file
        .sync_all()
        .map_err(Error::from)
        .into_diagnostic()
}