Add volumes to run command

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-05-07 04:58:04 +02:00
parent 7603a3b832
commit b25a6b4bd6
6 changed files with 183 additions and 8 deletions

54
cli/cmd/run/opts.go Normal file
View file

@ -0,0 +1,54 @@
package run
import (
"fmt"
"strconv"
"strings"
"github.com/docker/api/containers"
)
type runOpts struct {
name string
publish []string
volumes []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])
if err != nil {
return nil, err
}
destination, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
result = append(result, containers.Port{
HostPort: uint32(source),
ContainerPort: uint32(destination),
})
}
return result, nil
}
func (r *runOpts) toContainerConfig(image string) (containers.ContainerConfig, error) {
publish, err := toPorts(r.publish)
if err != nil {
return containers.ContainerConfig{}, err
}
return containers.ContainerConfig{
ID: r.name,
Image: image,
Ports: publish,
Volumes: r.volumes,
}, nil
}

View file

@ -54,6 +54,7 @@ func Command() *cobra.Command {
cmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT")
cmd.Flags().StringVar(&opts.Name, "name", getRandomName(), "Assign a name to the container")
cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container")
cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: user:key@my_share:/absolute/path/to/target")
return cmd
}
@ -64,18 +65,17 @@ func runRun(ctx context.Context, image string, opts run.Opts) error {
return err
}
project, err := opts.ToContainerConfig(image)
containerConfig, err := opts.ToContainerConfig(image)
if err != nil {
return err
}
if err = c.ContainerService().Run(ctx, project); err != nil {
if err = c.ContainerService().Run(ctx, containerConfig); err != nil {
return err
}
fmt.Println(opts.Name)
return nil
}
func getRandomName() string {