logs.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/porter-dev/porter/cli/cmd/api"
  6. "github.com/porter-dev/porter/cli/cmd/utils"
  7. "github.com/spf13/cobra"
  8. )
  9. // logsCmd represents the "porter logs" base command when called
  10. // without any subcommands
  11. var logsCmd = &cobra.Command{
  12. Use: "logs [release]",
  13. Args: cobra.ExactArgs(1),
  14. Short: "Logs the output from a given application.",
  15. Run: func(cmd *cobra.Command, args []string) {
  16. err := checkLoginAndRun(args, logs)
  17. if err != nil {
  18. os.Exit(1)
  19. }
  20. },
  21. }
  22. var follow bool
  23. func init() {
  24. rootCmd.AddCommand(logsCmd)
  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. }
  39. func logs(_ *api.AuthCheckResponse, client *api.Client, args []string) error {
  40. podsSimple, err := getPods(client, namespace, args[0])
  41. if err != nil {
  42. return fmt.Errorf("Could not retrieve list of pods: %s", err.Error())
  43. }
  44. // if length of pods is 0, throw error
  45. var selectedPod podSimple
  46. if len(podsSimple) == 0 {
  47. return fmt.Errorf("At least one pod must exist in this deployment.")
  48. } else if len(podsSimple) == 1 {
  49. selectedPod = podsSimple[0]
  50. } else {
  51. podNames := make([]string, 0)
  52. for _, podSimple := range podsSimple {
  53. podNames = append(podNames, podSimple.Name)
  54. }
  55. selectedPodName, err := utils.PromptSelect("Select the pod:", podNames)
  56. if err != nil {
  57. return err
  58. }
  59. // find selected pod
  60. for _, podSimple := range podsSimple {
  61. if selectedPodName == podSimple.Name {
  62. selectedPod = podSimple
  63. }
  64. }
  65. }
  66. var selectedContainerName string
  67. // if the selected pod has multiple container, spawn selector
  68. if len(selectedPod.ContainerNames) == 0 {
  69. return fmt.Errorf("At least one pod must exist in this deployment.")
  70. } else if len(selectedPod.ContainerNames) == 1 {
  71. selectedContainerName = selectedPod.ContainerNames[0]
  72. } else {
  73. selectedContainer, err := utils.PromptSelect("Select the container:", selectedPod.ContainerNames)
  74. if err != nil {
  75. return err
  76. }
  77. selectedContainerName = selectedContainer
  78. }
  79. config := &PorterRunSharedConfig{
  80. Client: client,
  81. }
  82. err = config.setSharedConfig()
  83. if err != nil {
  84. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  85. }
  86. _, err = pipePodLogsToStdout(config, namespace, selectedPod.Name, selectedContainerName, follow)
  87. return err
  88. }