opts.go 988 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package run
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/docker/api/containers"
  7. )
  8. type runOpts struct {
  9. name string
  10. publish []string
  11. }
  12. func toPorts(ports []string) ([]containers.Port, error) {
  13. var result []containers.Port
  14. for _, port := range ports {
  15. parts := strings.Split(port, ":")
  16. if len(parts) != 2 {
  17. return nil, fmt.Errorf("unable to parse ports %q", port)
  18. }
  19. source, err := strconv.Atoi(parts[0])
  20. if err != nil {
  21. return nil, err
  22. }
  23. destination, err := strconv.Atoi(parts[1])
  24. if err != nil {
  25. return nil, err
  26. }
  27. result = append(result, containers.Port{
  28. Source: uint32(source),
  29. Destination: uint32(destination),
  30. })
  31. }
  32. return result, nil
  33. }
  34. func (r *runOpts) toContainerConfig(image string) (containers.ContainerConfig, error) {
  35. publish, err := toPorts(r.publish)
  36. if err != nil {
  37. return containers.ContainerConfig{}, err
  38. }
  39. return containers.ContainerConfig{
  40. ID: r.name,
  41. Image: image,
  42. Ports: publish,
  43. }, nil
  44. }