Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 24d40ba

Browse files
authored
Merge pull request #1226 from docker/loglevel
Add support for --log-level
2 parents 42adbae + 9f80214 commit 24d40ba

File tree

8 files changed

+58
-86
lines changed

8 files changed

+58
-86
lines changed

cli/cmd/compose/compose.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
9494
func Command(contextType string) *cobra.Command {
9595
opts := projectOptions{}
9696
command := &cobra.Command{
97-
Short: "Docker Compose",
98-
Use: "compose",
97+
Short: "Docker Compose",
98+
Use: "compose",
99+
TraverseChildren: true,
99100
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
100101
if contextType == store.DefaultContextType || contextType == store.LocalContextType {
101102
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 {
126127
)
127128
}
128129
command.Flags().SetInterspersed(false)
129-
opts.addProjectFlags(command.PersistentFlags())
130+
opts.addProjectFlags(command.Flags())
130131
return command
131132
}

cli/cmd/login/login.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
"github.com/docker/compose-cli/api/client"
2727
"github.com/docker/compose-cli/api/errdefs"
28-
"github.com/docker/compose-cli/cli/cmd/mobyflags"
2928
"github.com/docker/compose-cli/cli/mobycli"
3029
)
3130

@@ -43,7 +42,6 @@ func Command() *cobra.Command {
4342
flags.StringP("username", "u", "", "username")
4443
flags.StringP("password", "p", "", "password")
4544
flags.BoolP("password-stdin", "", false, "Take the password from stdin")
46-
mobyflags.AddMobyFlagsForRetrocompatibility(flags)
4745

4846
cmd.AddCommand(AzureLoginCommand())
4947
return cmd

cli/cmd/mobyflags/mobyflags.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

cli/cmd/version.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/spf13/cobra"
2525

26-
"github.com/docker/compose-cli/cli/cmd/mobyflags"
2726
"github.com/docker/compose-cli/cli/formatter"
2827
"github.com/docker/compose-cli/cli/mobycli"
2928
"github.com/docker/compose-cli/internal"
@@ -45,7 +44,6 @@ func VersionCommand() *cobra.Command {
4544
flags := cmd.Flags()
4645
flags.StringP(formatOpt, "f", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
4746
flags.String("kubeconfig", "", "Kubernetes config file")
48-
mobyflags.AddMobyFlagsForRetrocompatibility(flags)
4947

5048
return cmd
5149
}

cli/main.go

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,21 @@ func isContextAgnosticCommand(cmd *cobra.Command) bool {
102102
func main() {
103103
var opts cliopts.GlobalOpts
104104
root := &cobra.Command{
105-
Use: "docker",
106-
SilenceErrors: true,
107-
SilenceUsage: true,
105+
Use: "docker",
106+
SilenceErrors: true,
107+
SilenceUsage: true,
108+
TraverseChildren: true,
108109
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
109110
if !isContextAgnosticCommand(cmd) {
110111
mobycli.ExecIfDefaultCtxType(cmd.Context(), cmd.Root())
111112
}
112113
return nil
113114
},
114115
RunE: func(cmd *cobra.Command, args []string) error {
115-
return cmd.Help()
116+
if len(args) == 0 {
117+
return cmd.Help()
118+
}
119+
return fmt.Errorf("unknown command %q", args[0])
116120
},
117121
}
118122

@@ -146,19 +150,27 @@ func main() {
146150
helpFunc(cmd, args)
147151
})
148152

149-
root.PersistentFlags().BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug output in the logs")
150-
root.PersistentFlags().StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to")
151-
opts.AddContextFlags(root.PersistentFlags())
152-
opts.AddConfigFlags(root.PersistentFlags())
153-
root.Flags().BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
153+
flags := root.Flags()
154+
flags.StringVarP(&opts.LogLevel, "log-level", "l", "info", "Set the logging level (\"debug\"|\"info\"|\"warn\"|\"error\"|\"fatal\")")
155+
flags.BoolVarP(&opts.Debug, "debug", "D", false, "Enable debug output in the logs")
156+
flags.StringVarP(&opts.Host, "host", "H", "", "Daemon socket(s) to connect to")
157+
opts.AddContextFlags(flags)
158+
opts.AddConfigFlags(flags)
159+
flags.BoolVarP(&opts.Version, "version", "v", false, "Print version information and quit")
154160

155161
walk(root, func(c *cobra.Command) {
156162
c.Flags().BoolP("help", "h", false, "Help for "+c.Name())
157163
})
158164

159165
// populate the opts with the global flags
160-
_ = root.PersistentFlags().Parse(os.Args[1:])
161-
_ = root.Flags().Parse(os.Args[1:])
166+
flags.Parse(os.Args[1:]) //nolint: errcheck
167+
168+
level, err := logrus.ParseLevel(opts.LogLevel)
169+
if err != nil {
170+
fmt.Fprintf(os.Stderr, "Unable to parse logging level: %s\n", opts.LogLevel)
171+
os.Exit(1)
172+
}
173+
logrus.SetLevel(level)
162174
if opts.Debug {
163175
logrus.SetLevel(logrus.DebugLevel)
164176
}
@@ -200,28 +212,32 @@ func main() {
200212
ctx = store.WithContextStore(ctx, s)
201213

202214
if err = root.ExecuteContext(ctx); err != nil {
203-
// if user canceled request, simply exit without any error message
204-
if errdefs.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {
205-
metrics.Track(ctype, os.Args[1:], metrics.CanceledStatus)
206-
os.Exit(130)
207-
}
208-
if ctype == store.AwsContextType {
209-
exit(currentContext, errors.Errorf(`%q context type has been renamed. Recreate the context by running:
210-
$ docker context create %s <name>`, cc.Type(), store.EcsContextType), ctype)
211-
}
212-
213-
// Context should always be handled by new CLI
214-
requiredCmd, _, _ := root.Find(os.Args[1:])
215-
if requiredCmd != nil && isContextAgnosticCommand(requiredCmd) {
216-
exit(currentContext, err, ctype)
217-
}
218-
mobycli.ExecIfDefaultCtxType(ctx, root)
215+
handleError(ctx, err, ctype, currentContext, cc, root)
216+
}
217+
metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus)
218+
}
219219

220-
checkIfUnknownCommandExistInDefaultContext(err, currentContext, ctype)
220+
func handleError(ctx context.Context, err error, ctype string, currentContext string, cc *store.DockerContext, root *cobra.Command) {
221+
// if user canceled request, simply exit without any error message
222+
if errdefs.IsErrCanceled(err) || errors.Is(ctx.Err(), context.Canceled) {
223+
metrics.Track(ctype, os.Args[1:], metrics.CanceledStatus)
224+
os.Exit(130)
225+
}
226+
if ctype == store.AwsContextType {
227+
exit(currentContext, errors.Errorf(`%q context type has been renamed. Recreate the context by running:
228+
$ docker context create %s <name>`, cc.Type(), store.EcsContextType), ctype)
229+
}
221230

231+
// Context should always be handled by new CLI
232+
requiredCmd, _, _ := root.Find(os.Args[1:])
233+
if requiredCmd != nil && isContextAgnosticCommand(requiredCmd) {
222234
exit(currentContext, err, ctype)
223235
}
224-
metrics.Track(ctype, os.Args[1:], metrics.SuccessStatus)
236+
mobycli.ExecIfDefaultCtxType(ctx, root)
237+
238+
checkIfUnknownCommandExistInDefaultContext(err, currentContext, ctype)
239+
240+
exit(currentContext, err, ctype)
225241
}
226242

227243
func exit(ctx string, err error, ctype string) {

cli/options/options.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525
type GlobalOpts struct {
2626
apicontext.ContextFlags
2727
cliconfig.ConfigFlags
28-
Debug bool
29-
Version bool
30-
Host string
28+
Debug bool
29+
LogLevel string
30+
Version bool
31+
Host string
3132
}

local/e2e/cli-only/e2e_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,6 @@ func TestLegacyLogin(t *testing.T) {
450450
Err: "WARNING! Using --password via the CLI is insecure",
451451
})
452452
})
453-
454-
t.Run("login help global flags", func(t *testing.T) {
455-
res := c.RunDockerCmd("login", "--help")
456-
assert.Assert(t, !strings.Contains(res.Combined(), "--log-level"))
457-
})
458453
}
459454

460455
func TestUnsupportedCommand(t *testing.T) {

local/e2e/compose/logs_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ func TestLocalComposeLogs(t *testing.T) {
3333
const projectName = "compose-e2e-logs"
3434

3535
t.Run("up", func(t *testing.T) {
36-
c.RunDockerCmd("compose", "up", "-d", "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "-d")
36+
c.RunDockerCmd("compose", "-f", "./fixtures/logs-test/compose.yaml", "--project-name", projectName, "up", "-d")
3737
})
3838

3939
t.Run("logs", func(t *testing.T) {
40-
res := c.RunDockerCmd("compose", "logs", "--project-name", projectName)
40+
res := c.RunDockerCmd("compose", "--project-name", projectName, "logs")
4141
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
4242
res.Assert(t, icmd.Expected{Out: `hello`})
4343
})
4444

4545
t.Run("logs ping", func(t *testing.T) {
46-
res := c.RunDockerCmd("compose", "logs", "--project-name", projectName, "ping")
46+
res := c.RunDockerCmd("compose", "--project-name", projectName, "logs", "ping")
4747
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
4848
assert.Assert(t, !strings.Contains(res.Stdout(), "hello"))
4949
})
5050

5151
t.Run("logs hello", func(t *testing.T) {
52-
res := c.RunDockerCmd("compose", "logs", "--project-name", projectName, "hello", "ping")
52+
res := c.RunDockerCmd("compose", "--project-name", projectName, "logs", "hello", "ping")
5353
res.Assert(t, icmd.Expected{Out: `PING localhost (127.0.0.1)`})
5454
res.Assert(t, icmd.Expected{Out: `hello`})
5555
})
5656

5757
t.Run("down", func(t *testing.T) {
58-
_ = c.RunDockerCmd("compose", "down", "--project-name", projectName)
58+
_ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
5959
})
6060
}

0 commit comments

Comments
 (0)