guard: read the motd from a file #381

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