get.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "github.com/porter-dev/porter/cli/cmd/config"
  8. v2 "github.com/porter-dev/porter/cli/cmd/v2"
  9. api "github.com/porter-dev/porter/api/client"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/spf13/cobra"
  12. "github.com/stefanmcshane/helm/pkg/time"
  13. "gopkg.in/yaml.v2"
  14. )
  15. // getCmd represents the "porter get" base command when called
  16. // without any subcommands
  17. var getCmd = &cobra.Command{
  18. Use: "get [release]",
  19. Args: cobra.ExactArgs(1),
  20. Short: "Fetches a release.",
  21. Run: func(cmd *cobra.Command, args []string) {
  22. err := checkLoginAndRun(cmd.Context(), args, get)
  23. if err != nil {
  24. os.Exit(1)
  25. }
  26. },
  27. }
  28. // getValuesCmd represents the "porter get values" command
  29. var getValuesCmd = &cobra.Command{
  30. Use: "values [release]",
  31. Args: cobra.ExactArgs(1),
  32. Short: "Fetches the Helm values for a release.",
  33. Run: func(cmd *cobra.Command, args []string) {
  34. err := checkLoginAndRun(cmd.Context(), args, getValues)
  35. if err != nil {
  36. os.Exit(1)
  37. }
  38. },
  39. }
  40. var output string
  41. func init() {
  42. getCmd.PersistentFlags().StringVar(
  43. &namespace,
  44. "namespace",
  45. "default",
  46. "the namespace of the release",
  47. )
  48. getCmd.PersistentFlags().StringVar(
  49. &output,
  50. "output",
  51. "",
  52. "the output format to use (\"yaml\" or \"json\")",
  53. )
  54. getCmd.AddCommand(getValuesCmd)
  55. rootCmd.AddCommand(getCmd)
  56. }
  57. type getReleaseInfo struct {
  58. Name string `json:"name" yaml:"name"`
  59. Namespace string `json:"namespace" yaml:"namespace"`
  60. LastDeployed time.Time `json:"last_deployed" yaml:"last_deployed"`
  61. ReleaseType string `json:"release_type" yaml:"release_type"`
  62. RevisionID int `json:"revision_id" yaml:"revision_id"`
  63. }
  64. func get(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  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(ctx, 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(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  109. project, err := client.GetProject(ctx, cliConf.Project)
  110. if err != nil {
  111. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  112. }
  113. if project.ValidateApplyV2 {
  114. err = v2.GetValues(ctx)
  115. if err != nil {
  116. return err
  117. }
  118. return nil
  119. }
  120. rel, err := client.GetRelease(ctx, cliConf.Project, cliConf.Cluster, namespace, args[0])
  121. if err != nil {
  122. return err
  123. }
  124. values := rel.Config
  125. if output == "json" {
  126. bytes, err := json.Marshal(values)
  127. if err != nil {
  128. return err
  129. }
  130. fmt.Println(string(bytes))
  131. } else { // yaml is the default
  132. bytes, err := yaml.Marshal(values)
  133. if err != nil {
  134. return err
  135. }
  136. fmt.Println(string(bytes))
  137. }
  138. return nil
  139. }