bundlefile.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // +build experimental
  2. package bundlefile
  3. import (
  4. "encoding/json"
  5. "io"
  6. "os"
  7. )
  8. // Bundlefile stores the contents of a bundlefile
  9. type Bundlefile struct {
  10. Version string
  11. Services map[string]Service
  12. }
  13. // Service is a service from a bundlefile
  14. type Service struct {
  15. Image string
  16. Command []string `json:",omitempty"`
  17. Args []string `json:",omitempty"`
  18. Env []string `json:",omitempty"`
  19. Labels map[string]string `json:",omitempty"`
  20. Ports []Port `json:",omitempty"`
  21. WorkingDir *string `json:",omitempty"`
  22. User *string `json:",omitempty"`
  23. Networks []string `json:",omitempty"`
  24. }
  25. // Port is a port as defined in a bundlefile
  26. type Port struct {
  27. Protocol string
  28. Port uint32
  29. }
  30. // LoadFile loads a bundlefile from a path to the file
  31. func LoadFile(path string) (*Bundlefile, error) {
  32. reader, err := os.Open(path)
  33. if err != nil {
  34. return nil, err
  35. }
  36. bundlefile := &Bundlefile{}
  37. if err := json.NewDecoder(reader).Decode(bundlefile); err != nil {
  38. return nil, err
  39. }
  40. return bundlefile, err
  41. }
  42. // Print writes the contents of the bundlefile to the output writer
  43. // as human readable json
  44. func Print(out io.Writer, bundle *Bundlefile) error {
  45. bytes, err := json.MarshalIndent(*bundle, "", " ")
  46. if err != nil {
  47. return err
  48. }
  49. _, err = out.Write(bytes)
  50. return err
  51. }