mirror of https://github.com/docker/compose.git
Merge pull request #1226 from docker/loglevel
Add support for --log-level
This commit is contained in:
commit
24d40ba6ac
|
@ -94,8 +94,9 @@ func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
|
||||||
func Command(contextType string) *cobra.Command {
|
func Command(contextType string) *cobra.Command {
|
||||||
opts := projectOptions{}
|
opts := projectOptions{}
|
||||||
command := &cobra.Command{
|
command := &cobra.Command{
|
||||||
Short: "Docker Compose",
|
Short: "Docker Compose",
|
||||||
Use: "compose",
|
Use: "compose",
|
||||||
|
TraverseChildren: true,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if contextType == store.DefaultContextType || contextType == store.LocalContextType {
|
if contextType == store.DefaultContextType || contextType == store.LocalContextType {
|
||||||
fmt.Println("The new 'docker compose' command is currently experimental. To provide feedback or request new features please open issues at https://github.com/docker/compose-cli")
|
fmt.Println("The new 'docker compose' command is currently experimental. To provide feedback or request new features please open issues at https://github.com/docker/compose-cli")
|
||||||
|
@ -126,6 +127,6 @@ func Command(contextType string) *cobra.Command {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
command.Flags().SetInterspersed(false)
|
command.Flags().SetInterspersed(false)
|
||||||
opts.addProjectFlags(command.PersistentFlags())
|
opts.addProjectFlags(command.Flags())
|
||||||
return command
|
return command
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,6 @@ import (
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/client"
|
"github.com/docker/compose-cli/api/client"
|
||||||
"github.com/docker/compose-cli/api/errdefs"
|
"github.com/docker/compose-cli/api/errdefs"
|
||||||
"github.com/docker/compose-cli/cli/cmd/mobyflags"
|
|
||||||
"github.com/docker/compose-cli/cli/mobycli"
|
"github.com/docker/compose-cli/cli/mobycli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,7 +42,6 @@ func Command() *cobra.Command {
|
||||||
flags.StringP("username", "u", "", "username")
|
flags.StringP("username", "u", "", "username")
|
||||||
flags.StringP("password", "p", "", "password")
|
flags.StringP("password", "p", "", "password")
|
||||||
flags.BoolP("password-stdin", "", false, "Take the password from stdin")
|
flags.BoolP("password-stdin", "", false, "Take the password from stdin")
|
||||||
mobyflags.AddMobyFlagsForRetrocompatibility(flags)
|
|
||||||
|
|
||||||
cmd.AddCommand(AzureLoginCommand())
|
cmd.AddCommand(AzureLoginCommand())
|
||||||
return cmd
|
return cmd
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2020 Docker Compose CLI authors
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package mobyflags
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
|
|
||||||
flag "github.com/spf13/pflag"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AddMobyFlagsForRetrocompatibility adds retrocompatibility flags to our commands
|
|
||||||
func AddMobyFlagsForRetrocompatibility(flags *flag.FlagSet) {
|
|
||||||
const logLevelFlag = "log-level"
|
|
||||||
flags.StringP(logLevelFlag, "l", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
|
|
||||||
markHidden(flags, logLevelFlag)
|
|
||||||
}
|
|
||||||
|
|
||||||
func markHidden(flags *flag.FlagSet, flagName string) {
|
|
||||||
err := flags.MarkHidden(flagName)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,7 +23,6 @@ import (
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/cli/cmd/mobyflags"
|
|
||||||
"github.com/docker/compose-cli/cli/formatter"
|
"github.com/docker/compose-cli/cli/formatter"
|
||||||
"github.com/docker/compose-cli/cli/mobycli"
|
"github.com/docker/compose-cli/cli/mobycli"
|
||||||
"github.com/docker/compose-cli/internal"
|
"github.com/docker/compose-cli/internal"
|
||||||
|
@ -45,7 +44,6 @@ func VersionCommand() *cobra.Command {
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.StringP(formatOpt, "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
|
flags.StringP(formatOpt, "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
|
||||||
flags.String("kubeconfig", "", "Kubernetes config file")
|
flags.String("kubeconfig", "", "Kubernetes config file")
|
||||||
mobyflags.AddMobyFlagsForRetrocompatibility(flags)
|
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
78
cli/main.go
78
cli/main.go
|
@ -102,9 +102,10 @@ func isContextAgnosticCommand(cmd *cobra.Command) bool {
|
||||||
func main() {
|
func main() {
|
||||||
var opts cliopts.GlobalOpts
|
var opts cliopts.GlobalOpts
|
||||||
root := &cobra.Command{
|
root := &cobra.Command{
|
||||||
Use: "docker",
|
Use: "docker",
|
||||||
SilenceErrors: true,
|
SilenceErrors: true,
|
||||||
SilenceUsage: true,
|
SilenceUsage: true,
|
||||||
|
TraverseChildren: true,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if !isContextAgnosticCommand(cmd) {
|
if !isContextAgnosticCommand(cmd) {
|
||||||
mobycli.ExecIfDefaultCtxType(cmd.Context(), cmd.Root())
|
mobycli.ExecIfDefaultCtxType(cmd.Context(), cmd.Root())
|
||||||
|
@ -112,7 +113,10 @@ func main() {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cmd.Help()
|
if len(args) == 0 {
|
||||||
|
return cmd.Help()
|
||||||
|
}
|
||||||
|
return fmt.Errorf("unknown command %q", args[0])
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,19 +150,27 @@ func main() {
|
||||||
helpFunc(cmd, args)
|
helpFunc(cmd, args)
|
||||||
})
|
})
|
||||||
|
|
||||||
root.PersistentFlags().BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug output in the logs")
|
flags := root.Flags()
|
||||||
root.PersistentFlags().StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to")
|
flags.StringVarP(&opts.LogLevel, "log-level", "l", "info", "Set the logging level (\"debug\"|\"info\"|\"warn\"|\"error\"|\"fatal\")")
|
||||||
opts.AddContextFlags(root.PersistentFlags())
|
flags.BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug output in the logs")
|
||||||
opts.AddConfigFlags(root.PersistentFlags())
|
flags.StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to")
|
||||||
root.Flags().BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
|
opts.AddContextFlags(flags)
|
||||||
|
opts.AddConfigFlags(flags)
|
||||||
|
flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
|
||||||
|
|
||||||
walk(root, func(c *cobra.Command) {
|
walk(root, func(c *cobra.Command) {
|
||||||
c.Flags().BoolP("help", "h", false, "Help for "+c.Name())
|
c.Flags().BoolP("help", "h", false, "Help for "+c.Name())
|
||||||
})
|
})
|
||||||
|
|
||||||
// populate the opts with the global flags
|
// populate the opts with the global flags
|
||||||
_ = root.PersistentFlags().Parse(os.Args[1:])
|
flags.Parse(os.Args[1:]) //nolint: errcheck
|
||||||
_ = root.Flags().Parse(os.Args[1:])
|
|
||||||
|
level, err := logrus.ParseLevel(opts.LogLevel)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", opts.LogLevel)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
logrus.SetLevel(level)
|
||||||
if opts.Debug {
|
if opts.Debug {
|
||||||
logrus.SetLevel(logrus.DebugLevel)
|
logrus.SetLevel(logrus.DebugLevel)
|
||||||
}
|
}
|
||||||
|
@ -200,30 +212,34 @@ func main() {
|
||||||
ctx = store.WithContextStore(ctx, s)
|
ctx = store.WithContextStore(ctx, s)
|
||||||
|
|
||||||
if err = root.ExecuteContext(ctx); err != nil {
|
if err = root.ExecuteContext(ctx); err != nil {
|
||||||
// if user canceled request, simply exit without any error message
|
handleError(ctx, err, ctype, currentContext, cc, root)
|
||||||
if errdefs.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {
|
|
||||||
metrics.Track(ctype, os.Args[1:], metrics.CanceledStatus)
|
|
||||||
os.Exit(130)
|
|
||||||
}
|
|
||||||
if ctype == store.AwsContextType {
|
|
||||||
exit(currentContext, errors.Errorf(`%q context type has been renamed. Recreate the context by running:
|
|
||||||
$ docker context create %s <name>`, cc.Type(), store.EcsContextType), ctype)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Context should always be handled by new CLI
|
|
||||||
requiredCmd, _, _ := root.Find(os.Args[1:])
|
|
||||||
if requiredCmd != nil && isContextAgnosticCommand(requiredCmd) {
|
|
||||||
exit(currentContext, err, ctype)
|
|
||||||
}
|
|
||||||
mobycli.ExecIfDefaultCtxType(ctx, root)
|
|
||||||
|
|
||||||
checkIfUnknownCommandExistInDefaultContext(err, currentContext, ctype)
|
|
||||||
|
|
||||||
exit(currentContext, err, ctype)
|
|
||||||
}
|
}
|
||||||
metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus)
|
metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleError(ctx context.Context, err error, ctype string, currentContext string, cc *store.DockerContext, root *cobra.Command) {
|
||||||
|
// if user canceled request, simply exit without any error message
|
||||||
|
if errdefs.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {
|
||||||
|
metrics.Track(ctype, os.Args[1:], metrics.CanceledStatus)
|
||||||
|
os.Exit(130)
|
||||||
|
}
|
||||||
|
if ctype == store.AwsContextType {
|
||||||
|
exit(currentContext, errors.Errorf(`%q context type has been renamed. Recreate the context by running:
|
||||||
|
$ docker context create %s <name>`, cc.Type(), store.EcsContextType), ctype)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Context should always be handled by new CLI
|
||||||
|
requiredCmd, _, _ := root.Find(os.Args[1:])
|
||||||
|
if requiredCmd != nil && isContextAgnosticCommand(requiredCmd) {
|
||||||
|
exit(currentContext, err, ctype)
|
||||||
|
}
|
||||||
|
mobycli.ExecIfDefaultCtxType(ctx, root)
|
||||||
|
|
||||||
|
checkIfUnknownCommandExistInDefaultContext(err, currentContext, ctype)
|
||||||
|
|
||||||
|
exit(currentContext, err, ctype)
|
||||||
|
}
|
||||||
|
|
||||||
func exit(ctx string, err error, ctype string) {
|
func exit(ctx string, err error, ctype string) {
|
||||||
metrics.Track(ctype, os.Args[1:], metrics.FailureStatus)
|
metrics.Track(ctype, os.Args[1:], metrics.FailureStatus)
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,8 @@ import (
|
||||||
type GlobalOpts struct {
|
type GlobalOpts struct {
|
||||||
apicontext.ContextFlags
|
apicontext.ContextFlags
|
||||||
cliconfig.ConfigFlags
|
cliconfig.ConfigFlags
|
||||||
Debug bool
|
Debug bool
|
||||||
Version bool
|
LogLevel string
|
||||||
Host string
|
Version bool
|
||||||
|
Host string
|
||||||
}
|
}
|
||||||
|
|
|
@ -450,11 +450,6 @@ func TestLegacyLogin(t *testing.T) {
|
||||||
Err: "WARNING! Using --password via the CLI is insecure",
|
Err: "WARNING! Using --password via the CLI is insecure",
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("login help global flags", func(t *testing.T) {
|
|
||||||
res := c.RunDockerCmd("login", "--help")
|
|
||||||
assert.Assert(t, !strings.Contains(res.Combined(), "--log-level"))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnsupportedCommand(t *testing.T) {
|
func TestUnsupportedCommand(t *testing.T) {
|
||||||
|
|
|
@ -33,28 +33,28 @@ func TestLocalComposeLogs(t *testing.T) {
|
||||||
const projectName = "compose-e2e-logs"
|
const projectName = "compose-e2e-logs"
|
||||||
|
|
||||||
t.Run("up", func(t *testing.T) {
|
t.Run("up", func(t *testing.T) {
|
||||||
c.RunDockerCmd("compose", "up", "-d", "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "-d")
|
c.RunDockerCmd("compose", "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "up", "-d")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("logs", func(t *testing.T) {
|
t.Run("logs", func(t *testing.T) {
|
||||||
res := c.RunDockerCmd("compose", "logs", "--project-name", projectName)
|
res := c.RunDockerCmd("compose", "--project-name", projectName, "logs")
|
||||||
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
|
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
|
||||||
res.Assert(t, icmd.Expected{Out: `hello`})
|
res.Assert(t, icmd.Expected{Out: `hello`})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("logs ping", func(t *testing.T) {
|
t.Run("logs ping", func(t *testing.T) {
|
||||||
res := c.RunDockerCmd("compose", "logs", "--project-name", projectName, "ping")
|
res := c.RunDockerCmd("compose", "--project-name", projectName, "logs", "ping")
|
||||||
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
|
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
|
||||||
assert.Assert(t, !strings.Contains(res.Stdout(), "hello"))
|
assert.Assert(t, !strings.Contains(res.Stdout(), "hello"))
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("logs hello", func(t *testing.T) {
|
t.Run("logs hello", func(t *testing.T) {
|
||||||
res := c.RunDockerCmd("compose", "logs", "--project-name", projectName, "hello", "ping")
|
res := c.RunDockerCmd("compose", "--project-name", projectName, "logs", "hello", "ping")
|
||||||
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
|
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
|
||||||
res.Assert(t, icmd.Expected{Out: `hello`})
|
res.Assert(t, icmd.Expected{Out: `hello`})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("down", func(t *testing.T) {
|
t.Run("down", func(t *testing.T) {
|
||||||
_ = c.RunDockerCmd("compose", "down", "--project-name", projectName)
|
_ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue