bundlefile.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // +build experimental
  2. package bundlefile
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io"
  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(reader io.Reader) (*Bundlefile, error) {
  32. bundlefile := &Bundlefile{}
  33. decoder := json.NewDecoder(reader)
  34. if err := decoder.Decode(bundlefile); err != nil {
  35. switch jsonErr := err.(type) {
  36. case *json.SyntaxError:
  37. return nil, fmt.Errorf(
  38. "JSON syntax error at byte %v: %s",
  39. jsonErr.Offset,
  40. jsonErr.Error())
  41. case *json.UnmarshalTypeError:
  42. return nil, fmt.Errorf(
  43. "Unexpected type at byte %v. Expected %s but received %s.",
  44. jsonErr.Offset,
  45. jsonErr.Type,
  46. jsonErr.Value)
  47. }
  48. return nil, err
  49. }
  50. return bundlefile, nil
  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. _, err = out.Write(bytes)
  60. return err
  61. }