Generate README on behalf of user.

pull/3/head
Joshua Potter 2022-01-09 10:29:43 -05:00
parent 37394588e5
commit d23fcadcc1
4 changed files with 23 additions and 2 deletions

View File

@ -274,7 +274,7 @@ mod tests {
env::set_var("HOME", &home_dir);
env::set_var("XDG_CONFIG_HOME", "");
let template = path::resolve(Path::new("examples/template.yml")).unwrap();
let template = path::resolve(Path::new("rsrc/template.yml")).unwrap();
let config = config::load(&vec![template]).unwrap();
func(&config, &home_dir);

View File

@ -5,7 +5,9 @@ use git2::{
StashFlags,
};
use simplelog::{info, paris, warn};
use std::{collections::HashSet, env::VarError, error, fmt, io, path::Path, result};
use std::{
collections::HashSet, env::VarError, error, fmt, fs::File, io, io::Write, path::Path, result,
};
// ========================================
// Error
@ -133,6 +135,24 @@ pub fn init(pc: &PathConfig) -> Result<Repository> {
// Syncing
// ========================================
fn create_readme(repo: &Repository) -> Result<()> {
let workdir = repo.workdir().ok_or(Error::InvalidBareRepo)?;
let mut readme = workdir.to_path_buf();
readme.push("README.md");
let mut file = File::create(&readme)?;
// TODO(jrpotter): Get the name of the remote repository.
file.write_all(
"# home-config\n\nThis repository is maintained by \
[homesync](https://github.com/jrpotter/homesync)."
.as_ref(),
)?;
info!("Generated new <cyan>README.md</> file.");
Ok(())
}
pub fn push(pc: &PathConfig, repo: &mut Repository) -> Result<()> {
// First pull to make sure there are no conflicts when we push our changes.
// This will also perform validation and construct our local and remote
@ -141,6 +161,7 @@ pub fn push(pc: &PathConfig, repo: &mut Repository) -> Result<()> {
let refspec = format!("refs/heads/{}", &pc.config.repos.remote.branch);
repo.set_head(&refspec)?;
create_readme(repo)?;
// The index corresponds to our staging area. We add all files and write out
// to a tree. The resulting tree can be found using `git ls-tree <oid>`.