From b8f4ee66c9bcf1e31b233a9beaa0aa693eae2a89 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 6 Jul 2024 09:14:10 +0200 Subject: Modify log output - exit message - more consistent messages - refactoring to implement this --- main.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 59f864f..08a97b8 100644 --- a/main.go +++ b/main.go @@ -14,17 +14,17 @@ import ( func main() { log.SetFlags(0) + defer log.Println("Exiting craft") log.Println("Starting craft") log.Printf("Git hook type: %s\n", getHookType()) - - cwd, err := os.Getwd() - if err != nil { - log.Fatal(err) - } - fmt.Printf("Current working directory: %s\n", cwd) + log.Printf("Git repository: %s\n", getRepositoryPath()) for { - fmt.Println(getUpdate()) + eof, update := getUpdate() + if eof { + break + } + log.Printf("Git ref update: %s\n", update) } } @@ -55,24 +55,32 @@ func validateHookType(hookType string) { log.Fatalf("Not supported Git hook type '%s'\n", hookType) } +func getRepositoryPath() string { + cwd, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + return cwd +} + type update struct { old, updated, ref string } func (u update) String() string { - ret := fmt.Sprintf("'%s' updated from '%s' to '%s'", u.ref, u.old, u.updated) + ret := fmt.Sprintf("'%s' from '%s' to '%s'", u.ref, u.old, u.updated) return ret } -func getUpdate() update { +func getUpdate() (bool, update) { var u update _, err := fmt.Scanf("%s %s %s\n", &u.old, &u.updated, &u.ref) if err != nil { if errors.Is(err, io.EOF) { - os.Exit(0) + return true, u } else { log.Fatal(err) } } - return u + return false, u } -- cgit v1.2.3-70-g09d2