summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-07-05 21:55:06 +0200
committerxengineering <me@xengineering.eu>2024-07-05 21:55:06 +0200
commit6af40875aadc742101c128fad37af6065996c3ed (patch)
treea49962ce3327766a991243c1cba034ad037bcb45
parent72b68a55e417273110f5db13c95c8916e1f7b809 (diff)
downloadcraft-6af40875aadc742101c128fad37af6065996c3ed.tar
craft-6af40875aadc742101c128fad37af6065996c3ed.tar.zst
craft-6af40875aadc742101c128fad37af6065996c3ed.zip
Determine and validate Git hook type
Since craft should only be referenced by a symlink like that: ln -s /usr/bin/craft myrepo/.git/hooks/post-receive It is important to detect the hook type, in this case 'post-receive'. This commit also validates that the determined hook type is supported. Currently only the post-receive hook is supported.
-rw-r--r--main.go33
1 files changed, 33 insertions, 0 deletions
diff --git a/main.go b/main.go
index 5ddc386..59f864f 100644
--- a/main.go
+++ b/main.go
@@ -8,9 +8,15 @@ import (
"io"
"log"
"os"
+ "strings"
)
func main() {
+ log.SetFlags(0)
+
+ log.Println("Starting craft")
+ log.Printf("Git hook type: %s\n", getHookType())
+
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
@@ -22,6 +28,33 @@ func main() {
}
}
+func getHookType() string {
+ args := len(os.Args)
+ if args != 1 {
+ log.Fatalf("Expected zero arguments but %d were given", args-1)
+ }
+ prefix := `hooks/`
+ path := os.Args[0]
+ hookType := strings.TrimPrefix(path, prefix)
+ if path == hookType {
+ log.Fatalf("Hook path '%s' has no '%s' prefix", path, prefix)
+ }
+ validateHookType(hookType)
+ return hookType
+}
+
+func validateHookType(hookType string) {
+ validTypes := [...]string{
+ `post-receive`,
+ }
+ for _, currentType := range validTypes {
+ if hookType == currentType {
+ return
+ }
+ }
+ log.Fatalf("Not supported Git hook type '%s'\n", hookType)
+}
+
type update struct {
old, updated, ref string
}