get.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package commands
  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. var output string
  16. func registerCommand_Get(cliConf config.CLIConfig) *cobra.Command {
  17. 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 := checkLoginAndRunWithConfig(cmd.Context(), cliConf, args, get)
  23. if err != nil {
  24. os.Exit(1)
  25. }
  26. },
  27. }
  28. // getValuesCmd represents the "porter get values" command
  29. 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 := checkLoginAndRunWithConfig(cmd.Context(), cliConf, args, getValues)
  35. if err != nil {
  36. os.Exit(1)
  37. }
  38. },
  39. }
  40. getCmd.PersistentFlags().StringVar(
  41. &namespace,
  42. "namespace",
  43. "default",
  44. "the namespace of the release",
  45. )
  46. getCmd.PersistentFlags().StringVar(
  47. &output,
  48. "output",
  49. "",
  50. "the output format to use (\"yaml\" or \"json\")",
  51. )
  52. getCmd.AddCommand(getValuesCmd)
  53. return getCmd
  54. }
  55. type getReleaseInfo struct {
  56. Name string `json:"name" yaml:"name"`
  57. Namespace string `json:"namespace" yaml:"namespace"`
  58. LastDeployed time.Time `json:"last_deployed" yaml:"last_deployed"`
  59. ReleaseType string `json:"release_type" yaml:"release_type"`
  60. RevisionID int `json:"revision_id" yaml:"revision_id"`
  61. }
  62. func get(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  63. project, err := client.GetProject(ctx, cliConf.Project)
  64. if err != nil {
  65. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  66. }
  67. if project.ValidateApplyV2 {
  68. err = v2.Get(ctx)
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. rel, err := client.GetRelease(ctx, cliConf.Project, cliConf.Cluster, namespace, args[0])
  75. if err != nil {
  76. return err
  77. }
  78. relInfo := &getReleaseInfo{
  79. Name: rel.Name,
  80. Namespace: rel.Namespace,
  81. LastDeployed: rel.Info.LastDeployed,
  82. ReleaseType: rel.Chart.Metadata.Name,
  83. RevisionID: rel.Release.Version,
  84. }
  85. if output == "yaml" {
  86. bytes, err := yaml.Marshal(relInfo)
  87. if err != nil {
  88. return err
  89. }
  90. fmt.Println(string(bytes))
  91. } else if output == "json" {
  92. bytes, err := json.Marshal(relInfo)
  93. if err != nil {
  94. return err
  95. }
  96. fmt.Println(string(bytes))
  97. } else {
  98. fmt.Printf("Name: %s\n", relInfo.Name)
  99. fmt.Printf("Namespace: %s\n", relInfo.Namespace)
  100. fmt.Printf("Last deployed: %s\n", relInfo.LastDeployed)
  101. fmt.Printf("Release type: %s\n", relInfo.ReleaseType)
  102. fmt.Printf("Revision ID: %d\n", relInfo.RevisionID)
  103. }
  104. return nil
  105. }
  106. func getValues(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, args []string) error {
  107. project, err := client.GetProject(ctx, cliConf.Project)
  108. if err != nil {
  109. return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
  110. }
  111. if project.ValidateApplyV2 {
  112. err = v2.GetValues(ctx)
  113. if err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. rel, err := client.GetRelease(ctx, cliConf.Project, cliConf.Cluster, namespace, args[0])
  119. if err != nil {
  120. return err
  121. }
  122. values := rel.Config
  123. if output == "json" {
  124. bytes, err := json.Marshal(values)
  125. if err != nil {
  126. return err
  127. }
  128. fmt.Println(string(bytes))
  129. } else { // yaml is the default
  130. bytes, err := yaml.Marshal(values)
  131. if err != nil {
  132. return err
  133. }
  134. fmt.Println(string(bytes))
  135. }
  136. return nil
  137. }