get.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, 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, 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, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
  63. if featureFlags.ValidateApplyV2Enabled {
  64. err := v2.Get(ctx)
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. }
  70. rel, err := client.GetRelease(ctx, cliConf.Project, cliConf.Cluster, namespace, args[0])
  71. if err != nil {
  72. return err
  73. }
  74. relInfo := &getReleaseInfo{
  75. Name: rel.Name,
  76. Namespace: rel.Namespace,
  77. LastDeployed: rel.Info.LastDeployed,
  78. ReleaseType: rel.Chart.Metadata.Name,
  79. RevisionID: rel.Release.Version,
  80. }
  81. if output == "yaml" {
  82. bytes, err := yaml.Marshal(relInfo)
  83. if err != nil {
  84. return err
  85. }
  86. fmt.Println(string(bytes))
  87. } else if output == "json" {
  88. bytes, err := json.Marshal(relInfo)
  89. if err != nil {
  90. return err
  91. }
  92. fmt.Println(string(bytes))
  93. } else {
  94. fmt.Printf("Name: %s\n", relInfo.Name)
  95. fmt.Printf("Namespace: %s\n", relInfo.Namespace)
  96. fmt.Printf("Last deployed: %s\n", relInfo.LastDeployed)
  97. fmt.Printf("Release type: %s\n", relInfo.ReleaseType)
  98. fmt.Printf("Revision ID: %d\n", relInfo.RevisionID)
  99. }
  100. return nil
  101. }
  102. func getValues(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
  103. if featureFlags.ValidateApplyV2Enabled {
  104. err := v2.GetValues(ctx)
  105. if err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. rel, err := client.GetRelease(ctx, cliConf.Project, cliConf.Cluster, namespace, args[0])
  111. if err != nil {
  112. return err
  113. }
  114. values := rel.Config
  115. if output == "json" {
  116. bytes, err := json.Marshal(values)
  117. if err != nil {
  118. return err
  119. }
  120. fmt.Println(string(bytes))
  121. } else { // yaml is the default
  122. bytes, err := yaml.Marshal(values)
  123. if err != nil {
  124. return err
  125. }
  126. fmt.Println(string(bytes))
  127. }
  128. return nil
  129. }