···
11
+
"github.com/urfave/cli/v3"
14
+
// The hook command is nested like so:
16
+
// knot hook --[flags] [hook]
17
+
func Command() *cli.Command {
18
+
return &cli.Command{
20
+
Usage: "run git hooks",
24
+
Usage: "base directory for git repos",
28
+
Usage: "git user's did",
31
+
Name: "user-handle",
32
+
Usage: "git user's handle",
35
+
Name: "internal-api",
36
+
Usage: "endpoint for the internal API",
37
+
Value: "http://localhost:5444",
40
+
Commands: []*cli.Command{
42
+
Name: "post-recieve",
43
+
Usage: "sends a post-recieve hook to the knot (waits for stdin)",
44
+
Action: postRecieve,
50
+
func postRecieve(ctx context.Context, cmd *cli.Command) error {
51
+
gitDir := cmd.String("git-dir")
52
+
userDid := cmd.String("user-did")
53
+
userHandle := cmd.String("user-handle")
54
+
endpoint := cmd.String("internal-api")
56
+
payloadReader := bufio.NewReader(os.Stdin)
57
+
payload, _ := payloadReader.ReadString('\n')
59
+
client := &http.Client{}
61
+
req, err := http.NewRequest("POST", endpoint+"/hooks/post-receive", strings.NewReader(payload))
63
+
return fmt.Errorf("failed to create request: %w", err)
66
+
req.Header.Set("Content-Type", "text/plain")
67
+
req.Header.Set("X-Git-Dir", gitDir)
68
+
req.Header.Set("X-Git-User-Did", userDid)
69
+
req.Header.Set("X-Git-User-Handle", userHandle)
71
+
resp, err := client.Do(req)
73
+
return fmt.Errorf("failed to execute request: %w", err)
75
+
defer resp.Body.Close()
77
+
if resp.StatusCode != http.StatusOK {
78
+
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)