version_manager/cli/
files.rs

1use crate::{
2    files::TrackedFiles,
3    version::{run, Operator, SetTypes, VersionFile},
4    CommandRun, VersionResult,
5};
6use clap::{value_parser, Args, Parser, Subcommand};
7use clio::ClioPath;
8use regex::Regex;
9
10#[derive(Parser, Debug, Clone)]
11#[command(arg_required_else_help(true))]
12pub struct FilesCommand {
13    #[clap(subcommand)]
14    pub command: Files,
15}
16
17#[derive(Subcommand, Debug, Clone)]
18#[command(arg_required_else_help(true))]
19pub enum Files {
20    /// Add a file to add the version number
21    Track(TrackFile),
22    /// Remove a file from tracking the version number
23    Rm(File),
24    /// Set the version number from a file
25    Update(File),
26    /// Update all files
27    UpdateAll,
28    /// List tracked files
29    List,
30}
31
32#[derive(Args, Debug, Clone)]
33#[command(arg_required_else_help(true))]
34pub struct TrackFile {
35    /// The path to the file to track
36    #[arg(value_parser = value_parser!(ClioPath).exists().is_file())]
37    pub path: ClioPath,
38    /// The expression to match the version number
39    ///
40    /// This expression should be a regex with a single capture group that matches the version number
41    #[arg(value_parser = value_parser!(Regex))]
42    pub expr: Regex,
43}
44
45#[derive(Args, Debug, Clone)]
46#[command(arg_required_else_help(true))]
47pub struct File {
48    /// The path to the file
49    #[arg(value_parser = clap::value_parser!(ClioPath).exists().is_file())]
50    pub path: ClioPath,
51}
52
53impl CommandRun for FilesCommand {
54    fn run(&self, version: &mut VersionFile) -> VersionResult<()> {
55        self.command.run(version)
56    }
57}
58
59impl CommandRun for TrackFile {
60    fn run(&self, version: &mut VersionFile) -> VersionResult<()> {
61        let track_file =
62            TrackedFiles::new_from_path_and_regex(self.path.to_path_buf(), self.expr.clone());
63        version.value = Some(SetTypes::NewFile(track_file));
64        run(version)
65    }
66}
67
68impl CommandRun for File {
69    fn run(&self, version: &mut VersionFile) -> VersionResult<()> {
70        version.value = Some(SetTypes::String(self.path.to_string()));
71        run(version)
72    }
73}
74
75impl CommandRun for Files {
76    fn run(&self, version: &mut VersionFile) -> VersionResult<()> {
77        match self {
78            Files::Track(track) => {
79                version.operator = Some(Operator::AddFile);
80                track.run(version)
81            }
82            Files::Rm(file) => {
83                version.operator = Some(Operator::RmFile);
84                file.run(version)
85            }
86            Files::Update(file) => {
87                version.operator = Some(Operator::Update);
88                file.run(version)
89            }
90            Files::UpdateAll => {
91                version.operator = Some(Operator::UpdateAll);
92                run(version)
93            }
94            Files::List => {
95                version.operator = Some(Operator::ListFiles);
96                run(version)
97            }
98        }
99    }
100}