Skip to main content

commit_wizard/core/
context.rs

1use std::path::PathBuf;
2
3use crate::{
4    engine::{
5        Error,
6        models::{
7            policy::Policy,
8            runtime::{AvailableConfigOptions, ResolvedConfig, Runtime, mode::RunMode},
9        },
10    },
11    infra::{git::Git, ui::Ui},
12};
13
14pub type AppResult<T> = Result<T, Error>;
15
16#[derive(Debug)]
17pub struct Context {
18    runtime: Runtime,
19}
20
21impl Context {
22    pub fn new(runtime: Runtime) -> Self {
23        Self { runtime }
24    }
25
26    pub fn runtime(&self) -> &Runtime {
27        &self.runtime
28    }
29
30    pub fn set_workdir(&mut self, path: PathBuf) {
31        self.runtime.set_cwd(path);
32    }
33
34    pub fn set_run_mode(&mut self, ci: bool, non_interactive: bool) {
35        self.runtime
36            .set_mode(RunMode::from_flags(ci, non_interactive));
37    }
38
39    pub(crate) fn set_in_git_repo(&mut self, value: bool) {
40        self.runtime.set_in_git_repo(value);
41    }
42
43    pub(crate) fn set_repo_root(&mut self, path: PathBuf) {
44        self.runtime.set_repo_root(path);
45    }
46
47    pub(crate) fn resolve_available_sources(&mut self) {
48        let ui = self.ui();
49        self.runtime.resolve_available_sources(&ui);
50    }
51
52    pub(crate) fn resolve_active_config(&mut self) -> crate::engine::error::Result<()> {
53        let ui = self.ui();
54        self.runtime.resolve_active_config(&ui)
55    }
56
57    pub fn is_interactive(&self) -> bool {
58        self.runtime.is_interactive()
59    }
60
61    pub fn cwd(&self) -> &PathBuf {
62        self.runtime.cwd()
63    }
64
65    pub fn repo_root(&self) -> &PathBuf {
66        self.runtime.repo_root()
67    }
68
69    pub fn dry_run(&self) -> bool {
70        self.runtime.options().dry_run()
71    }
72
73    pub fn auto_yes(&self) -> bool {
74        self.runtime.options().auto_yes()
75    }
76
77    pub fn force(&self) -> bool {
78        self.runtime.options().force()
79    }
80
81    pub fn in_git_repo(&self) -> bool {
82        self.runtime.in_git_repo()
83    }
84
85    pub fn config(&self) -> Option<&ResolvedConfig> {
86        self.runtime.config()
87    }
88    pub fn explicit_config_path(&self) -> Option<&PathBuf> {
89        self.runtime.explicit_config_path()
90    }
91
92    pub fn project_config_path(&self) -> Option<PathBuf> {
93        self.runtime.project_config_path()
94    }
95
96    pub fn sources(&self) -> &AvailableConfigOptions {
97        self.runtime.sources()
98    }
99
100    pub fn policy(&self) -> &Policy {
101        self.runtime.policy()
102    }
103
104    pub fn ui_config(&self) -> scriba::Config {
105        self.runtime.output_config()
106    }
107
108    pub fn ui(&self) -> Ui {
109        Ui::cached_with_config(self.ui_config(), self.runtime.options().output_envelope())
110    }
111
112    pub fn git(&self) -> Git {
113        Git::new(self.cwd().clone())
114    }
115}