codeberg_cli/actions/config/
generate.rs1use std::env::current_dir;
2
3use crate::actions::GeneralArgs;
4use crate::paths::config_path;
5use crate::types::config::{BergConfig, ConfigLocation};
6
7use anyhow::Context;
8use clap::Parser;
9
10#[derive(Parser, Debug)]
12pub struct ConfigGenerateArgs {
13 #[arg(short, long, value_enum, default_value_t = ConfigLocation::Local)]
19 pub location: ConfigLocation,
20
21 #[arg(short, long, default_value_t = false)]
23 pub replace: bool,
24}
25
26impl ConfigGenerateArgs {
27 pub fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
28 let _ = general_args;
29 let args = self;
30 let path_dir = match args.location {
31 ConfigLocation::Global => config_path()?
32 .parent()
33 .context("config path has parent directory")?
34 .to_path_buf(),
35 ConfigLocation::Local => current_dir()?,
36 };
37 let path_file = config_path()?;
38 if !path_file.exists() || (path_file.exists() && args.replace) {
39 if args.replace {
40 std::fs::remove_file(path_file.clone())?;
41 }
42 std::fs::create_dir_all(path_dir.clone())?;
43 let config = BergConfig::default();
44 let config_text = toml::to_string(&config)?;
45 std::fs::write(path_file.clone(), config_text)?;
46 println!("Successfully created berg config at {path_file:?}");
47 } else {
48 println!("berg config already exists at {path_file:?}!");
49 }
50 Ok(())
51 }
52}