1
1
package main
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ "encoding/json"
5
7
"fmt"
6
8
"io"
7
9
stdlog "log"
39
41
logFile := viper .GetString ("log-file" )
40
42
readOnly := viper .GetBool ("read-only" )
41
43
exportTranslations := viper .GetBool ("export-translations" )
44
+ prettyPrintJSON := viper .GetBool ("pretty-print-json" )
42
45
logger , err := initLogger (logFile )
43
46
if err != nil {
44
47
stdlog .Fatal ("Failed to initialize logger:" , err )
49
52
logger : logger ,
50
53
logCommands : logCommands ,
51
54
exportTranslations : exportTranslations ,
55
+ prettyPrintJSON : prettyPrintJSON ,
52
56
}
53
57
if err := runStdioServer (cfg ); err != nil {
54
58
stdlog .Fatal ("failed to run stdio server:" , err )
@@ -66,13 +70,15 @@ func init() {
66
70
rootCmd .PersistentFlags ().Bool ("enable-command-logging" , false , "When enabled, the server will log all command requests and responses to the log file" )
67
71
rootCmd .PersistentFlags ().Bool ("export-translations" , false , "Save translations to a JSON file" )
68
72
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" )
69
74
70
75
// Bind flag to viper
71
76
_ = viper .BindPFlag ("read-only" , rootCmd .PersistentFlags ().Lookup ("read-only" ))
72
77
_ = viper .BindPFlag ("log-file" , rootCmd .PersistentFlags ().Lookup ("log-file" ))
73
78
_ = viper .BindPFlag ("enable-command-logging" , rootCmd .PersistentFlags ().Lookup ("enable-command-logging" ))
74
79
_ = viper .BindPFlag ("export-translations" , rootCmd .PersistentFlags ().Lookup ("export-translations" ))
75
80
_ = viper .BindPFlag ("gh-host" , rootCmd .PersistentFlags ().Lookup ("gh-host" ))
81
+ _ = viper .BindPFlag ("pretty-print-json" , rootCmd .PersistentFlags ().Lookup ("pretty-print-json" ))
76
82
77
83
// Add subcommands
78
84
rootCmd .AddCommand (stdioCmd )
@@ -106,6 +112,20 @@ type runConfig struct {
106
112
logger * log.Logger
107
113
logCommands bool
108
114
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 ())
109
129
}
110
130
111
131
func runStdioServer (cfg runConfig ) error {
@@ -159,6 +179,9 @@ func runStdioServer(cfg runConfig) error {
159
179
in , out = loggedIO , loggedIO
160
180
}
161
181
182
+ if cfg .prettyPrintJSON {
183
+ out = JSONPrettyPrintWriter {writer : out }
184
+ }
162
185
errC <- stdioServer .Listen (ctx , in , out )
163
186
}()
164
187
0 commit comments