get.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. v2 "github.com/porter-dev/porter/cli/cmd/v2"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/spf13/cobra"
  11. "github.com/stefanmcshane/helm/pkg/time"
  12. "gopkg.in/yaml.v2"
  13. )
  14. // getCmd represents the "porter get" base command when called
  15. // without any subcommands
  16. var getCmd = &cobra.Command{
  17. Use: "get [release]",
  18. Args: cobra.ExactArgs(1),
  19. Short: "Fetches a release.",
  20. Run: func(cmd *cobra.Command, args []string) {
  21. err := checkLoginAndRun(args, get)
  22. if err != nil {
  23. os.Exit(1)
  24. }
  25. },
  26. }
  27. // getValuesCmd represents the "porter get values" command
  28. var getValuesCmd = &cobra.Command{
  29. Use: "values [release]",
  30. Args: cobra.ExactArgs(1),
  31. Short: "Fetches the Helm values for a release.",
  32. Run: func(cmd *cobra.Command, args []string) {
  33. err := checkLoginAndRun(args, getValues)
  34. if err != nil {
  35. os.Exit(1)
  36. }
  37. },
  38. }
  39. var output string
  40. func init() {
  41. getCmd.PersistentFlags().StringVar(
  42. &namespace,
  43. "namespace",
  44. "default",
  45. "the namespace of the release",
  46. )
  47. getCmd.PersistentFlags().StringVar(
  48. &output,
  49. "output",
  50. "",
  51. "the output format to use (\"yaml\" or \"json\")",
  52. )
  53. getCmd.AddCommand(getValuesCmd)
  54. rootCmd.AddCommand(getCmd)
  55. }
  56. type getReleaseInfo struct {
  57. Name string `json:"name" yaml:"name"`
  58. Namespace string `json:"namespace" yaml:"namespace"`
  59. LastDeployed time.Time `json:"last_deployed" yaml:"last_deployed"`
  60. ReleaseType string `json:"release_type" yaml:"release_type"`
  61. RevisionID int `json:"revision_id" yaml:"revision_id"`
  62. }
  63. func get(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  64. ctx := context.Background()
  65. project, err := client.GetProject(ctx, cliConf.Project)
  66. if err != nil {
  67. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  68. }
  69. if project.ValidateApplyV2 {
  70. err = v2.Get(ctx)
  71. if err != nil {
  72. return err
  73. }
  74. return nil
  75. }
  76. rel, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, args[0])
  77. if err != nil {
  78. return err
  79. }
  80. relInfo := &getReleaseInfo{
  81. Name: rel.Name,
  82. Namespace: rel.Namespace,
  83. LastDeployed: rel.Info.LastDeployed,
  84. ReleaseType: rel.Chart.Metadata.Name,
  85. RevisionID: rel.Release.Version,
  86. }
  87. if output == "yaml" {
  88. bytes, err := yaml.Marshal(relInfo)
  89. if err != nil {
  90. return err
  91. }
  92. fmt.Println(string(bytes))
  93. } else if output == "json" {
  94. bytes, err := json.Marshal(relInfo)
  95. if err != nil {
  96. return err
  97. }
  98. fmt.Println(string(bytes))
  99. } else {
  100. fmt.Printf("Name: %s\n", relInfo.Name)
  101. fmt.Printf("Namespace: %s\n", relInfo.Namespace)
  102. fmt.Printf("Last deployed: %s\n", relInfo.LastDeployed)
  103. fmt.Printf("Release type: %s\n", relInfo.ReleaseType)
  104. fmt.Printf("Revision ID: %d\n", relInfo.RevisionID)
  105. }
  106. return nil
  107. }
  108. func getValues(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  109. ctx := context.Background()
  110. project, err := client.GetProject(ctx, cliConf.Project)
  111. if err != nil {
  112. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  113. }
  114. if project.ValidateApplyV2 {
  115. err = v2.GetValues(ctx)
  116. if err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. rel, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, args[0])
  122. if err != nil {
  123. return err
  124. }
  125. values := rel.Config
  126. if output == "json" {
  127. bytes, err := json.Marshal(values)
  128. if err != nil {
  129. return err
  130. }
  131. fmt.Println(string(bytes))
  132. } else { // yaml is the default
  133. bytes, err := yaml.Marshal(values)
  134. if err != nil {
  135. return err
  136. }
  137. fmt.Println(string(bytes))
  138. }
  139. return nil
  140. }