connect.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. connectCmd.PersistentFlags().StringVar(
  102. &host,
  103. "host",
  104. getHost(),
  105. "host url of Porter instance",
  106. )
  107. projectID = *connectCmd.PersistentFlags().UintP(
  108. "project-id",
  109. "p",
  110. getProjectID(),
  111. "project id to use",
  112. )
  113. connectKubeconfigCmd.PersistentFlags().StringVarP(
  114. &kubeconfigPath,
  115. "kubeconfig",
  116. "k",
  117. "",
  118. "path to kubeconfig",
  119. )
  120. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  121. "context",
  122. nil,
  123. "the context to connect (defaults to the current context)",
  124. )
  125. connectCmd.AddCommand(connectActionsCmd)
  126. connectCmd.AddCommand(connectECRCmd)
  127. connectCmd.AddCommand(connectRegistryCmd)
  128. connectCmd.AddCommand(connectDockerhubCmd)
  129. connectCmd.AddCommand(connectGCRCmd)
  130. connectCmd.AddCommand(connectDOCRCmd)
  131. connectCmd.AddCommand(connectHRCmd)
  132. }
  133. func runConnectKubeconfig(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  134. isLocal := false
  135. if getDriver() == "local" {
  136. isLocal = true
  137. }
  138. id, err := connect.Kubeconfig(
  139. client,
  140. kubeconfigPath,
  141. *contexts,
  142. getProjectID(),
  143. isLocal,
  144. )
  145. if err != nil {
  146. return err
  147. }
  148. return setCluster(id)
  149. }
  150. func runConnectECR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  151. regID, err := connect.ECR(
  152. client,
  153. getProjectID(),
  154. )
  155. if err != nil {
  156. return err
  157. }
  158. return setRegistry(regID)
  159. }
  160. func runConnectGCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  161. regID, err := connect.GCR(
  162. client,
  163. getProjectID(),
  164. )
  165. if err != nil {
  166. return err
  167. }
  168. return setRegistry(regID)
  169. }
  170. func runConnectDOCR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  171. regID, err := connect.DOCR(
  172. client,
  173. getProjectID(),
  174. )
  175. if err != nil {
  176. return err
  177. }
  178. return setRegistry(regID)
  179. }
  180. func runConnectDockerhub(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  181. regID, err := connect.Dockerhub(
  182. client,
  183. getProjectID(),
  184. )
  185. if err != nil {
  186. return err
  187. }
  188. return setRegistry(regID)
  189. }
  190. func runConnectRegistry(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  191. regID, err := connect.Registry(
  192. client,
  193. getProjectID(),
  194. )
  195. if err != nil {
  196. return err
  197. }
  198. return setRegistry(regID)
  199. }
  200. func runConnectHelmRepoBasic(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  201. hrID, err := connect.Helm(
  202. client,
  203. getProjectID(),
  204. )
  205. if err != nil {
  206. return err
  207. }
  208. return setHelmRepo(hrID)
  209. }
  210. func runConnectActions(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  211. return connect.Actions(
  212. client,
  213. getProjectID(),
  214. )
  215. }