mirror of https://github.com/docker/docs.git
Reformatting parseRun and partParse
This commit is contained in:
parent
d4c8fb9ee2
commit
476559458d
112
commands.go
112
commands.go
|
@ -1730,60 +1730,59 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Config, *HostConfig, *flag.FlagSet, error) {
|
func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Config, *HostConfig, *flag.FlagSet, error) {
|
||||||
|
var (
|
||||||
|
// FIXME: use utils.ListOpts for attach and volumes?
|
||||||
|
flAttach = NewAttachOpts()
|
||||||
|
flVolumes = NewPathOpts()
|
||||||
|
|
||||||
flHostname := cmd.String("h", "", "Container host name")
|
flPublish utils.ListOpts
|
||||||
flWorkingDir := cmd.String("w", "", "Working directory inside the container")
|
flExpose utils.ListOpts
|
||||||
flUser := cmd.String("u", "", "Username or UID")
|
flEnv utils.ListOpts
|
||||||
flDetach := cmd.Bool("d", false, "Detached mode: Run container in the background, print new container id")
|
flDns utils.ListOpts
|
||||||
flAttach := NewAttachOpts()
|
flVolumesFrom utils.ListOpts
|
||||||
|
flLxcOpts utils.ListOpts
|
||||||
|
flLinks utils.ListOpts
|
||||||
|
|
||||||
|
flAutoRemove = cmd.Bool("rm", false, "Automatically remove the container when it exits (incompatible with -d)")
|
||||||
|
flDetach = cmd.Bool("d", false, "Detached mode: Run container in the background, print new container id")
|
||||||
|
flNetwork = cmd.Bool("n", true, "Enable networking for this container")
|
||||||
|
flPrivileged = cmd.Bool("privileged", false, "Give extended privileges to this container")
|
||||||
|
flPublishAll = cmd.Bool("P", false, "Publish all exposed ports to the host interfaces")
|
||||||
|
flStdin = cmd.Bool("i", false, "Keep stdin open even if not attached")
|
||||||
|
flTty = cmd.Bool("t", false, "Allocate a pseudo-tty")
|
||||||
|
flContainerIDFile = cmd.String("cidfile", "", "Write the container ID to the file")
|
||||||
|
flEntrypoint = cmd.String("entrypoint", "", "Overwrite the default entrypoint of the image")
|
||||||
|
flHostname = cmd.String("h", "", "Container host name")
|
||||||
|
flMemoryString = cmd.String("m", "", "Memory limit (format: <number><optional unit>, where unit = b, k, m or g)")
|
||||||
|
flUser = cmd.String("u", "", "Username or UID")
|
||||||
|
flWorkingDir = cmd.String("w", "", "Working directory inside the container")
|
||||||
|
flCpuShares = cmd.Int64("c", 0, "CPU shares (relative weight)")
|
||||||
|
|
||||||
|
// For documentation purpose
|
||||||
|
_ = cmd.Bool("sig-proxy", true, "Proxify all received signal to the process (even in non-tty mode)")
|
||||||
|
_ = cmd.String("name", "", "Assign a name to the container")
|
||||||
|
)
|
||||||
cmd.Var(flAttach, "a", "Attach to stdin, stdout or stderr.")
|
cmd.Var(flAttach, "a", "Attach to stdin, stdout or stderr.")
|
||||||
flStdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
|
|
||||||
flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
|
||||||
flMemoryString := cmd.String("m", "", "Memory limit (format: <number><optional unit>, where unit = b, k, m or g)")
|
|
||||||
flContainerIDFile := cmd.String("cidfile", "", "Write the container ID to the file")
|
|
||||||
flNetwork := cmd.Bool("n", true, "Enable networking for this container")
|
|
||||||
flPrivileged := cmd.Bool("privileged", false, "Give extended privileges to this container")
|
|
||||||
flAutoRemove := cmd.Bool("rm", false, "Automatically remove the container when it exits (incompatible with -d)")
|
|
||||||
cmd.Bool("sig-proxy", true, "Proxify all received signal to the process (even in non-tty mode)")
|
|
||||||
cmd.String("name", "", "Assign a name to the container")
|
|
||||||
flPublishAll := cmd.Bool("P", false, "Publish all exposed ports to the host interfaces")
|
|
||||||
|
|
||||||
if capabilities != nil && *flMemoryString != "" && !capabilities.MemoryLimit {
|
|
||||||
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
|
|
||||||
*flMemoryString = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
flCpuShares := cmd.Int64("c", 0, "CPU shares (relative weight)")
|
|
||||||
|
|
||||||
var flPublish utils.ListOpts
|
|
||||||
cmd.Var(&flPublish, "p", "Publish a container's port to the host (use 'docker port' to see the actual mapping)")
|
|
||||||
|
|
||||||
var flExpose utils.ListOpts
|
|
||||||
cmd.Var(&flExpose, "expose", "Expose a port from the container without publishing it to your host")
|
|
||||||
|
|
||||||
var flEnv utils.ListOpts
|
|
||||||
cmd.Var(&flEnv, "e", "Set environment variables")
|
|
||||||
|
|
||||||
var flDns utils.ListOpts
|
|
||||||
cmd.Var(&flDns, "dns", "Set custom dns servers")
|
|
||||||
|
|
||||||
flVolumes := NewPathOpts()
|
|
||||||
cmd.Var(flVolumes, "v", "Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)")
|
cmd.Var(flVolumes, "v", "Bind mount a volume (e.g. from the host: -v /host:/container, from docker: -v /container)")
|
||||||
|
|
||||||
var flVolumesFrom utils.ListOpts
|
cmd.Var(&flPublish, "p", "Publish a container's port to the host (use 'docker port' to see the actual mapping)")
|
||||||
|
cmd.Var(&flExpose, "expose", "Expose a port from the container without publishing it to your host")
|
||||||
|
cmd.Var(&flEnv, "e", "Set environment variables")
|
||||||
|
cmd.Var(&flDns, "dns", "Set custom dns servers")
|
||||||
cmd.Var(&flVolumesFrom, "volumes-from", "Mount volumes from the specified container(s)")
|
cmd.Var(&flVolumesFrom, "volumes-from", "Mount volumes from the specified container(s)")
|
||||||
|
|
||||||
flEntrypoint := cmd.String("entrypoint", "", "Overwrite the default entrypoint of the image")
|
|
||||||
|
|
||||||
var flLxcOpts utils.ListOpts
|
|
||||||
cmd.Var(&flLxcOpts, "lxc-conf", "Add custom lxc options -lxc-conf=\"lxc.cgroup.cpuset.cpus = 0,1\"")
|
cmd.Var(&flLxcOpts, "lxc-conf", "Add custom lxc options -lxc-conf=\"lxc.cgroup.cpuset.cpus = 0,1\"")
|
||||||
|
|
||||||
var flLinks utils.ListOpts
|
|
||||||
cmd.Var(&flLinks, "link", "Add link to another container (name:alias)")
|
cmd.Var(&flLinks, "link", "Add link to another container (name:alias)")
|
||||||
|
|
||||||
if err := cmd.Parse(args); err != nil {
|
if err := cmd.Parse(args); err != nil {
|
||||||
return nil, nil, cmd, err
|
return nil, nil, cmd, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the kernel supports memory limit cgroup.
|
||||||
|
if capabilities != nil && *flMemoryString != "" && !capabilities.MemoryLimit {
|
||||||
|
*flMemoryString = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate input params
|
||||||
if *flDetach && len(flAttach) > 0 {
|
if *flDetach && len(flAttach) > 0 {
|
||||||
return nil, nil, cmd, ErrConflictAttachDetach
|
return nil, nil, cmd, ErrConflictAttachDetach
|
||||||
}
|
}
|
||||||
|
@ -1805,8 +1804,7 @@ func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Co
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
envs := []string{}
|
var envs []string
|
||||||
|
|
||||||
for _, env := range flEnv {
|
for _, env := range flEnv {
|
||||||
arr := strings.Split(env, "=")
|
arr := strings.Split(env, "=")
|
||||||
if len(arr) > 1 {
|
if len(arr) > 1 {
|
||||||
|
@ -1818,19 +1816,15 @@ func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Co
|
||||||
}
|
}
|
||||||
|
|
||||||
var flMemory int64
|
var flMemory int64
|
||||||
|
|
||||||
if *flMemoryString != "" {
|
if *flMemoryString != "" {
|
||||||
parsedMemory, err := utils.RAMInBytes(*flMemoryString)
|
parsedMemory, err := utils.RAMInBytes(*flMemoryString)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, cmd, err
|
return nil, nil, cmd, err
|
||||||
}
|
}
|
||||||
|
|
||||||
flMemory = parsedMemory
|
flMemory = parsedMemory
|
||||||
}
|
}
|
||||||
|
|
||||||
var binds []string
|
var binds []string
|
||||||
|
|
||||||
// add any bind targets to the list of container volumes
|
// add any bind targets to the list of container volumes
|
||||||
for bind := range flVolumes {
|
for bind := range flVolumes {
|
||||||
arr := strings.Split(bind, ":")
|
arr := strings.Split(bind, ":")
|
||||||
|
@ -1845,10 +1839,12 @@ func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Co
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedArgs := cmd.Args()
|
var (
|
||||||
runCmd := []string{}
|
parsedArgs = cmd.Args()
|
||||||
entrypoint := []string{}
|
runCmd []string
|
||||||
image := ""
|
entrypoint []string
|
||||||
|
image string
|
||||||
|
)
|
||||||
if len(parsedArgs) >= 1 {
|
if len(parsedArgs) >= 1 {
|
||||||
image = cmd.Arg(0)
|
image = cmd.Arg(0)
|
||||||
}
|
}
|
||||||
|
@ -1859,16 +1855,16 @@ func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Co
|
||||||
entrypoint = []string{*flEntrypoint}
|
entrypoint = []string{*flEntrypoint}
|
||||||
}
|
}
|
||||||
|
|
||||||
var lxcConf []KeyValuePair
|
|
||||||
lxcConf, err := parseLxcConfOpts(flLxcOpts)
|
lxcConf, err := parseLxcConfOpts(flLxcOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, cmd, err
|
return nil, nil, cmd, err
|
||||||
}
|
}
|
||||||
|
|
||||||
hostname := *flHostname
|
var (
|
||||||
domainname := ""
|
domainname string
|
||||||
|
hostname = *flHostname
|
||||||
parts := strings.SplitN(hostname, ".", 2)
|
parts = strings.SplitN(hostname, ".", 2)
|
||||||
|
)
|
||||||
if len(parts) > 1 {
|
if len(parts) > 1 {
|
||||||
hostname = parts[0]
|
hostname = parts[0]
|
||||||
domainname = parts[1]
|
domainname = parts[1]
|
||||||
|
|
16
utils.go
16
utils.go
|
@ -209,11 +209,14 @@ func parseLxcOpt(opt string) (string, string, error) {
|
||||||
// We will receive port specs in the format of ip:public:private/proto and these need to be
|
// We will receive port specs in the format of ip:public:private/proto and these need to be
|
||||||
// parsed in the internal types
|
// parsed in the internal types
|
||||||
func parsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
|
func parsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding, error) {
|
||||||
exposedPorts := make(map[Port]struct{}, len(ports))
|
var (
|
||||||
bindings := make(map[Port][]PortBinding)
|
exposedPorts = make(map[Port]struct{}, len(ports))
|
||||||
|
bindings = make(map[Port][]PortBinding)
|
||||||
|
)
|
||||||
|
|
||||||
for _, rawPort := range ports {
|
for _, rawPort := range ports {
|
||||||
proto := "tcp"
|
proto := "tcp"
|
||||||
|
|
||||||
if i := strings.LastIndex(rawPort, "/"); i != -1 {
|
if i := strings.LastIndex(rawPort, "/"); i != -1 {
|
||||||
proto = rawPort[i+1:]
|
proto = rawPort[i+1:]
|
||||||
rawPort = rawPort[:i]
|
rawPort = rawPort[:i]
|
||||||
|
@ -228,9 +231,12 @@ func parsePortSpecs(ports []string) (map[Port]struct{}, map[Port][]PortBinding,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
containerPort := parts["containerPort"]
|
|
||||||
rawIp := parts["ip"]
|
var (
|
||||||
hostPort := parts["hostPort"]
|
containerPort = parts["containerPort"]
|
||||||
|
rawIp = parts["ip"]
|
||||||
|
hostPort = parts["hostPort"]
|
||||||
|
)
|
||||||
|
|
||||||
if containerPort == "" {
|
if containerPort == "" {
|
||||||
return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
|
return nil, nil, fmt.Errorf("No port specified: %s<empty>", rawPort)
|
||||||
|
|
|
@ -1237,12 +1237,14 @@ func IsClosedError(err error) bool {
|
||||||
|
|
||||||
func PartParser(template, data string) (map[string]string, error) {
|
func PartParser(template, data string) (map[string]string, error) {
|
||||||
// ip:public:private
|
// ip:public:private
|
||||||
templateParts := strings.Split(template, ":")
|
var (
|
||||||
parts := strings.Split(data, ":")
|
templateParts = strings.Split(template, ":")
|
||||||
|
parts = strings.Split(data, ":")
|
||||||
|
out = make(map[string]string, len(templateParts))
|
||||||
|
)
|
||||||
if len(parts) != len(templateParts) {
|
if len(parts) != len(templateParts) {
|
||||||
return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template)
|
return nil, fmt.Errorf("Invalid format to parse. %s should match template %s", data, template)
|
||||||
}
|
}
|
||||||
out := make(map[string]string, len(templateParts))
|
|
||||||
|
|
||||||
for i, t := range templateParts {
|
for i, t := range templateParts {
|
||||||
value := ""
|
value := ""
|
||||||
|
|
Loading…
Reference in New Issue