bundlefile.go 1.6 KB

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