connect.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 connect to 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: "Connects 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. func init() {
  38. rootCmd.AddCommand(connectCmd)
  39. connectCmd.AddCommand(connectKubeconfigCmd)
  40. connectCmd.PersistentFlags().StringVar(
  41. &host,
  42. "host",
  43. getHost(),
  44. "host url of Porter instance",
  45. )
  46. projectID = *connectCmd.PersistentFlags().UintP(
  47. "project-id",
  48. "p",
  49. getProjectID(),
  50. "project id to use",
  51. )
  52. connectKubeconfigCmd.PersistentFlags().StringVarP(
  53. &kubeconfigPath,
  54. "kubeconfig",
  55. "k",
  56. "",
  57. "path to kubeconfig",
  58. )
  59. contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
  60. "contexts",
  61. nil,
  62. "the list of contexts to connect (defaults to the current context)",
  63. )
  64. connectCmd.AddCommand(connectECRCmd)
  65. }
  66. func runConnectKubeconfig(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  67. isLocal := false
  68. if getDriver() == "local" {
  69. isLocal = true
  70. }
  71. return connect.Kubeconfig(
  72. client,
  73. kubeconfigPath,
  74. *contexts,
  75. getProjectID(),
  76. isLocal,
  77. )
  78. }
  79. func runConnectECR(_ *api.AuthCheckResponse, client *api.Client, _ []string) error {
  80. return connect.ECR(
  81. client,
  82. getProjectID(),
  83. )
  84. }