mirror of
https://github.com/docker/compose.git
synced 2025-07-23 13:45:00 +02:00
Remap restart policies for command run
This adds 'no' and 'always' as options. Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
c1ee51f31e
commit
7aee922b35
@ -24,16 +24,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// RestartPolicyAny Always restarts
|
|
||||||
RestartPolicyAny = "any"
|
|
||||||
// RestartPolicyNone Never restarts
|
// RestartPolicyNone Never restarts
|
||||||
RestartPolicyNone = "none"
|
RestartPolicyNone = "none"
|
||||||
|
// RestartPolicyAny Always restarts
|
||||||
|
RestartPolicyAny = "any"
|
||||||
// RestartPolicyOnFailure Restarts only on failure
|
// RestartPolicyOnFailure Restarts only on failure
|
||||||
RestartPolicyOnFailure = "on-failure"
|
RestartPolicyOnFailure = "on-failure"
|
||||||
|
|
||||||
|
// RestartPolicyRunNo Always restarts
|
||||||
|
RestartPolicyRunNo = "no"
|
||||||
|
// RestartPolicyRunAlways Always restarts
|
||||||
|
RestartPolicyRunAlways = "always"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RestartPolicyList all available restart policy values
|
// RestartPolicyList all available restart policy values
|
||||||
var RestartPolicyList = []string{RestartPolicyNone, RestartPolicyAny, RestartPolicyOnFailure}
|
var RestartPolicyList = []string{RestartPolicyRunNo, RestartPolicyRunAlways, RestartPolicyOnFailure}
|
||||||
|
|
||||||
// Container represents a created container
|
// Container represents a created container
|
||||||
type Container struct {
|
type Container struct {
|
||||||
|
@ -57,7 +57,7 @@ func Command(contextType string) *cobra.Command {
|
|||||||
cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
|
cmd.Flags().VarP(&opts.Memory, "memory", "m", "Memory limit")
|
||||||
cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
|
cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables")
|
||||||
cmd.Flags().StringArrayVar(&opts.EnvironmentFiles, "env-file", []string{}, "Path to environment files to be translated as environment variables")
|
cmd.Flags().StringArrayVar(&opts.EnvironmentFiles, "env-file", []string{}, "Path to environment files to be translated as environment variables")
|
||||||
cmd.Flags().StringVarP(&opts.RestartPolicyCondition, "restart", "", containers.RestartPolicyNone, "Restart policy to apply when a container exits")
|
cmd.Flags().StringVarP(&opts.RestartPolicyCondition, "restart", "", containers.RestartPolicyRunNo, "Restart policy to apply when a container exits (no|always|on-failure)")
|
||||||
cmd.Flags().BoolVar(&opts.Rm, "rm", false, "Automatically remove the container when it exits")
|
cmd.Flags().BoolVar(&opts.Rm, "rm", false, "Automatically remove the container when it exits")
|
||||||
|
|
||||||
if contextType == store.AciContextType {
|
if contextType == store.AciContextType {
|
||||||
|
2
cli/cmd/run/testdata/run-help.golden
vendored
2
cli/cmd/run/testdata/run-help.golden
vendored
@ -13,5 +13,5 @@ Flags:
|
|||||||
-m, --memory bytes Memory limit
|
-m, --memory bytes Memory limit
|
||||||
--name string Assign a name to the container
|
--name string Assign a name to the container
|
||||||
-p, --publish stringArray Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT
|
-p, --publish stringArray Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT
|
||||||
--restart string Restart policy to apply when a container exits (default "none")
|
--restart string Restart policy to apply when a container exits (no|always|on-failure) (default "no")
|
||||||
-v, --volume stringArray Volume. Ex: storageaccount/my_share[:/absolute/path/to/target][:ro]
|
-v, --volume stringArray Volume. Ex: storageaccount/my_share[:/absolute/path/to/target][:ro]
|
||||||
|
@ -27,7 +27,6 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/compose-cli/api/containers"
|
"github.com/docker/compose-cli/api/containers"
|
||||||
"github.com/docker/compose-cli/formatter"
|
"github.com/docker/compose-cli/formatter"
|
||||||
"github.com/docker/compose-cli/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Opts contain run command options
|
// Opts contain run command options
|
||||||
@ -93,15 +92,22 @@ func (r *Opts) ToContainerConfig(image string) (containers.ContainerConfig, erro
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toRestartPolicy(value string) (string, error) {
|
var restartPolicyMap = map[string]string{
|
||||||
if value == "" {
|
"": containers.RestartPolicyNone,
|
||||||
return containers.RestartPolicyNone, nil
|
containers.RestartPolicyNone: containers.RestartPolicyNone,
|
||||||
}
|
containers.RestartPolicyAny: containers.RestartPolicyAny,
|
||||||
if utils.StringContains(containers.RestartPolicyList, value) {
|
containers.RestartPolicyOnFailure: containers.RestartPolicyOnFailure,
|
||||||
return value, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", fmt.Errorf("invalid restart value, must be one of %s", strings.Join(containers.RestartPolicyList, ", "))
|
containers.RestartPolicyRunNo: containers.RestartPolicyNone,
|
||||||
|
containers.RestartPolicyRunAlways: containers.RestartPolicyAny,
|
||||||
|
}
|
||||||
|
|
||||||
|
func toRestartPolicy(value string) (string, error) {
|
||||||
|
value, ok := restartPolicyMap[value]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("invalid restart value, must be one of %s", strings.Join(containers.RestartPolicyList, ", "))
|
||||||
|
}
|
||||||
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Opts) toPorts() ([]containers.Port, error) {
|
func (r *Opts) toPorts() ([]containers.Port, error) {
|
||||||
|
@ -200,10 +200,21 @@ func TestValidateRestartPolicy(t *testing.T) {
|
|||||||
expected: "none",
|
expected: "none",
|
||||||
expectedError: nil,
|
expectedError: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
in: "no",
|
||||||
|
expected: "none",
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
in: "always",
|
||||||
|
expected: "any",
|
||||||
|
expectedError: nil,
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
in: "toto",
|
in: "toto",
|
||||||
expected: "",
|
expected: "",
|
||||||
expectedError: errors.New("invalid restart value, must be one of none, any, on-failure"),
|
expectedError: errors.New("invalid restart value, must be one of no, always, on-failure"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user