connect.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package cmd
  2. import (
  3. "os"
  4. "github.com/porter-dev/porter/cli/cmd/api"
  5. "github.com/porter-dev/porter/cli/cmd/connect"
  6. "github.com/spf13/cobra"
  7. )
  8. var (
  9. kubeconfigPath string
  10. print *bool
  11. contexts *[]string
  12. )
  13. var connectCmd = &cobra.Command{
  14. Use: "connect",
  15. Short: "Commands that connect to external clusters and providers",
  16. }
  17. var connectKubeconfigCmd = &cobra.Command{
  18. Use: "kubeconfig",
  19. Short: "Uses the local kubeconfig to add a cluster",
  20. Run: func(cmd *cobra.Command, args []string) {
  21. err := checkLoginAndRun(args, runConnectKubeconfig)
  22. if err != nil {
  23. os.Exit(1)
  24. }
  25. },
  26. }
  27. var connectECRCmd = &cobra.Command{
  28. Use: "ecr",
  29. Short: "Adds an ECR instance to a project",
  30. Run: func(cmd *cobra.Command, args []string) {
  31. err := checkLoginAndRun(args, runConnectECR)
  32. if err != nil {
  33. os.Exit(1)
  34. }
  35. },
  36. }
  37. var connectGCRCmd = &cobra.Command{
  38. Use: "gcr",
  39. Short: "Adds a GCR instance to a project",
  40. Run: func(cmd *cobra.Command, args []string) {
  41. err := checkLoginAndRun(args, runConnectGCR)
  42. if err != nil {
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. func init() {
  48. rootCmd.AddCommand(connectCmd)
  49. connectCmd.AddCommand(connectKubeconfigCmd)
  50. connectCmd.PersistentFlags().StringVar(
  51. &host,
  52. "host",
  53. getHost(),
  54. "host url of Porter instance",
  55. )
  56. projectID = *connectCmd.PersistentFlags().UintP(
  57. "project-id",
  58. "p",
  59. getProjectID(),
  60. "project id to use",
  61. )
  62. connectKubeconfigCmd.PersistentFlags().StringVarP(
  63. &kubeconfigPath,
  64. "kubeconfig",
  65. "k",
  66. "",
  67. "path to kubeconfig",
  68. )
  69. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  70. "context",
  71. nil,
  72. "the context to connect (defaults to the current context)",
  73. )
  74. connectCmd.AddCommand(connectECRCmd)
  75. connectCmd.AddCommand(connectGCRCmd)
  76. }
  77. func runConnectKubeconfig(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  78. isLocal := false
  79. if getDriver() == "local" {
  80. isLocal = true
  81. }
  82. id, err := connect.Kubeconfig(
  83. client,
  84. kubeconfigPath,
  85. *contexts,
  86. getProjectID(),
  87. isLocal,
  88. )
  89. if err != nil {
  90. return err
  91. }
  92. return setCluster(id)
  93. }
  94. func runConnectECR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  95. regID, err := connect.ECR(
  96. client,
  97. getProjectID(),
  98. )
  99. if err != nil {
  100. return err
  101. }
  102. return setRegistry(regID)
  103. }
  104. func runConnectGCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  105. regID, err := connect.GCR(
  106. client,
  107. getProjectID(),
  108. )
  109. if err != nil {
  110. return err
  111. }
  112. return setRegistry(regID)
  113. }