logs.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. api "github.com/porter-dev/porter/api/client"
  7. "github.com/porter-dev/porter/api/types"
  8. "github.com/porter-dev/porter/cli/cmd/config"
  9. "github.com/porter-dev/porter/cli/cmd/utils"
  10. "github.com/spf13/cobra"
  11. )
  12. var follow bool
  13. func registerCommand_Logs(cliConf config.CLIConfig) *cobra.Command {
  14. logsCmd := &cobra.Command{
  15. Use: "logs [release]",
  16. Args: cobra.ExactArgs(1),
  17. Short: "Logs the output from a given application.",
  18. Run: func(cmd *cobra.Command, args []string) {
  19. err := checkLoginAndRunWithConfig(cmd, cliConf, args, logs)
  20. if err != nil {
  21. os.Exit(1)
  22. }
  23. },
  24. }
  25. logsCmd.PersistentFlags().StringVar(
  26. &namespace,
  27. "namespace",
  28. "default",
  29. "namespace of release to connect to",
  30. )
  31. logsCmd.PersistentFlags().BoolVarP(
  32. &follow,
  33. "follow",
  34. "f",
  35. false,
  36. "specify if the logs should be streamed",
  37. )
  38. return logsCmd
  39. }
  40. func logs(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, _ config.FeatureFlags, args []string) error {
  41. podsSimple, err := getPods(ctx, client, cliConfig, namespace, args[0])
  42. if err != nil {
  43. return fmt.Errorf("Could not retrieve list of pods: %s", err.Error())
  44. }
  45. // if length of pods is 0, throw error
  46. var selectedPod podSimple
  47. if len(podsSimple) == 0 {
  48. return fmt.Errorf("At least one pod must exist in this deployment.")
  49. } else if len(podsSimple) == 1 {
  50. selectedPod = podsSimple[0]
  51. } else {
  52. podNames := make([]string, 0)
  53. for _, podSimple := range podsSimple {
  54. podNames = append(podNames, podSimple.Name)
  55. }
  56. selectedPodName, err := utils.PromptSelect("Select the pod:", podNames)
  57. if err != nil {
  58. return err
  59. }
  60. // find selected pod
  61. for _, podSimple := range podsSimple {
  62. if selectedPodName == podSimple.Name {
  63. selectedPod = podSimple
  64. }
  65. }
  66. }
  67. var selectedContainerName string
  68. // if the selected pod has multiple container, spawn selector
  69. if len(selectedPod.ContainerNames) == 0 {
  70. return fmt.Errorf("At least one pod must exist in this deployment.")
  71. } else if len(selectedPod.ContainerNames) == 1 {
  72. selectedContainerName = selectedPod.ContainerNames[0]
  73. } else {
  74. selectedContainer, err := utils.PromptSelect("Select the container:", selectedPod.ContainerNames)
  75. if err != nil {
  76. return err
  77. }
  78. selectedContainerName = selectedContainer
  79. }
  80. config := &PorterRunSharedConfig{
  81. Client: client,
  82. CLIConfig: cliConfig,
  83. }
  84. err = config.setSharedConfig(ctx)
  85. if err != nil {
  86. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  87. }
  88. _, err = pipePodLogsToStdout(ctx, config, namespace, selectedPod.Name, selectedContainerName, follow)
  89. return err
  90. }