| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package cmd
- import (
- "context"
- "encoding/json"
- "fmt"
- "os"
- api "github.com/porter-dev/porter/api/client"
- "github.com/porter-dev/porter/api/types"
- "github.com/spf13/cobra"
- "gopkg.in/yaml.v2"
- "helm.sh/helm/v3/pkg/time"
- )
- // getCmd represents the "porter get" base command when called
- // without any subcommands
- var getCmd = &cobra.Command{
- Use: "get [release]",
- Args: cobra.ExactArgs(1),
- Short: "Fetches a release.",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRun(args, get)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- // getValuesCmd represents the "porter get values" command
- var getValuesCmd = &cobra.Command{
- Use: "values [release]",
- Args: cobra.ExactArgs(1),
- Short: "Fetches the Helm values for a release.",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRun(args, getValues)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- var output string
- func init() {
- getCmd.PersistentFlags().StringVar(
- &namespace,
- "namespace",
- "default",
- "the namespace of the release",
- )
- getCmd.PersistentFlags().StringVar(
- &output,
- "output",
- "",
- "the output format to use (\"yaml\" or \"json\")",
- )
- getCmd.AddCommand(getValuesCmd)
- rootCmd.AddCommand(getCmd)
- }
- type getReleaseInfo struct {
- Name string
- Namespace string
- LastDeployed time.Time `json:"last_deployed" yaml:"last_deployed"`
- ReleaseType string `json:"release_type" yaml:"release_type"`
- }
- func get(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
- rel, err := client.GetRelease(context.Background(), config.Project, config.Cluster, namespace, args[0])
- if err != nil {
- return err
- }
- relInfo := &getReleaseInfo{
- Name: rel.Name,
- Namespace: rel.Namespace,
- LastDeployed: rel.Info.LastDeployed,
- ReleaseType: rel.Chart.Metadata.Name,
- }
- if output == "yaml" {
- bytes, err := yaml.Marshal(relInfo)
- if err != nil {
- return err
- }
- fmt.Println(string(bytes))
- } else if output == "json" {
- bytes, err := json.Marshal(relInfo)
- if err != nil {
- return err
- }
- fmt.Println(string(bytes))
- } else {
- fmt.Printf("Name: %s\n", relInfo.Name)
- fmt.Printf("Namespace: %s\n", relInfo.Namespace)
- fmt.Printf("Last deployed: %s\n", relInfo.LastDeployed)
- fmt.Printf("Release type: %s\n", relInfo.ReleaseType)
- }
- return nil
- }
- func getValues(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
- rel, err := client.GetRelease(context.Background(), config.Project, config.Cluster, namespace, args[0])
- if err != nil {
- return err
- }
- values := rel.Config
- if output == "json" {
- bytes, err := json.Marshal(values)
- if err != nil {
- return err
- }
- fmt.Println(string(bytes))
- } else { // yaml is the default
- bytes, err := yaml.Marshal(values)
- if err != nil {
- return err
- }
- fmt.Println(string(bytes))
- }
- return nil
- }
|