2
0

connect.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 connectDockerhubCmd = &cobra.Command{
  38. Use: "dockerhub",
  39. Short: "Adds a Docker Hub registry integration to a project",
  40. Run: func(cmd *cobra.Command, args []string) {
  41. err := checkLoginAndRun(args, runConnectDockerhub)
  42. if err != nil {
  43. os.Exit(1)
  44. }
  45. },
  46. }
  47. var connectRegistryCmd = &cobra.Command{
  48. Use: "registry",
  49. Short: "Adds a custom image registry to a project",
  50. Run: func(cmd *cobra.Command, args []string) {
  51. err := checkLoginAndRun(args, runConnectRegistry)
  52. if err != nil {
  53. os.Exit(1)
  54. }
  55. },
  56. }
  57. var connectActionsCmd = &cobra.Command{
  58. Use: "actions",
  59. Short: "Adds Github Actions to a project",
  60. Run: func(cmd *cobra.Command, args []string) {
  61. err := checkLoginAndRun(args, runConnectActions)
  62. if err != nil {
  63. os.Exit(1)
  64. }
  65. },
  66. }
  67. var connectGCRCmd = &cobra.Command{
  68. Use: "gcr",
  69. Short: "Adds a GCR instance to a project",
  70. Run: func(cmd *cobra.Command, args []string) {
  71. err := checkLoginAndRun(args, runConnectGCR)
  72. if err != nil {
  73. os.Exit(1)
  74. }
  75. },
  76. }
  77. var connectDOCRCmd = &cobra.Command{
  78. Use: "docr",
  79. Short: "Adds a DOCR instance to a project",
  80. Run: func(cmd *cobra.Command, args []string) {
  81. err := checkLoginAndRun(args, runConnectDOCR)
  82. if err != nil {
  83. os.Exit(1)
  84. }
  85. },
  86. }
  87. var connectHRCmd = &cobra.Command{
  88. Use: "helmrepo",
  89. Aliases: []string{"helm", "helmrepos"},
  90. Short: "Adds a Helm repository to a project",
  91. Run: func(cmd *cobra.Command, args []string) {
  92. err := checkLoginAndRun(args, runConnectHelmRepoBasic)
  93. if err != nil {
  94. os.Exit(1)
  95. }
  96. },
  97. }
  98. func init() {
  99. rootCmd.AddCommand(connectCmd)
  100. connectCmd.AddCommand(connectKubeconfigCmd)
  101. connectKubeconfigCmd.PersistentFlags().StringVarP(
  102. &kubeconfigPath,
  103. "kubeconfig",
  104. "k",
  105. "",
  106. "path to kubeconfig",
  107. )
  108. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  109. "context",
  110. nil,
  111. "the context to connect (defaults to the current context)",
  112. )
  113. connectCmd.AddCommand(connectActionsCmd)
  114. connectCmd.AddCommand(connectECRCmd)
  115. connectCmd.AddCommand(connectRegistryCmd)
  116. connectCmd.AddCommand(connectDockerhubCmd)
  117. connectCmd.AddCommand(connectGCRCmd)
  118. connectCmd.AddCommand(connectDOCRCmd)
  119. connectCmd.AddCommand(connectHRCmd)
  120. }
  121. func runConnectKubeconfig(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  122. isLocal := false
  123. if config.Driver == "local" {
  124. isLocal = true
  125. }
  126. id, err := connect.Kubeconfig(
  127. client,
  128. kubeconfigPath,
  129. *contexts,
  130. config.Project,
  131. isLocal,
  132. )
  133. if err != nil {
  134. return err
  135. }
  136. return config.SetCluster(id)
  137. }
  138. func runConnectECR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  139. regID, err := connect.ECR(
  140. client,
  141. config.Project,
  142. )
  143. if err != nil {
  144. return err
  145. }
  146. return config.SetRegistry(regID)
  147. }
  148. func runConnectGCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  149. regID, err := connect.GCR(
  150. client,
  151. config.Project,
  152. )
  153. if err != nil {
  154. return err
  155. }
  156. return config.SetRegistry(regID)
  157. }
  158. func runConnectDOCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  159. regID, err := connect.DOCR(
  160. client,
  161. config.Project,
  162. )
  163. if err != nil {
  164. return err
  165. }
  166. return config.SetRegistry(regID)
  167. }
  168. func runConnectDockerhub(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  169. regID, err := connect.Dockerhub(
  170. client,
  171. config.Project,
  172. )
  173. if err != nil {
  174. return err
  175. }
  176. return config.SetRegistry(regID)
  177. }
  178. func runConnectRegistry(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  179. regID, err := connect.Registry(
  180. client,
  181. config.Project,
  182. )
  183. if err != nil {
  184. return err
  185. }
  186. return config.SetRegistry(regID)
  187. }
  188. func runConnectHelmRepoBasic(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  189. hrID, err := connect.Helm(
  190. client,
  191. config.Project,
  192. )
  193. if err != nil {
  194. return err
  195. }
  196. return config.SetHelmRepo(hrID)
  197. }
  198. func runConnectActions(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  199. return connect.Actions(
  200. client,
  201. config.Project,
  202. )
  203. }