From 45ce792eb40bb71933bf436ce7195319885b560b Mon Sep 17 00:00:00 2001 From: Mark van Holsteijn Date: Fri, 25 Mar 2022 11:28:42 +0100 Subject: [PATCH] feat: specify git username and email on the command line --- README.md | 10 +++++++--- committer.go | 4 ++-- main.go | 21 ++++++++++++++++++--- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index adb315a..e70c79a 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,10 @@ git repository, without requiring complete write access to the repository. ## Usage ``` - cru list [--verbose] [--no-filename] [--repository=URL [--branch=BRANCH]] [PATH] ... - cru update [--verbose] [--dry-run] [(--resolve-digest|--resolve-tag)] [--repository=URL [--branch=BRANCH] [--commit=MESSAGE]] (--all | --image-reference=REFERENCE ...) [PATH] ... - cru serve [--verbose] [--dry-run] [--port=PORT] --repository=URL --branch=BRANCH [PATH] ... +Usage: + cru list [--verbose] [--no-filename] [--repository=URL [--branch=BRANCH] [(--username=USERNAME --email=EMAIL)] ] [PATH] ... + cru update [--verbose] [--dry-run] [(--resolve-digest|--resolve-tag)] [--repository=URL [--branch=BRANCH] [(--username=USERNAME --email=EMAIL)] [--commit=MESSAGE]] (--all | --image-reference=REFERENCE ...) [PATH] ... + cru serve [--verbose] [--dry-run] [--port=PORT] --repository=URL --branch=BRANCH [(--username=USERNAME --email=EMAIL)] [PATH] ... ``` ## Options @@ -32,6 +33,8 @@ git repository, without requiring complete write access to the repository. --commit=MESSAGE commit the changes with the specified message. --repository=URL to read and/or update. --branch=BRANCH to update. +--username=USERNAME to use for the commit [default: cru]. +--email=EMAIL to use for the commit [default: cru@binx.io]. --port=PORT to listen on, defaults to 8080 or PORT environment variable. ``` @@ -132,3 +135,4 @@ To install you have a number of different options: - cru is not context-aware: anything that looks like a container image references is updated. - cru will ignore any references to unqualified official images, like docker:latest or nginx:3. To update the official docker image references, prefix them with docker.io/ or docker.io/library/. - the time to find the alternate tag is proportional to the number of tags associated with the image. +- the default git commit username and email is cru / cru@binx.io. It is not taken from gitconfig. diff --git a/committer.go b/committer.go index 6e2c588..00689ce 100644 --- a/committer.go +++ b/committer.go @@ -32,8 +32,8 @@ func (c *Cru) Commit() (hash plumbing.Hash, err error) { if !c.DryRun { hash, err = c.workTree.Commit(c.CommitMsg, &git.CommitOptions{ Author: &object.Signature{ - Name: "cru", - Email: "cru@binx.io", + Name: c.Username, + Email: c.Email, When: time.Now(), }, }) diff --git a/main.go b/main.go index 68e4c5b..554bbc5 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,8 @@ type Cru struct { Url string `docopt:"--repository"` CommitMsg string `docopt:"--commit"` Branch string `docopt:"--branch"` + Username string + Email string imageRefs ref.ContainerImageReferences updatedFiles []string committedFiles []string @@ -178,13 +180,23 @@ func (c *Cru) ConnectToRepository() error { return nil } +func (c *Cru) ApplyDefaults() { + if c.Username == "" { + c.Username = "cru" + } + + if c.Email == "" { + c.Email = "cru@binx.io" + } +} + func main() { usage := `cru - container image reference updater Usage: - cru list [--verbose] [--no-filename] [--repository=URL [--branch=BRANCH]] [PATH] ... - cru update [--verbose] [--dry-run] [(--resolve-digest|--resolve-tag)] [--repository=URL [--branch=BRANCH] [--commit=MESSAGE]] (--all | --image-reference=REFERENCE ...) [PATH] ... - cru serve [--verbose] [--dry-run] [--port=PORT] --repository=URL --branch=BRANCH [PATH] ... + cru list [--verbose] [--no-filename] [--repository=URL [--branch=BRANCH] [(--username=USERNAME --email=EMAIL)] ] [PATH] ... + cru update [--verbose] [--dry-run] [(--resolve-digest|--resolve-tag)] [--repository=URL [--branch=BRANCH] [(--username=USERNAME --email=EMAIL)] [--commit=MESSAGE]] (--all | --image-reference=REFERENCE ...) [PATH] ... + cru serve [--verbose] [--dry-run] [--port=PORT] --repository=URL --branch=BRANCH [(--username=USERNAME --email=EMAIL)] [PATH] ... Options: --no-filename do not print the filename. @@ -197,6 +209,8 @@ Options: --commit=MESSAGE commit the changes with the specified message. --repository=URL to read and/or update. --branch=BRANCH to update. +--username=USERNAME to use for the commit [default: cru]. +--email=EMAIL to use for the commit [default: cru@binx.io]. --port=PORT to listen on, defaults to 8080 or PORT environment variable. ` cru := Cru{} @@ -209,6 +223,7 @@ Options: if err = args.Bind(&cru); err != nil { log.Fatal(err) } + cru.ApplyDefaults() if err = cru.ConnectToRepository(); err != nil { log.Fatal(err)