2
0

logs.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package cmd
  2. import (
  3. "fmt"
  4. "os"
  5. api "github.com/porter-dev/porter/api/client"
  6. "github.com/porter-dev/porter/api/types"
  7. "github.com/porter-dev/porter/cli/cmd/utils"
  8. "github.com/spf13/cobra"
  9. )
  10. // logsCmd represents the "porter logs" base command when called
  11. // without any subcommands
  12. var logsCmd = &cobra.Command{
  13. Use: "logs [release]",
  14. Args: cobra.ExactArgs(1),
  15. Short: "Logs the output from a given application.",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. err := checkLoginAndRun(args, logs)
  18. if err != nil {
  19. os.Exit(1)
  20. }
  21. },
  22. }
  23. var follow bool
  24. func init() {
  25. rootCmd.AddCommand(logsCmd)
  26. logsCmd.PersistentFlags().StringVar(
  27. &namespace,
  28. "namespace",
  29. "default",
  30. "namespace of release to connect to",
  31. )
  32. logsCmd.PersistentFlags().BoolVarP(
  33. &follow,
  34. "follow",
  35. "f",
  36. false,
  37. "specify if the logs should be streamed",
  38. )
  39. }
  40. func logs(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  41. podsSimple, err := getPods(client, 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. }
  83. err = config.setSharedConfig()
  84. if err != nil {
  85. return fmt.Errorf("Could not retrieve kube credentials: %s", err.Error())
  86. }
  87. _, err = pipePodLogsToStdout(config, namespace, selectedPod.Name, selectedContainerName, follow)
  88. return err
  89. }