- Notifications
You must be signed in to change notification settings - Fork945
pretty print json#114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package main | ||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
stdlog "log" | ||
@@ -39,12 +41,20 @@ var ( | ||
logFile := viper.GetString("log-file") | ||
readOnly := viper.GetBool("read-only") | ||
exportTranslations := viper.GetBool("export-translations") | ||
prettyPrintJSON := viper.GetBool("pretty-print-json") | ||
logger, err := initLogger(logFile) | ||
if err != nil { | ||
stdlog.Fatal("Failed to initialize logger:", err) | ||
} | ||
logCommands := viper.GetBool("enable-command-logging") | ||
cfg := runConfig{ | ||
readOnly: readOnly, | ||
logger: logger, | ||
logCommands: logCommands, | ||
exportTranslations: exportTranslations, | ||
prettyPrintJSON: prettyPrintJSON, | ||
} | ||
if err := runStdioServer(cfg); err != nil { | ||
stdlog.Fatal("failed to run stdio server:", err) | ||
} | ||
}, | ||
@@ -60,13 +70,15 @@ func init() { | ||
rootCmd.PersistentFlags().Bool("enable-command-logging", false, "When enabled, the server will log all command requests and responses to the log file") | ||
rootCmd.PersistentFlags().Bool("export-translations", false, "Save translations to a JSON file") | ||
rootCmd.PersistentFlags().String("gh-host", "", "Specify the GitHub hostname (for GitHub Enterprise etc.)") | ||
rootCmd.PersistentFlags().Bool("pretty-print-json", false, "Pretty print JSON output") | ||
// Bind flag to viper | ||
_ = viper.BindPFlag("read-only", rootCmd.PersistentFlags().Lookup("read-only")) | ||
_ = viper.BindPFlag("log-file", rootCmd.PersistentFlags().Lookup("log-file")) | ||
_ = viper.BindPFlag("enable-command-logging", rootCmd.PersistentFlags().Lookup("enable-command-logging")) | ||
_ = viper.BindPFlag("export-translations", rootCmd.PersistentFlags().Lookup("export-translations")) | ||
_ = viper.BindPFlag("gh-host", rootCmd.PersistentFlags().Lookup("gh-host")) | ||
_ = viper.BindPFlag("pretty-print-json", rootCmd.PersistentFlags().Lookup("pretty-print-json")) | ||
// Add subcommands | ||
rootCmd.AddCommand(stdioCmd) | ||
@@ -95,15 +107,36 @@ func initLogger(outPath string) (*log.Logger, error) { | ||
return logger, nil | ||
} | ||
type runConfig struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'd probably just call this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. going to leave it so you don't have to do another review | ||
readOnly bool | ||
logger *log.Logger | ||
logCommands bool | ||
exportTranslations bool | ||
prettyPrintJSON bool | ||
} | ||
// JSONPrettyPrintWriter is a Writer that pretty prints input to indented JSON | ||
type JSONPrettyPrintWriter struct { | ||
writer io.Writer | ||
} | ||
func (j JSONPrettyPrintWriter) Write(p []byte) (n int, err error) { | ||
var prettyJSON bytes.Buffer | ||
if err := json.Indent(&prettyJSON, p, "", "\t"); err != nil { | ||
return 0, err | ||
} | ||
return j.writer.Write(prettyJSON.Bytes()) | ||
} | ||
func runStdioServer(cfg runConfig) error { | ||
// Create app context | ||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||
defer stop() | ||
// Create GH client | ||
token := os.Getenv("GITHUB_PERSONAL_ACCESS_TOKEN") | ||
if token == "" { | ||
cfg.logger.Fatal("GITHUB_PERSONAL_ACCESS_TOKEN not set") | ||
} | ||
ghClient := gogithub.NewClient(nil).WithAuthToken(token) | ||
ghClient.UserAgent = fmt.Sprintf("github-mcp-server/%s", version) | ||
@@ -125,13 +158,13 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT | ||
t, dumpTranslations := translations.TranslationHelper() | ||
// Create | ||
ghServer := github.NewServer(ghClient,cfg.readOnly, t) | ||
stdioServer := server.NewStdioServer(ghServer) | ||
stdLogger := stdlog.New(cfg.logger.Writer(), "stdioserver", 0) | ||
stdioServer.SetErrorLogger(stdLogger) | ||
ifcfg.exportTranslations { | ||
// Once server is initialized, all translations are loaded | ||
dumpTranslations() | ||
} | ||
@@ -141,11 +174,14 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT | ||
go func() { | ||
in, out := io.Reader(os.Stdin), io.Writer(os.Stdout) | ||
ifcfg.logCommands { | ||
loggedIO := iolog.NewIOLogger(in, out,cfg.logger) | ||
in, out = loggedIO, loggedIO | ||
} | ||
if cfg.prettyPrintJSON { | ||
out = JSONPrettyPrintWriter{writer: out} | ||
} | ||
errC <- stdioServer.Listen(ctx, in, out) | ||
}() | ||
@@ -155,7 +191,7 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT | ||
// Wait for shutdown signal | ||
select { | ||
case <-ctx.Done(): | ||
cfg.logger.Infof("shutting down server...") | ||
case err := <-errC: | ||
if err != nil { | ||
return fmt.Errorf("error running server: %w", err) | ||