2016-06-08 13:47:46 -04:00
|
|
|
package bundlefile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-06-16 17:55:50 -04:00
|
|
|
"fmt"
|
2016-06-08 13:47:46 -04:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bundlefile stores the contents of a bundlefile
|
|
|
|
type Bundlefile struct {
|
|
|
|
Version string
|
|
|
|
Services map[string]Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service is a service from a bundlefile
|
|
|
|
type Service struct {
|
|
|
|
Image string
|
|
|
|
Command []string `json:",omitempty"`
|
|
|
|
Args []string `json:",omitempty"`
|
|
|
|
Env []string `json:",omitempty"`
|
|
|
|
Labels map[string]string `json:",omitempty"`
|
|
|
|
Ports []Port `json:",omitempty"`
|
|
|
|
WorkingDir *string `json:",omitempty"`
|
|
|
|
User *string `json:",omitempty"`
|
|
|
|
Networks []string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Port is a port as defined in a bundlefile
|
|
|
|
type Port struct {
|
|
|
|
Protocol string
|
|
|
|
Port uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFile loads a bundlefile from a path to the file
|
2016-06-16 17:55:50 -04:00
|
|
|
func LoadFile(reader io.Reader) (*Bundlefile, error) {
|
2016-06-08 13:47:46 -04:00
|
|
|
bundlefile := &Bundlefile{}
|
|
|
|
|
2016-06-16 17:55:50 -04:00
|
|
|
decoder := json.NewDecoder(reader)
|
|
|
|
if err := decoder.Decode(bundlefile); err != nil {
|
|
|
|
switch jsonErr := err.(type) {
|
|
|
|
case *json.SyntaxError:
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"JSON syntax error at byte %v: %s",
|
|
|
|
jsonErr.Offset,
|
|
|
|
jsonErr.Error())
|
|
|
|
case *json.UnmarshalTypeError:
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Unexpected type at byte %v. Expected %s but received %s.",
|
|
|
|
jsonErr.Offset,
|
|
|
|
jsonErr.Type,
|
|
|
|
jsonErr.Value)
|
|
|
|
}
|
2016-06-08 13:47:46 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-16 17:55:50 -04:00
|
|
|
return bundlefile, nil
|
2016-06-08 13:47:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Print writes the contents of the bundlefile to the output writer
|
|
|
|
// as human readable json
|
|
|
|
func Print(out io.Writer, bundle *Bundlefile) error {
|
|
|
|
bytes, err := json.MarshalIndent(*bundle, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = out.Write(bytes)
|
|
|
|
return err
|
|
|
|
}
|