get.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "github.com/stefanmcshane/helm/pkg/time"
  11. "gopkg.in/yaml.v2"
  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 `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(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  63. rel, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, args[0])
  64. if err != nil {
  65. return err
  66. }
  67. relInfo := &getReleaseInfo{
  68. Name: rel.Name,
  69. Namespace: rel.Namespace,
  70. LastDeployed: rel.Info.LastDeployed,
  71. ReleaseType: rel.Chart.Metadata.Name,
  72. RevisionID: rel.Release.Version,
  73. }
  74. if output == "yaml" {
  75. bytes, err := yaml.Marshal(relInfo)
  76. if err != nil {
  77. return err
  78. }
  79. fmt.Println(string(bytes))
  80. } else if output == "json" {
  81. bytes, err := json.Marshal(relInfo)
  82. if err != nil {
  83. return err
  84. }
  85. fmt.Println(string(bytes))
  86. } else {
  87. fmt.Printf("Name: %s\n", relInfo.Name)
  88. fmt.Printf("Namespace: %s\n", relInfo.Namespace)
  89. fmt.Printf("Last deployed: %s\n", relInfo.LastDeployed)
  90. fmt.Printf("Release type: %s\n", relInfo.ReleaseType)
  91. fmt.Printf("Revision ID: %d\n", relInfo.RevisionID)
  92. }
  93. return nil
  94. }
  95. func getValues(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  96. rel, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, args[0])
  97. if err != nil {
  98. return err
  99. }
  100. values := rel.Config
  101. if output == "json" {
  102. bytes, err := json.Marshal(values)
  103. if err != nil {
  104. return err
  105. }
  106. fmt.Println(string(bytes))
  107. } else { // yaml is the default
  108. bytes, err := yaml.Marshal(values)
  109. if err != nil {
  110. return err
  111. }
  112. fmt.Println(string(bytes))
  113. }
  114. return nil
  115. }