connect.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 connectActionsCmd = &cobra.Command{
  38. Use: "actions",
  39. Short: "Adds Github Actions to a project",
  40. Run: func(cmd *cobra.Command, args []string) {
  41. err := checkLoginAndRun(args, runConnectActions)
  42. if err != nil {
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. var connectGCRCmd = &cobra.Command{
  48. Use: "gcr",
  49. Short: "Adds a GCR instance to a project",
  50. Run: func(cmd *cobra.Command, args []string) {
  51. err := checkLoginAndRun(args, runConnectGCR)
  52. if err != nil {
  53. os.Exit(1)
  54. }
  55. },
  56. }
  57. var connectDOCRCmd = &cobra.Command{
  58. Use: "docr",
  59. Short: "Adds a DOCR instance to a project",
  60. Run: func(cmd *cobra.Command, args []string) {
  61. err := checkLoginAndRun(args, runConnectDOCR)
  62. if err != nil {
  63. os.Exit(1)
  64. }
  65. },
  66. }
  67. var connectHRCmd = &cobra.Command{
  68. Use: "helmrepo",
  69. Aliases: []string{"helm", "helmrepos"},
  70. Short: "Adds a Helm repository to a project",
  71. Run: func(cmd *cobra.Command, args []string) {
  72. err := checkLoginAndRun(args, runConnectHelmRepoBasic)
  73. if err != nil {
  74. os.Exit(1)
  75. }
  76. },
  77. }
  78. func init() {
  79. rootCmd.AddCommand(connectCmd)
  80. connectCmd.AddCommand(connectKubeconfigCmd)
  81. connectCmd.PersistentFlags().StringVar(
  82. &host,
  83. "host",
  84. getHost(),
  85. "host url of Porter instance",
  86. )
  87. projectID = *connectCmd.PersistentFlags().UintP(
  88. "project-id",
  89. "p",
  90. getProjectID(),
  91. "project id to use",
  92. )
  93. connectKubeconfigCmd.PersistentFlags().StringVarP(
  94. &kubeconfigPath,
  95. "kubeconfig",
  96. "k",
  97. "",
  98. "path to kubeconfig",
  99. )
  100. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  101. "context",
  102. nil,
  103. "the context to connect (defaults to the current context)",
  104. )
  105. connectCmd.AddCommand(connectActionsCmd)
  106. connectCmd.AddCommand(connectECRCmd)
  107. connectCmd.AddCommand(connectGCRCmd)
  108. connectCmd.AddCommand(connectDOCRCmd)
  109. connectCmd.AddCommand(connectHRCmd)
  110. }
  111. func runConnectKubeconfig(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  112. isLocal := false
  113. if getDriver() == "local" {
  114. isLocal = true
  115. }
  116. id, err := connect.Kubeconfig(
  117. client,
  118. kubeconfigPath,
  119. *contexts,
  120. getProjectID(),
  121. isLocal,
  122. )
  123. if err != nil {
  124. return err
  125. }
  126. return setCluster(id)
  127. }
  128. func runConnectECR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  129. regID, err := connect.ECR(
  130. client,
  131. getProjectID(),
  132. )
  133. if err != nil {
  134. return err
  135. }
  136. return setRegistry(regID)
  137. }
  138. func runConnectGCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  139. regID, err := connect.GCR(
  140. client,
  141. getProjectID(),
  142. )
  143. if err != nil {
  144. return err
  145. }
  146. return setRegistry(regID)
  147. }
  148. func runConnectDOCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  149. regID, err := connect.DOCR(
  150. client,
  151. getProjectID(),
  152. )
  153. if err != nil {
  154. return err
  155. }
  156. return setRegistry(regID)
  157. }
  158. func runConnectHelmRepoBasic(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  159. hrID, err := connect.Helm(
  160. client,
  161. getProjectID(),
  162. )
  163. if err != nil {
  164. return err
  165. }
  166. return setHelmRepo(hrID)
  167. }
  168. func runConnectActions(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  169. return connect.Actions(
  170. client,
  171. getProjectID(),
  172. )
  173. }