MCPcopy Index your code
hub / github.com/cli/cli / Write

Method Write

pkg/cmdutil/json_flags.go:225–257  ·  view source on GitHub ↗

Write serializes data into JSON output written to w. If the object passed as data implements exportable, or if data is a map or slice of exportable object, ExportData() will be called on each object to obtain raw data for serialization.

(ios *iostreams.IOStreams, data interface{})

Source from the content-addressed store, hash-verified

223// or if data is a map or slice of exportable object, ExportData() will be called on each object to obtain
224// raw data for serialization.
225func (e *jsonExporter) Write(ios *iostreams.IOStreams, data interface{}) error {
226 buf := bytes.Buffer{}
227 encoder := json.NewEncoder(&buf)
228 encoder.SetEscapeHTML(false)
229 if err := encoder.Encode(e.exportData(reflect.ValueOf(data))); err != nil {
230 return err
231 }
232
233 w := ios.Out
234 if e.filter != "" {
235 indent := ""
236 if ios.IsStdoutTTY() {
237 indent = " "
238 }
239 if err := jq.EvaluateFormatted(&buf, w, e.filter, indent, ios.ColorEnabled()); err != nil {
240 return err
241 }
242 } else if e.template != "" {
243 t := template.New(w, ios.TerminalWidth(), ios.ColorEnabled())
244 if err := t.Parse(e.template); err != nil {
245 return err
246 }
247 if err := t.Execute(&buf); err != nil {
248 return err
249 }
250 return t.Flush()
251 } else if ios.ColorEnabled() {
252 return jsoncolor.Write(w, &buf, " ")
253 }
254
255 _, err := io.Copy(w, &buf)
256 return err
257}
258
259func (e *jsonExporter) exportData(v reflect.Value) interface{} {
260 switch v.Kind() {

Callers

nothing calls this directly

Calls 7

exportDataMethod · 0.95
WriteFunction · 0.92
IsStdoutTTYMethod · 0.80
ColorEnabledMethod · 0.80
TerminalWidthMethod · 0.80
FlushMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected