From fcea1153cc2c49c60a4fa6de48fde765d8d7e95a Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Thu, 15 Jun 2023 12:12:01 -0700 Subject: [PATCH] use user provided git author details --- .env.example | 5 ++++- README.md | 2 ++ main.go | 11 +++++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 99403c1..6aea8eb 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,7 @@ mongoURI=mongodb://localhost:27017 databases=db_name_1, db_name_2, ... # Github repository ssh link github=git@github:Username/MongoDB_Backup_Repo.git - +# Github Username for git commit --author +github_author=Github_Username +# Github email for git commit --author +github_email=john_does@example.com diff --git a/README.md b/README.md index c95e1de..f4aadff 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ A script to automatically handle the backing up of MongoDB databases and optiona mongoURI=mongodb://localhost:27017 databases=db_name_1, db_name_2, ... github=git@github:Username/MongoDB_Backup_Repo.git + github_author=Github_Username + github_email=john_doe@example.com ``` **Note:** If you wish to backup more than one database at a time, be sure to seperate the database names with a comma and a space as follows "`, `". diff --git a/main.go b/main.go index ea80a3c..35b4782 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,6 @@ const dtFormat string = "2006-01-02 15:04:05 Monday" func Handle(err error) { if err != nil { - LogToFile(err.Error()) log.Panic(err.Error()) } } @@ -42,8 +41,16 @@ func UploadToGithub(archiveDir string) { now := time.Now() + var author string = os.Getenv("github_author") + var email string = os.Getenv("github_email") + + if author == "" || email == "" { + LogToFile("ERROR - github link provided but no author or email, add to .env: github_author= and github_email=") + log.Panic("Missing required .env: github_author, github_email") + } + // Commit archive files - cmd = exec.Command("git", "commit", "-m", fmt.Sprintf("'%s''", now.Format(dtFormat))) + cmd = exec.Command("git", "commit", "-m", fmt.Sprintf("'%s''", now.Format(dtFormat)), fmt.Springf("--author=\"%s <%s>\"", author, email)) cmd.Dir = archiveDir _, err = cmd.Output() Handle(err)