Skip to content

Commit

Permalink
feat: add option to save config
Browse files Browse the repository at this point in the history
  • Loading branch information
benpueschel committed Jul 5, 2024
1 parent 042107b commit 6520bb2
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct Config {
/// Maps the remote name to the remote configuration.
pub remotes: HashMap<String, GitRemoteConfig>,
pub secrets: Secrets,
#[serde(skip)]
pub path: String,
}

Expand Down Expand Up @@ -97,13 +98,13 @@ pub struct AuthConfig {
impl Config {
pub fn save_default() -> Result<()> {
let config = Config::default();
let toml = toml::to_string(&config)?;
let home = env::var("HOME").unwrap();
let xdg_config = env::var("XDG_CONFIG_HOME").unwrap_or(format!("{home}/.config"));
let config_path = format!("{xdg_config}/gitrc-rs/config.toml");
fs::create_dir_all(Path::new(&config_path).parent().unwrap())?;
fs::write(&config_path, toml)?;
println!("Saved default config to '{config_path}'");
config.save()
}
pub fn save(&self) -> Result<()> {
let toml = toml::to_string(self)?;
fs::create_dir_all(Path::new(&self.path).parent().unwrap())?;
fs::write(&self.path, toml)?;
println!("Saved config to '{}'", &self.path);
Ok(())
}
pub fn load_from_file(path: Option<String>) -> Result<Self> {
Expand Down Expand Up @@ -190,17 +191,21 @@ impl Config {

let contents = fs::read_to_string(&file)?;
let mut secrets: InlineSecrets = toml::from_str(&contents)?;
secrets.insert(name.to_string(), AuthConfig {
token: Some(token.to_string()),
..Default::default()
});
secrets.insert(
name.to_string(),
AuthConfig {
token: Some(token.to_string()),
..Default::default()
},
);

let toml = toml::to_string(&secrets)?;
fs::write(&file, toml)?;
Ok(())
}
}
}

pub fn get_auth(&self, secrets: &Secrets, name: &str) -> Result<Auth> {
match secrets {
Secrets::Plaintext(secrets) => {
Expand Down Expand Up @@ -270,7 +275,11 @@ impl Config {
impl Default for Config {
fn default() -> Self {
Self {
path: String::new(),
path: {
let home = env::var("HOME").unwrap();
let xdg_config = env::var("XDG_CONFIG_HOME").unwrap_or(format!("{home}/.config"));
format!("{xdg_config}/gitrc-rs/config.toml")
},
remotes: HashMap::from([
(
"gitea".to_string(),
Expand Down

0 comments on commit 6520bb2

Please sign in to comment.