get.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. )
  12. // getCmd represents the "porter get" base command when called
  13. // without any subcommands
  14. var getCmd = &cobra.Command{
  15. Use: "get [release]",
  16. Args: cobra.ExactArgs(1),
  17. Short: "Fetches a release.",
  18. Run: func(cmd *cobra.Command, args []string) {
  19. err := checkLoginAndRun(args, get)
  20. if err != nil {
  21. os.Exit(1)
  22. }
  23. },
  24. }
  25. var output string
  26. func init() {
  27. rootCmd.AddCommand(getCmd)
  28. getCmd.PersistentFlags().StringVar(
  29. &namespace,
  30. "namespace",
  31. "default",
  32. "the namespace of the release",
  33. )
  34. getCmd.PersistentFlags().StringVar(
  35. &output,
  36. "output",
  37. "yaml",
  38. "the output format to use (\"yaml\" or \"json\")",
  39. )
  40. }
  41. func get(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  42. rel, err := client.GetRelease(context.Background(), config.Project, config.Cluster, namespace, args[0])
  43. if err != nil {
  44. return err
  45. }
  46. var bytes []byte
  47. if output == "yaml" {
  48. bytes, err = yaml.Marshal(rel)
  49. if err != nil {
  50. return err
  51. }
  52. } else if output == "json" {
  53. bytes, err = json.Marshal(rel)
  54. if err != nil {
  55. return err
  56. }
  57. } else {
  58. return fmt.Errorf("invalid output format: %s", output)
  59. }
  60. fmt.Println(string(bytes))
  61. return nil
  62. }