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