浏览代码

Add support for --log-level

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 年之前
父节点
当前提交
4cbb33e20d
共有 6 个文件被更改,包括 13 次插入45 次删除
  1. 0 2
      cli/cmd/login/login.go
  2. 0 37
      cli/cmd/mobyflags/mobyflags.go
  3. 0 2
      cli/cmd/version.go
  4. 8 0
      cli/main.go
  5. 4 3
      cli/options/options.go
  6. 1 1
      local/e2e/cli-only/e2e_test.go

+ 0 - 2
cli/cmd/login/login.go

@@ -25,7 +25,6 @@ import (
 
 	"github.com/docker/compose-cli/api/client"
 	"github.com/docker/compose-cli/api/errdefs"
-	"github.com/docker/compose-cli/cli/cmd/mobyflags"
 	"github.com/docker/compose-cli/cli/mobycli"
 )
 
@@ -43,7 +42,6 @@ func Command() *cobra.Command {
 	flags.StringP("username", "u", "", "username")
 	flags.StringP("password", "p", "", "password")
 	flags.BoolP("password-stdin", "", false, "Take the password from stdin")
-	mobyflags.AddMobyFlagsForRetrocompatibility(flags)
 
 	cmd.AddCommand(AzureLoginCommand())
 	return cmd

+ 0 - 37
cli/cmd/mobyflags/mobyflags.go

@@ -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)
-	}
-}

+ 0 - 2
cli/cmd/version.go

@@ -23,7 +23,6 @@ import (
 
 	"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/mobycli"
 	"github.com/docker/compose-cli/internal"
@@ -45,7 +44,6 @@ func VersionCommand() *cobra.Command {
 	flags := cmd.Flags()
 	flags.StringP(formatOpt, "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
 	flags.String("kubeconfig", "", "Kubernetes config file")
-	mobyflags.AddMobyFlagsForRetrocompatibility(flags)
 
 	return cmd
 }

+ 8 - 0
cli/main.go

@@ -146,6 +146,7 @@ func main() {
 		helpFunc(cmd, args)
 	})
 
+	root.PersistentFlags().StringVarP(&opts.LogLevel, "log-level", "l", "info", "Set the logging level (\"debug\"|\"info\"|\"warn\"|\"error\"|\"fatal\")")
 	root.PersistentFlags().BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug output in the logs")
 	root.PersistentFlags().StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to")
 	opts.AddContextFlags(root.PersistentFlags())
@@ -159,6 +160,13 @@ func main() {
 	// populate the opts with the global flags
 	_ = root.PersistentFlags().Parse(os.Args[1:])
 	_ = 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 {
 		logrus.SetLevel(logrus.DebugLevel)
 	}

+ 4 - 3
cli/options/options.go

@@ -25,7 +25,8 @@ import (
 type GlobalOpts struct {
 	apicontext.ContextFlags
 	cliconfig.ConfigFlags
-	Debug   bool
-	Version bool
-	Host    string
+	Debug    bool
+	LogLevel string
+	Version  bool
+	Host     string
 }

+ 1 - 1
local/e2e/cli-only/e2e_test.go

@@ -453,7 +453,7 @@ func TestLegacyLogin(t *testing.T) {
 
 	t.Run("login help global flags", func(t *testing.T) {
 		res := c.RunDockerCmd("login", "--help")
-		assert.Assert(t, !strings.Contains(res.Combined(), "--log-level"))
+		assert.Assert(t, strings.Contains(res.Combined(), "--log-level"))
 	})
 }