guard: read the motd from a file #382

merged
opened by ptr.pet targeting master from [deleted fork]: master
Changed files
+18 -2
guard
+18 -2
guard/guard.go
··· 2 2 3 3 import ( 4 4 "context" 5 + "errors" 5 6 "fmt" 6 7 "log/slog" 7 8 "net/http" ··· 43 44 Usage: "internal API endpoint", 44 45 Value: "http://localhost:5444", 45 46 }, 47 + &cli.StringFlag{ 48 + Name: "motd-file", 49 + Usage: "path to message of the day file", 50 + Value: "/home/git/motd", 51 + }, 46 52 }, 47 53 } 48 54 } ··· 54 60 gitDir := cmd.String("git-dir") 55 61 logPath := cmd.String("log-path") 56 62 endpoint := cmd.String("internal-api") 63 + motdFile := cmd.String("motd-file") 57 64 58 65 logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) 59 66 if err != nil { ··· 149 156 "fullPath", fullPath, 150 157 "client", clientIP) 151 158 159 + var motd string 160 + if motdRaw, err := os.ReadFile(motdFile); err != nil { 161 + if !errors.Is(err, os.ErrNotExist) { 162 + l.Error("failed to read motd file", "error", err) 163 + } 164 + motd = "Welcome to this knot!\n" 165 + } else { 166 + motd = string(motdRaw) 167 + } 152 168 if gitCommand == "git-upload-pack" { 153 - fmt.Fprintf(os.Stderr, "\x02%s\n", "Welcome to this knot!") 169 + fmt.Fprintf(os.Stderr, "\x02%s", motd) 154 170 } else { 155 - fmt.Fprintf(os.Stderr, "%s\n", "Welcome to this knot!") 171 + fmt.Fprintf(os.Stderr, "%s", motd) 156 172 } 157 173 158 174 gitCmd := exec.Command(gitCommand, fullPath)