codeberg_cli/types/
context.rs1use anyhow::Context;
2
3use crate::actions::GeneralArgs;
4use crate::types::config::BergConfig;
5use crate::types::token::Token;
6use crate::{client::BergClient, render::table::TableWrapper};
7
8use super::git::{Git, OwnerRepo};
9
10pub struct BergContext<T> {
11 pub client: BergClient,
12 pub config: BergConfig,
13 pub git: Git,
14 pub args: T,
15 pub general_args: GeneralArgs,
16}
17
18impl<T> BergContext<T> {
19 pub async fn new(args: T, general_args: GeneralArgs) -> anyhow::Result<Self> {
20 let mut config = BergConfig::new()?;
21
22 if let Some(width) = general_args.max_width {
23 config.max_width = width;
24 }
25
26 let token = Token::read_from_data_dir().context(
27 "Couldn't find login data. Please use `berg auth login` to authenticate first.",
28 )?;
29 tracing::debug!("{token:?}");
30 let base_url = config.url()?;
31 tracing::debug!("{base_url:#?}");
32 let client = BergClient::new(&token, base_url)?;
33 let git = Git::new();
34
35 let version = client.get_version().await?;
36 tracing::debug!("API VERSION: {version:?}");
37
38 Ok(Self {
39 client,
40 config,
41 git,
42 args,
43 general_args,
44 })
45 }
46
47 pub fn owner_repo(&self) -> anyhow::Result<OwnerRepo> {
48 self.git.owner_repo()
49 }
50
51 pub fn make_table(&self) -> TableWrapper {
52 self.config.make_table()
53 }
54
55 pub fn editor_for(&self, prompt_for: &str, predefined_text: &str) -> anyhow::Result<String> {
56 inquire::Editor::new(format!("Open editor to write {prompt_for}").as_str())
57 .with_predefined_text(predefined_text)
58 .with_editor_command(std::ffi::OsStr::new(&self.config.editor))
59 .prompt()
60 .map_err(anyhow::Error::from)
61 }
62}