compose/cli/cmd/run/opts.go

53 lines
988 B
Go
Raw Normal View History

2020-05-04 10:28:42 +02:00
package run
2020-05-01 15:28:44 +02:00
import (
"fmt"
2020-05-01 16:03:33 +02:00
"strconv"
2020-05-01 15:28:44 +02:00
"strings"
"github.com/docker/api/containers"
)
type runOpts struct {
name string
publish []string
}
func toPorts(ports []string) ([]containers.Port, error) {
var result []containers.Port
for _, port := range ports {
parts := strings.Split(port, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("unable to parse ports %q", port)
}
source, err := strconv.Atoi(parts[0])
2020-05-01 16:03:33 +02:00
if err != nil {
return nil, err
}
destination, err := strconv.Atoi(parts[1])
2020-05-01 16:03:33 +02:00
if err != nil {
return nil, err
}
2020-05-01 15:28:44 +02:00
result = append(result, containers.Port{
2020-05-01 16:03:33 +02:00
Source: uint32(source),
Destination: uint32(destination),
2020-05-01 15:28:44 +02:00
})
}
return result, nil
}
func (r *runOpts) toContainerConfig(image string) (containers.ContainerConfig, error) {
2020-05-01 15:28:44 +02:00
publish, err := toPorts(r.publish)
if err != nil {
return containers.ContainerConfig{}, err
}
return containers.ContainerConfig{
ID: r.name,
Image: image,
Ports: publish,
}, nil
}