From d71bc60d2f6c0861face00ef867c581b6cb1a1f1 Mon Sep 17 00:00:00 2001 From: dusk Date: Thu, 31 Jul 2025 23:35:22 +0300 Subject: [PATCH] guard: read the motd from a file Change-Id: kvuxwxxzvvqtvypqzlotlwkqmuttqwun Signed-off-by: dusk Change-Id: kvuxwxxzvvqtvypqzlotlwkqmuttqwun --- guard/guard.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/guard/guard.go b/guard/guard.go index 8bc21ff..02a410a 100644 --- a/guard/guard.go +++ b/guard/guard.go @@ -2,6 +2,7 @@ package guard import ( "context" + "errors" "fmt" "log/slog" "net/http" @@ -43,6 +44,11 @@ func Command() *cli.Command { Usage: "internal API endpoint", Value: "http://localhost:5444", }, + &cli.StringFlag{ + Name: "motd-file", + Usage: "path to message of the day file", + Value: "/home/git/motd", + }, }, } } @@ -54,6 +60,7 @@ func Run(ctx context.Context, cmd *cli.Command) error { gitDir := cmd.String("git-dir") logPath := cmd.String("log-path") endpoint := cmd.String("internal-api") + motdFile := cmd.String("motd-file") logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { @@ -149,10 +156,19 @@ func Run(ctx context.Context, cmd *cli.Command) error { "fullPath", fullPath, "client", clientIP) + var motd string + if motdRaw, err := os.ReadFile(motdFile); err != nil { + if !errors.Is(err, os.ErrNotExist) { + l.Error("failed to read motd file", "error", err) + } + motd = "Welcome to this knot!\n" + } else { + motd = string(motdRaw) + } if gitCommand == "git-upload-pack" { - fmt.Fprintf(os.Stderr, "\x02%s\n", "Welcome to this knot!") + fmt.Fprintf(os.Stderr, "\x02%s", motd) } else { - fmt.Fprintf(os.Stderr, "%s\n", "Welcome to this knot!") + fmt.Fprintf(os.Stderr, "%s", motd) } gitCmd := exec.Command(gitCommand, fullPath) -- 2.43.0