mirror of https://github.com/docker/compose.git
Return exit code 1 if engine error on version query
Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
parent
fef479ad1f
commit
29cc59cf42
|
@ -43,7 +43,7 @@ func WithCliOptions(ctx gocontext.Context, options cliflags.CommonOptions) conte
|
||||||
return context.WithValue(ctx, cliOptionsKey{}, options)
|
return context.WithValue(ctx, cliOptionsKey{}, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CliOptions returns the current context name
|
// CliOptions returns common cli options
|
||||||
func CliOptions(ctx context.Context) cliflags.CommonOptions {
|
func CliOptions(ctx context.Context) cliflags.CommonOptions {
|
||||||
cc, _ := ctx.Value(cliOptionsKey{}).(cliflags.CommonOptions)
|
cc, _ := ctx.Value(cliOptionsKey{}).(cliflags.CommonOptions)
|
||||||
return cc
|
return cc
|
||||||
|
|
|
@ -36,8 +36,12 @@ func VersionCommand() *cobra.Command {
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Show the Docker version information",
|
Short: "Show the Docker version information",
|
||||||
Args: cobra.MaximumNArgs(0),
|
Args: cobra.MaximumNArgs(0),
|
||||||
Run: func(cmd *cobra.Command, _ []string) {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
runVersion(cmd)
|
err := runVersion(cmd)
|
||||||
|
if err != nil {
|
||||||
|
return ExitCodeError{ExitCode: 1}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
// define flags for backward compatibility with com.docker.cli
|
// define flags for backward compatibility with com.docker.cli
|
||||||
|
@ -48,34 +52,38 @@ func VersionCommand() *cobra.Command {
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func runVersion(cmd *cobra.Command) {
|
func runVersion(cmd *cobra.Command) error {
|
||||||
var versionString string
|
var versionString string
|
||||||
|
var err error
|
||||||
format := strings.ToLower(strings.ReplaceAll(cmd.Flag(formatOpt).Value.String(), " ", ""))
|
format := strings.ToLower(strings.ReplaceAll(cmd.Flag(formatOpt).Value.String(), " ", ""))
|
||||||
displayedVersion := strings.TrimPrefix(internal.Version, "v")
|
displayedVersion := strings.TrimPrefix(internal.Version, "v")
|
||||||
// Replace is preferred in this case to keep the order.
|
// Replace is preferred in this case to keep the order.
|
||||||
switch format {
|
switch format {
|
||||||
case formatter.PRETTY, "":
|
case formatter.PRETTY, "":
|
||||||
versionString = strings.Replace(getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...),
|
versionString, err = getOutFromMoby(cmd, fixedPrettyArgs(os.Args[1:])...)
|
||||||
|
versionString = strings.Replace(versionString,
|
||||||
"\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
|
"\n Version:", "\n Cloud integration: "+displayedVersion+"\n Version:", 1)
|
||||||
case formatter.JSON, formatter.TemplateLegacyJSON: // Try to catch full JSON formats
|
case formatter.JSON, formatter.TemplateLegacyJSON: // Try to catch full JSON formats
|
||||||
versionString = strings.Replace(getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...),
|
versionString, err = getOutFromMoby(cmd, fixedJSONArgs(os.Args[1:])...)
|
||||||
|
versionString = strings.Replace(versionString,
|
||||||
`"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
|
`"Version":`, fmt.Sprintf(`"CloudIntegration":%q,"Version":`, displayedVersion), 1)
|
||||||
default:
|
default:
|
||||||
versionString = getOutFromMoby(cmd)
|
versionString, err = getOutFromMoby(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(versionString)
|
fmt.Print(versionString)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOutFromMoby(cmd *cobra.Command, args ...string) string {
|
func getOutFromMoby(cmd *cobra.Command, args ...string) (string, error) {
|
||||||
versionResult, _ := mobycli.ExecSilent(cmd.Context(), args...)
|
versionResult, err := mobycli.ExecSilent(cmd.Context(), args...)
|
||||||
// we don't want to fail on error, there is an error if the engine is not available but it displays client version info
|
// we don't want to fail on error, there is an error if the engine is not available but it displays client version info
|
||||||
// Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
|
// Still, technically the [] byte versionResult could be nil, just let the original command display what it has to display
|
||||||
if versionResult == nil {
|
if versionResult == nil {
|
||||||
mobycli.Exec(cmd.Root())
|
mobycli.Exec(cmd.Root())
|
||||||
return ""
|
return "", nil
|
||||||
}
|
}
|
||||||
return string(versionResult)
|
return string(versionResult), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func fixedPrettyArgs(oArgs []string) []string {
|
func fixedPrettyArgs(oArgs []string) []string {
|
||||||
|
|
|
@ -396,14 +396,14 @@ func TestLegacy(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("host flag", func(t *testing.T) {
|
t.Run("host flag", func(t *testing.T) {
|
||||||
stderr := []string{"dial tcp: lookup nonexistent", "no such host"}
|
stderr := "dial tcp: lookup nonexistent"
|
||||||
res := c.RunDockerOrExitError("-H", "tcp://nonexistent:123", "version")
|
if runtime.GOOS == "windows" {
|
||||||
res.Assert(t, icmd.Expected{
|
stderr = "dial tcp: lookup nonexistent: no such host"
|
||||||
ExitCode: 1,
|
|
||||||
})
|
|
||||||
for _, s := range stderr {
|
|
||||||
assert.Assert(t, strings.Contains(res.Stderr(), s), res.Stderr())
|
|
||||||
}
|
}
|
||||||
|
res := c.RunDockerOrExitError("-H", "tcp://nonexistent:123", "version")
|
||||||
|
assert.Assert(t, res.ExitCode == 1)
|
||||||
|
assert.Assert(t, strings.Contains(res.Stdout(), stderr), res.Stdout())
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("existing contexts delegate", func(t *testing.T) {
|
t.Run("existing contexts delegate", func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue