target.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package commands
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "sort"
  7. "text/tabwriter"
  8. "github.com/fatih/color"
  9. api "github.com/porter-dev/porter/api/client"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/cli/cmd/config"
  12. "github.com/spf13/cobra"
  13. )
  14. func registerCommand_Target(cliConf config.CLIConfig) *cobra.Command {
  15. targetCmd := &cobra.Command{
  16. Use: "target",
  17. Aliases: []string{"targets"},
  18. Short: "Commands that control Porter target settings",
  19. }
  20. createTargetCmd := &cobra.Command{
  21. Use: "create --name [name]",
  22. Short: "Creates a deployment target",
  23. Run: func(cmd *cobra.Command, args []string) {
  24. err := checkLoginAndRunWithConfig(cmd, cliConf, args, createTarget)
  25. if err != nil {
  26. os.Exit(1)
  27. }
  28. },
  29. }
  30. var targetName string
  31. createTargetCmd.Flags().StringVar(&targetName, "name", "", "Name of deployment target")
  32. targetCmd.AddCommand(createTargetCmd)
  33. listTargetCmd := &cobra.Command{
  34. Use: "list",
  35. Short: "Lists the deployment targets for the logged in user",
  36. Long: `Lists the deployment targets in the project
  37. The following columns are returned:
  38. * ID: id of the deployment target
  39. * NAME: name of the deployment target
  40. * CLUSTER-ID: id of the cluster associated with the deployment target
  41. * DEFAULT: whether the deployment target is the default target for the cluster
  42. If the --preview flag is set, only deployment targets for preview environments will be returned.
  43. `,
  44. Run: func(cmd *cobra.Command, args []string) {
  45. err := checkLoginAndRunWithConfig(cmd, cliConf, args, listTargets)
  46. if err != nil {
  47. os.Exit(1)
  48. }
  49. },
  50. }
  51. var includePreviews bool
  52. listTargetCmd.Flags().BoolVar(&includePreviews, "preview", false, "List preview environments")
  53. targetCmd.AddCommand(listTargetCmd)
  54. return targetCmd
  55. }
  56. func createTarget(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
  57. targetName, err := cmd.Flags().GetString("name")
  58. if err != nil {
  59. return fmt.Errorf("error finding name flag: %w", err)
  60. }
  61. resp, err := client.CreateDeploymentTarget(ctx, cliConf.Project, &types.CreateDeploymentTargetRequest{
  62. Name: targetName,
  63. ClusterId: cliConf.Cluster,
  64. })
  65. if err != nil {
  66. return err
  67. }
  68. _, _ = color.New(color.FgGreen).Printf("Created target with name %s and id %s\n", targetName, resp.DeploymentTargetID)
  69. return nil
  70. }
  71. func listTargets(ctx context.Context, user *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, featureFlags config.FeatureFlags, cmd *cobra.Command, args []string) error {
  72. includePreviews, err := cmd.Flags().GetBool("preview")
  73. if err != nil {
  74. return fmt.Errorf("error finding preview flag: %w", err)
  75. }
  76. resp, err := client.ListDeploymentTargets(ctx, cliConf.Project, includePreviews)
  77. if err != nil {
  78. return err
  79. }
  80. if resp == nil {
  81. return nil
  82. }
  83. targets := *resp
  84. sort.Slice(targets.DeploymentTargets, func(i, j int) bool {
  85. if targets.DeploymentTargets[i].ClusterID != targets.DeploymentTargets[j].ClusterID {
  86. return targets.DeploymentTargets[i].ClusterID < targets.DeploymentTargets[j].ClusterID
  87. }
  88. return targets.DeploymentTargets[i].Name < targets.DeploymentTargets[j].Name
  89. })
  90. w := new(tabwriter.Writer)
  91. w.Init(os.Stdout, 3, 8, 0, '\t', tabwriter.AlignRight)
  92. if includePreviews {
  93. fmt.Fprintf(w, "%s\t%s\t%s\n", "ID", "NAME", "CLUSTER-ID")
  94. for _, target := range targets.DeploymentTargets {
  95. fmt.Fprintf(w, "%s\t%s\t%d\n", target.ID, target.Name, target.ClusterID)
  96. }
  97. } else {
  98. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", "ID", "NAME", "CLUSTER-ID", "DEFAULT")
  99. for _, target := range targets.DeploymentTargets {
  100. fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", target.ID, target.Name, target.ClusterID, checkmark(target.IsDefault))
  101. }
  102. }
  103. _ = w.Flush()
  104. return nil
  105. }
  106. func checkmark(b bool) string {
  107. if b {
  108. return "✓"
  109. }
  110. return ""
  111. }