| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- package commands
- import (
- "context"
- "os"
- api "github.com/porter-dev/porter/api/client"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/cli/cmd/config"
- "github.com/porter-dev/porter/cli/cmd/connect"
- "github.com/spf13/cobra"
- )
- var (
- kubeconfigPath string
- print *bool
- contexts *[]string
- )
- func registerCommand_Connect(cliConf config.CLIConfig) *cobra.Command {
- connectCmd := &cobra.Command{
- Use: "connect",
- Short: "Commands that connect to external clusters and providers",
- }
- connectKubeconfigCmd := &cobra.Command{
- Use: "kubeconfig",
- Short: "Uses the local kubeconfig to add a cluster",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectKubeconfig)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- connectECRCmd := &cobra.Command{
- Use: "ecr",
- Short: "Adds an ECR instance to a project",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectECR)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- connectDockerhubCmd := &cobra.Command{
- Use: "dockerhub",
- Short: "Adds a Docker Hub registry integration to a project",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectDockerhub)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- connectRegistryCmd := &cobra.Command{
- Use: "registry",
- Short: "Adds a custom image registry to a project",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectRegistry)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- connectHelmRepoCmd := &cobra.Command{
- Use: "helm",
- Short: "Adds a custom Helm registry to a project",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectHelmRepo)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- connectGCRCmd := &cobra.Command{
- Use: "gcr",
- Short: "Adds a GCR instance to a project",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectGCR)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- connectGARCmd := &cobra.Command{
- Use: "gar",
- Short: "Adds a GAR instance to a project",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectGAR)
- if err != nil {
- os.Exit(1)
- }
- cmd.Context()
- },
- }
- connectDOCRCmd := &cobra.Command{
- Use: "docr",
- Short: "Adds a DOCR instance to a project",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRunWithConfig(cmd, cliConf, args, runConnectDOCR)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- connectCmd.AddCommand(connectKubeconfigCmd)
- connectKubeconfigCmd.PersistentFlags().StringVarP(
- &kubeconfigPath,
- "kubeconfig",
- "k",
- "",
- "path to kubeconfig",
- )
- contexts = connectKubeconfigCmd.PersistentFlags().StringArray(
- "context",
- nil,
- "the context to connect (defaults to the current context)",
- )
- connectCmd.AddCommand(connectECRCmd)
- connectCmd.AddCommand(connectRegistryCmd)
- connectCmd.AddCommand(connectDockerhubCmd)
- connectCmd.AddCommand(connectGCRCmd)
- connectCmd.AddCommand(connectGARCmd)
- connectCmd.AddCommand(connectDOCRCmd)
- connectCmd.AddCommand(connectHelmRepoCmd)
- return connectCmd
- }
- func runConnectKubeconfig(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- isLocal := false
- if cliConf.Driver == "local" {
- isLocal = true
- }
- id, err := connect.Kubeconfig(
- ctx,
- client,
- kubeconfigPath,
- *contexts,
- cliConf.Project,
- isLocal,
- )
- if err != nil {
- return err
- }
- return cliConf.SetCluster(id)
- }
- func runConnectECR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- regID, err := connect.ECR(
- ctx,
- client,
- cliConf.Project,
- )
- if err != nil {
- return err
- }
- return cliConf.SetRegistry(regID)
- }
- func runConnectGCR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- regID, err := connect.GCR(
- ctx,
- client,
- cliConf.Project,
- )
- if err != nil {
- return err
- }
- return cliConf.SetRegistry(regID)
- }
- func runConnectGAR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- regID, err := connect.GAR(
- ctx,
- client,
- cliConf.Project,
- )
- if err != nil {
- return err
- }
- return cliConf.SetRegistry(regID)
- }
- func runConnectDOCR(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- regID, err := connect.DOCR(
- ctx,
- client,
- cliConf.Project,
- )
- if err != nil {
- return err
- }
- return cliConf.SetRegistry(regID)
- }
- func runConnectDockerhub(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- regID, err := connect.Dockerhub(
- ctx,
- client,
- cliConf.Project,
- )
- if err != nil {
- return err
- }
- return cliConf.SetRegistry(regID)
- }
- func runConnectRegistry(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- regID, err := connect.Registry(
- ctx,
- client,
- cliConf.Project,
- )
- if err != nil {
- return err
- }
- return cliConf.SetRegistry(regID)
- }
- func runConnectHelmRepo(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConf config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, _ []string) error {
- hrID, err := connect.HelmRepo(
- ctx,
- client,
- cliConf.Project,
- )
- if err != nil {
- return err
- }
- return cliConf.SetHelmRepo(hrID)
- }
|