github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/formatter/common.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package formatter
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"errors"
    23  	"fmt"
    24  	"io"
    25  	"text/template"
    26  
    27  	"github.com/docker/cli/templates"
    28  )
    29  
    30  // Flusher is implemented by text/tabwriter.Writer
    31  type Flusher interface {
    32  	Flush() error
    33  }
    34  
    35  // FormatSlice formats the slice with `--format` flag.
    36  //
    37  // --format="" (default): JSON
    38  // --format='{{json .}}': JSON lines
    39  //
    40  // FormatSlice is expected to be only used for `nerdctl OBJECT inspect` commands.
    41  func FormatSlice(format string, writer io.Writer, x []interface{}) error {
    42  	var tmpl *template.Template
    43  	switch format {
    44  	case "":
    45  		b, err := json.MarshalIndent(x, "", "    ")
    46  		if err != nil {
    47  			return err
    48  		}
    49  		fmt.Fprintln(writer, string(b))
    50  	case "raw", "table", "wide":
    51  		return errors.New("unsupported format: \"raw\", \"table\", and \"wide\"")
    52  	default:
    53  		var err error
    54  		tmpl, err = ParseTemplate(format)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		for _, f := range x {
    59  			var b bytes.Buffer
    60  			if err := tmpl.Execute(&b, f); err != nil {
    61  				if _, ok := err.(template.ExecError); ok {
    62  					// FallBack to Raw Format
    63  					if err = tryRawFormat(&b, f, tmpl); err != nil {
    64  						return err
    65  					}
    66  				}
    67  			}
    68  			if _, err = fmt.Fprintln(writer, b.String()); err != nil {
    69  				return err
    70  			}
    71  		}
    72  	}
    73  	return nil
    74  }
    75  
    76  func tryRawFormat(b *bytes.Buffer, f interface{}, tmpl *template.Template) error {
    77  	m, err := json.MarshalIndent(f, "", "    ")
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	var raw interface{}
    83  	rdr := bytes.NewReader(m)
    84  	dec := json.NewDecoder(rdr)
    85  	dec.UseNumber()
    86  
    87  	if rawErr := dec.Decode(&raw); rawErr != nil {
    88  		return fmt.Errorf("unable to read inspect data: %v", rawErr)
    89  	}
    90  
    91  	tmplMissingKey := tmpl.Option("missingkey=error")
    92  	if rawErr := tmplMissingKey.Execute(b, raw); rawErr != nil {
    93  		return fmt.Errorf("template parsing error: %v", rawErr)
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  // ParseTemplate wraps github.com/docker/cli/templates.Parse() to allow `json` as an alias of `{{json .}}`.
   100  // ParseTemplate can be removed when https://github.com/docker/cli/pull/3355 gets merged and tagged (Docker 22.XX).
   101  func ParseTemplate(format string) (*template.Template, error) {
   102  	aliases := map[string]string{
   103  		"json": "{{json .}}",
   104  	}
   105  	if alias, ok := aliases[format]; ok {
   106  		format = alias
   107  	}
   108  	return templates.Parse(format)
   109  }