Skip to content

Commit 164bc05

Browse files
committed
pretty print json
1 parent 52bed28 commit 164bc05

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cmd/github-mcp-server/main.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"bytes"
45
"context"
6+
"encoding/json"
57
"fmt"
68
"io"
79
stdlog "log"
@@ -39,6 +41,7 @@ var (
3941
logFile := viper.GetString("log-file")
4042
readOnly := viper.GetBool("read-only")
4143
exportTranslations := viper.GetBool("export-translations")
44+
prettyPrintJSON := viper.GetBool("pretty-print-json")
4245
logger, err := initLogger(logFile)
4346
if err != nil {
4447
stdlog.Fatal("Failed to initialize logger:", err)
@@ -49,6 +52,7 @@ var (
4952
logger: logger,
5053
logCommands: logCommands,
5154
exportTranslations: exportTranslations,
55+
prettyPrintJSON: prettyPrintJSON,
5256
}
5357
if err := runStdioServer(cfg); err != nil {
5458
stdlog.Fatal("failed to run stdio server:", err)
@@ -66,13 +70,15 @@ func init() {
6670
rootCmd.PersistentFlags().Bool("enable-command-logging", false, "When enabled, the server will log all command requests and responses to the log file")
6771
rootCmd.PersistentFlags().Bool("export-translations", false, "Save translations to a JSON file")
6872
rootCmd.PersistentFlags().String("gh-host", "", "Specify the GitHub hostname (for GitHub Enterprise etc.)")
73+
rootCmd.PersistentFlags().Bool("pretty-print-json", false, "Pretty print JSON output")
6974

7075
// Bind flag to viper
7176
_ = viper.BindPFlag("read-only", rootCmd.PersistentFlags().Lookup("read-only"))
7277
_ = viper.BindPFlag("log-file", rootCmd.PersistentFlags().Lookup("log-file"))
7378
_ = viper.BindPFlag("enable-command-logging", rootCmd.PersistentFlags().Lookup("enable-command-logging"))
7479
_ = viper.BindPFlag("export-translations", rootCmd.PersistentFlags().Lookup("export-translations"))
7580
_ = viper.BindPFlag("gh-host", rootCmd.PersistentFlags().Lookup("gh-host"))
81+
_ = viper.BindPFlag("pretty-print-json", rootCmd.PersistentFlags().Lookup("pretty-print-json"))
7682

7783
// Add subcommands
7884
rootCmd.AddCommand(stdioCmd)
@@ -106,6 +112,20 @@ type runConfig struct {
106112
logger *log.Logger
107113
logCommands bool
108114
exportTranslations bool
115+
prettyPrintJSON bool
116+
}
117+
118+
// JSONPrettyPrintWriter is a Writer that pretty prints input to indented JSON
119+
type JSONPrettyPrintWriter struct {
120+
writer io.Writer
121+
}
122+
123+
func (j JSONPrettyPrintWriter) Write(p []byte) (n int, err error) {
124+
var prettyJSON bytes.Buffer
125+
if err := json.Indent(&prettyJSON, p, "", "\t"); err != nil {
126+
return 0, err
127+
}
128+
return j.writer.Write(prettyJSON.Bytes())
109129
}
110130

111131
func runStdioServer(cfg runConfig) error {
@@ -159,6 +179,9 @@ func runStdioServer(cfg runConfig) error {
159179
in, out = loggedIO, loggedIO
160180
}
161181

182+
if cfg.prettyPrintJSON {
183+
out = JSONPrettyPrintWriter{writer: out}
184+
}
162185
errC <- stdioServer.Listen(ctx, in, out)
163186
}()
164187

0 commit comments

Comments
 (0)