logs.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package cmd
  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. // logsCmd represents the "porter logs" base command when called
  13. // without any subcommands
  14. var 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 := checkLoginAndRun(cmd.Context(), args, logs)
  20. if err != nil {
  21. os.Exit(1)
  22. }
  23. },
  24. }
  25. var follow bool
  26. func init() {
  27. rootCmd.AddCommand(logsCmd)
  28. logsCmd.PersistentFlags().StringVar(
  29. &namespace,
  30. "namespace",
  31. "default",
  32. "namespace of release to connect to",
  33. )
  34. logsCmd.PersistentFlags().BoolVarP(
  35. &follow,
  36. "follow",
  37. "f",
  38. false,
  39. "specify if the logs should be streamed",
  40. )
  41. }
  42. func logs(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, args []string) error {
  43. podsSimple, err := getPods(ctx, client, cliConfig, namespace, args[0])
  44. if err != nil {
  45. return fmt.Errorf("Could not retrieve list of pods: %s", err.Error())
  46. }
  47. // if length of pods is 0, throw error
  48. var selectedPod podSimple
  49. if len(podsSimple) == 0 {
  50. return fmt.Errorf("At least one pod must exist in this deployment.")
  51. } else if len(podsSimple) == 1 {
  52. selectedPod = podsSimple[0]
  53. } else {
  54. podNames := make([]string, 0)
  55. for _, podSimple := range podsSimple {
  56. podNames = append(podNames, podSimple.Name)
  57. }
  58. selectedPodName, err := utils.PromptSelect("Select the pod:", podNames)
  59. if err != nil {
  60. return err
  61. }
  62. // find selected pod
  63. for _, podSimple := range podsSimple {
  64. if selectedPodName == podSimple.Name {
  65. selectedPod = podSimple
  66. }
  67. }
  68. }
  69. var selectedContainerName string
  70. // if the selected pod has multiple container, spawn selector
  71. if len(selectedPod.ContainerNames) == 0 {
  72. return fmt.Errorf("At least one pod must exist in this deployment.")
  73. } else if len(selectedPod.ContainerNames) == 1 {
  74. selectedContainerName = selectedPod.ContainerNames[0]
  75. } else {
  76. selectedContainer, err := utils.PromptSelect("Select the container:", selectedPod.ContainerNames)
  77. if err != nil {
  78. return err
  79. }
  80. selectedContainerName = selectedContainer
  81. }
  82. config := &PorterRunSharedConfig{
  83. Client: client,
  84. CLIConfig: cliConfig,
  85. }
  86. err = config.setSharedConfig(ctx)
  87. if err != nil {
  88. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  89. }
  90. _, err = pipePodLogsToStdout(ctx, config, namespace, selectedPod.Name, selectedContainerName, follow)
  91. return err
  92. }