github.com/portworx/docker@v1.12.1/api/client/bundlefile/bundlefile.go (about)

     1  // +build experimental
     2  
     3  package bundlefile
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  	"io"
     9  )
    10  
    11  // Bundlefile stores the contents of a bundlefile
    12  type Bundlefile struct {
    13  	Version  string
    14  	Services map[string]Service
    15  }
    16  
    17  // Service is a service from a bundlefile
    18  type Service struct {
    19  	Image      string
    20  	Command    []string          `json:",omitempty"`
    21  	Args       []string          `json:",omitempty"`
    22  	Env        []string          `json:",omitempty"`
    23  	Labels     map[string]string `json:",omitempty"`
    24  	Ports      []Port            `json:",omitempty"`
    25  	WorkingDir *string           `json:",omitempty"`
    26  	User       *string           `json:",omitempty"`
    27  	Networks   []string          `json:",omitempty"`
    28  }
    29  
    30  // Port is a port as defined in a bundlefile
    31  type Port struct {
    32  	Protocol string
    33  	Port     uint32
    34  }
    35  
    36  // LoadFile loads a bundlefile from a path to the file
    37  func LoadFile(reader io.Reader) (*Bundlefile, error) {
    38  	bundlefile := &Bundlefile{}
    39  
    40  	decoder := json.NewDecoder(reader)
    41  	if err := decoder.Decode(bundlefile); err != nil {
    42  		switch jsonErr := err.(type) {
    43  		case *json.SyntaxError:
    44  			return nil, fmt.Errorf(
    45  				"JSON syntax error at byte %v: %s",
    46  				jsonErr.Offset,
    47  				jsonErr.Error())
    48  		case *json.UnmarshalTypeError:
    49  			return nil, fmt.Errorf(
    50  				"Unexpected type at byte %v. Expected %s but received %s.",
    51  				jsonErr.Offset,
    52  				jsonErr.Type,
    53  				jsonErr.Value)
    54  		}
    55  		return nil, err
    56  	}
    57  
    58  	return bundlefile, nil
    59  }
    60  
    61  // Print writes the contents of the bundlefile to the output writer
    62  // as human readable json
    63  func Print(out io.Writer, bundle *Bundlefile) error {
    64  	bytes, err := json.MarshalIndent(*bundle, "", "    ")
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	_, err = out.Write(bytes)
    70  	return err
    71  }