mirror of
https://github.com/containers/podman.git
synced 2025-06-22 01:48:54 +08:00
Merge pull request #2075 from baude/runlabelname
container runlabel NAME implementation
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/google/shlex"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -640,6 +641,14 @@ func GetRunlabel(label string, runlabelImage string, ctx context.Context, runtim
|
|||||||
|
|
||||||
// GenerateRunlabelCommand generates the command that will eventually be execucted by podman
|
// GenerateRunlabelCommand generates the command that will eventually be execucted by podman
|
||||||
func GenerateRunlabelCommand(runLabel, imageName, name string, opts map[string]string, extraArgs []string) ([]string, []string, error) {
|
func GenerateRunlabelCommand(runLabel, imageName, name string, opts map[string]string, extraArgs []string) ([]string, []string, error) {
|
||||||
|
// If no name is provided, we use the image's basename instead
|
||||||
|
if name == "" {
|
||||||
|
baseName, err := image.GetImageBaseName(imageName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
name = baseName
|
||||||
|
}
|
||||||
// The user provided extra arguments that need to be tacked onto the label's command
|
// The user provided extra arguments that need to be tacked onto the label's command
|
||||||
if len(extraArgs) > 0 {
|
if len(extraArgs) > 0 {
|
||||||
runLabel = fmt.Sprintf("%s %s", runLabel, strings.Join(extraArgs, " "))
|
runLabel = fmt.Sprintf("%s %s", runLabel, strings.Join(extraArgs, " "))
|
||||||
@ -665,7 +674,10 @@ func GenerateRunlabelCommand(runLabel, imageName, name string, opts map[string]s
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
newS := os.Expand(strings.Join(cmd, " "), envmapper)
|
newS := os.Expand(strings.Join(cmd, " "), envmapper)
|
||||||
cmd = strings.Split(newS, " ")
|
cmd, err = shlex.Split(newS)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
return cmd, env, nil
|
return cmd, env, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +65,8 @@ func GenerateCommand(command, imageName, name string) ([]string, error) {
|
|||||||
switch arg {
|
switch arg {
|
||||||
case "IMAGE":
|
case "IMAGE":
|
||||||
newArg = imageName
|
newArg = imageName
|
||||||
|
case "$IMAGE":
|
||||||
|
newArg = imageName
|
||||||
case "IMAGE=IMAGE":
|
case "IMAGE=IMAGE":
|
||||||
newArg = fmt.Sprintf("IMAGE=%s", imageName)
|
newArg = fmt.Sprintf("IMAGE=%s", imageName)
|
||||||
case "IMAGE=$IMAGE":
|
case "IMAGE=$IMAGE":
|
||||||
@ -75,6 +77,8 @@ func GenerateCommand(command, imageName, name string) ([]string, error) {
|
|||||||
newArg = fmt.Sprintf("NAME=%s", name)
|
newArg = fmt.Sprintf("NAME=%s", name)
|
||||||
case "NAME=$NAME":
|
case "NAME=$NAME":
|
||||||
newArg = fmt.Sprintf("NAME=%s", name)
|
newArg = fmt.Sprintf("NAME=%s", name)
|
||||||
|
case "$NAME":
|
||||||
|
newArg = name
|
||||||
default:
|
default:
|
||||||
newArg = arg
|
newArg = arg
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,18 @@ func isRegistry(name string) bool {
|
|||||||
return strings.ContainsAny(name, ".:") || name == "localhost"
|
return strings.ContainsAny(name, ".:") || name == "localhost"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetImageBaseName uses decompose and string splits to obtain the base
|
||||||
|
// name of an image. Doing this here because it beats changing the
|
||||||
|
// imageParts struct names to be exported as well.
|
||||||
|
func GetImageBaseName(input string) (string, error) {
|
||||||
|
decomposedImage, err := decompose(input)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
splitImageName := strings.Split(decomposedImage.name, "/")
|
||||||
|
return splitImageName[len(splitImageName)-1], nil
|
||||||
|
}
|
||||||
|
|
||||||
// decompose breaks an input name into an imageParts description
|
// decompose breaks an input name into an imageParts description
|
||||||
func decompose(input string) (imageParts, error) {
|
func decompose(input string) (imageParts, error) {
|
||||||
var (
|
var (
|
||||||
|
Reference in New Issue
Block a user