github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/formatter/json/driver.go (about)

     1  package json
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/docker/app/internal/formatter"
     7  	composetypes "github.com/docker/cli/cli/compose/types"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  func init() {
    12  	formatter.Register("json", &Driver{})
    13  }
    14  
    15  // Driver is the json implementation of formatter drivers.
    16  type Driver struct{}
    17  
    18  // Format creates a JSON document from the source config.
    19  func (d *Driver) Format(config *composetypes.Config) (string, error) {
    20  	result, err := json.MarshalIndent(config, "", "  ")
    21  	if err != nil {
    22  		return "", errors.Wrap(err, "failed to produce json structure")
    23  	}
    24  	return string(result) + "\n", nil
    25  }