From 8f760c6584e52e7f303c14e5bc72afa4ebc8055b 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 | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/guard/guard.go b/guard/guard.go index 8bc21ff..9e6ff6b 100644 --- a/guard/guard.go +++ b/guard/guard.go @@ -2,7 +2,9 @@ package guard import ( "context" + "errors" "fmt" + "io" "log/slog" "net/http" "net/url" @@ -43,6 +45,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 +61,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,11 +157,19 @@ func Run(ctx context.Context, cmd *cli.Command) error { "fullPath", fullPath, "client", clientIP) - if gitCommand == "git-upload-pack" { - fmt.Fprintf(os.Stderr, "\x02%s\n", "Welcome to this knot!") + var motdReader io.Reader + if reader, err := os.Open(motdFile); err != nil { + if !errors.Is(err, os.ErrNotExist) { + l.Error("failed to read motd file", "error", err) + } + motdReader = strings.NewReader("Welcome to this knot!\n") } else { - fmt.Fprintf(os.Stderr, "%s\n", "Welcome to this knot!") + motdReader = reader + } + if gitCommand == "git-upload-pack" { + io.WriteString(os.Stderr, "\x02") } + io.Copy(os.Stderr, motdReader) gitCmd := exec.Command(gitCommand, fullPath) gitCmd.Stdout = os.Stdout -- 2.43.0