introduce compose logs --tail and --follow options

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-01-26 16:46:15 +01:00
parent 24b5aeaa32
commit 7d0e1dfc3c
8 changed files with 25 additions and 10 deletions

View file

@ -97,7 +97,7 @@ func Command(contextType string) *cobra.Command {
stopCommand(&opts),
psCommand(&opts),
listCommand(),
logsCommand(&opts),
logsCommand(&opts, contextType),
convertCommand(&opts),
runCommand(&opts),
)

View file

@ -24,15 +24,18 @@ import (
"github.com/docker/compose-cli/api/client"
"github.com/docker/compose-cli/api/compose"
"github.com/docker/compose-cli/api/context/store"
"github.com/docker/compose-cli/cli/formatter"
)
type logsOptions struct {
*projectOptions
composeOptions
follow bool
tail string
}
func logsCommand(p *projectOptions) *cobra.Command {
func logsCommand(p *projectOptions, contextType string) *cobra.Command {
opts := logsOptions{
projectOptions: p,
}
@ -43,6 +46,10 @@ func logsCommand(p *projectOptions) *cobra.Command {
return runLogs(cmd.Context(), opts, args)
},
}
logsCmd.Flags().BoolVarP(&opts.follow, "follow", "f", false, "Follow log output.")
if contextType == store.DefaultContextType {
logsCmd.Flags().StringVar(&opts.tail, "tail", "all", "Number of lines to show from the end of the logs for each container.")
}
return logsCmd
}
@ -59,5 +66,7 @@ func runLogs(ctx context.Context, opts logsOptions, services []string) error {
consumer := formatter.NewLogConsumer(ctx, os.Stdout)
return c.ComposeService().Logs(ctx, projectName, consumer, compose.LogOptions{
Services: services,
Follow: opts.follow,
Tail: opts.tail,
})
}