logs.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "os"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose-cli/client"
  19. )
  20. func logsCommand() *cobra.Command {
  21. opts := composeOptions{}
  22. logsCmd := &cobra.Command{
  23. Use: "logs",
  24. RunE: func(cmd *cobra.Command, args []string) error {
  25. return runLogs(cmd.Context(), opts)
  26. },
  27. }
  28. logsCmd.Flags().StringVarP(&opts.Name, "project-name", "p", "", "Project name")
  29. logsCmd.Flags().StringVar(&opts.WorkingDir, "workdir", "", "Work dir")
  30. logsCmd.Flags().StringArrayVarP(&opts.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  31. return logsCmd
  32. }
  33. func runLogs(ctx context.Context, opts composeOptions) error {
  34. c, err := client.New(ctx)
  35. if err != nil {
  36. return err
  37. }
  38. projectName, err := opts.toProjectName()
  39. if err != nil {
  40. return err
  41. }
  42. return c.ComposeService().Logs(ctx, projectName, os.Stdout)
  43. }