get.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. var output string
  27. func init() {
  28. rootCmd.AddCommand(getCmd)
  29. getCmd.PersistentFlags().StringVar(
  30. &namespace,
  31. "namespace",
  32. "default",
  33. "the namespace of the release",
  34. )
  35. getCmd.PersistentFlags().StringVar(
  36. &output,
  37. "output",
  38. "",
  39. "the output format to use (\"yaml\" or \"json\")",
  40. )
  41. }
  42. type getReleaseInfo struct {
  43. Name string
  44. Namespace string
  45. LastDeployed time.Time `json:"last_deployed" yaml:"last_deployed"`
  46. ReleaseType string `json:"release_type" yaml:"release_type"`
  47. }
  48. func get(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  49. rel, err := client.GetRelease(context.Background(), config.Project, config.Cluster, namespace, args[0])
  50. if err != nil {
  51. return err
  52. }
  53. relInfo := &getReleaseInfo{
  54. Name: rel.Name,
  55. Namespace: rel.Namespace,
  56. LastDeployed: rel.Info.LastDeployed,
  57. ReleaseType: rel.Chart.Metadata.Name,
  58. }
  59. if output == "yaml" {
  60. bytes, err := yaml.Marshal(relInfo)
  61. if err != nil {
  62. return err
  63. }
  64. fmt.Println(string(bytes))
  65. } else if output == "json" {
  66. bytes, err := json.Marshal(relInfo)
  67. if err != nil {
  68. return err
  69. }
  70. fmt.Println(string(bytes))
  71. } else {
  72. fmt.Printf("Name: %s\n", relInfo.Name)
  73. fmt.Printf("Namespace: %s\n", relInfo.Namespace)
  74. fmt.Printf("Last deployed: %s\n", relInfo.LastDeployed)
  75. fmt.Printf("Release type: %s\n", relInfo.ReleaseType)
  76. }
  77. return nil
  78. }