github.com/kobeld/docker@v1.12.0-rc1/api/client/bundlefile/bundlefile.go (about)

     1  // +build experimental
     2  
     3  package bundlefile
     4  
     5  import (
     6  	"encoding/json"
     7  	"io"
     8  	"os"
     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(path string) (*Bundlefile, error) {
    38  	reader, err := os.Open(path)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	bundlefile := &Bundlefile{}
    44  
    45  	if err := json.NewDecoder(reader).Decode(bundlefile); err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	return bundlefile, err
    50  }
    51  
    52  // Print writes the contents of the bundlefile to the output writer
    53  // as human readable json
    54  func Print(out io.Writer, bundle *Bundlefile) error {
    55  	bytes, err := json.MarshalIndent(*bundle, "", "    ")
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	_, err = out.Write(bytes)
    61  	return err
    62  }