get.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. api "github.com/porter-dev/porter/api/client"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/spf13/cobra"
  10. "gopkg.in/yaml.v2"
  11. "helm.sh/helm/v3/pkg/time"
  12. )
  13. // getCmd represents the "porter get" base command when called
  14. // without any subcommands
  15. var getCmd = &cobra.Command{
  16. Use: "get [release]",
  17. Args: cobra.ExactArgs(1),
  18. Short: "Fetches a release.",
  19. Run: func(cmd *cobra.Command, args []string) {
  20. err := checkLoginAndRun(args, get)
  21. if err != nil {
  22. os.Exit(1)
  23. }
  24. },
  25. }
  26. // getValuesCmd represents the "porter get values" command
  27. var getValuesCmd = &cobra.Command{
  28. Use: "values [release]",
  29. Args: cobra.ExactArgs(1),
  30. Short: "Fetches the Helm values for a release.",
  31. Run: func(cmd *cobra.Command, args []string) {
  32. err := checkLoginAndRun(args, getValues)
  33. if err != nil {
  34. os.Exit(1)
  35. }
  36. },
  37. }
  38. var output string
  39. func init() {
  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. rootCmd.AddCommand(getCmd)
  54. }
  55. type getReleaseInfo struct {
  56. Name string
  57. Namespace string
  58. LastDeployed time.Time `json:"last_deployed" yaml:"last_deployed"`
  59. ReleaseType string `json:"release_type" yaml:"release_type"`
  60. }
  61. func get(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  62. rel, err := client.GetRelease(context.Background(), config.Project, config.Cluster, namespace, args[0])
  63. if err != nil {
  64. return err
  65. }
  66. relInfo := &getReleaseInfo{
  67. Name: rel.Name,
  68. Namespace: rel.Namespace,
  69. LastDeployed: rel.Info.LastDeployed,
  70. ReleaseType: rel.Chart.Metadata.Name,
  71. }
  72. if output == "yaml" {
  73. bytes, err := yaml.Marshal(relInfo)
  74. if err != nil {
  75. return err
  76. }
  77. fmt.Println(string(bytes))
  78. } else if output == "json" {
  79. bytes, err := json.Marshal(relInfo)
  80. if err != nil {
  81. return err
  82. }
  83. fmt.Println(string(bytes))
  84. } else {
  85. fmt.Printf("Name: %s\n", relInfo.Name)
  86. fmt.Printf("Namespace: %s\n", relInfo.Namespace)
  87. fmt.Printf("Last deployed: %s\n", relInfo.LastDeployed)
  88. fmt.Printf("Release type: %s\n", relInfo.ReleaseType)
  89. }
  90. return nil
  91. }
  92. func getValues(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  93. rel, err := client.GetRelease(context.Background(), config.Project, config.Cluster, namespace, args[0])
  94. if err != nil {
  95. return err
  96. }
  97. values := rel.Config
  98. if output == "json" {
  99. bytes, err := json.Marshal(values)
  100. if err != nil {
  101. return err
  102. }
  103. fmt.Println(string(bytes))
  104. } else { // yaml is the default
  105. bytes, err := yaml.Marshal(values)
  106. if err != nil {
  107. return err
  108. }
  109. fmt.Println(string(bytes))
  110. }
  111. return nil
  112. }