From 87349aed2c84e73bc6021727a2503db9a1daab43 Mon Sep 17 00:00:00 2001 From: Mark van Holsteijn Date: Thu, 9 Dec 2021 20:34:31 +0100 Subject: [PATCH] nope. no polymorphism in golang --- cmd/gkeclient.go | 13 ++++--------- cmd/gkeserver.go | 19 ++++++------------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/cmd/gkeclient.go b/cmd/gkeclient.go index f04caea..236b108 100644 --- a/cmd/gkeclient.go +++ b/cmd/gkeclient.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "log" "github.com/binxio/simple-iap-proxy/flags" @@ -17,7 +16,6 @@ func newGKEClientCmd() *cobra.Command { Short: "starts a client side proxy, forwarding requests to the GKE cluster via the IAP", Long: `The client will start a real HTTP/S proxy and forward any requests for, ip address of GKE cluster master endpoints, to the IAP proxy.`, - RunE: runProxy, }, }, } @@ -33,13 +31,10 @@ ip address of GKE cluster master endpoints, to the IAP proxy.`, c.MarkFlagRequired("service-account") c.MarkFlagRequired("target-url") c.Flags().SortFlags = false - return &c.Command -} -func runProxy(c *cobra.Command, _ []string) error { - p, ok := interface{}(c).(gkeclient.Proxy) - if !ok { - return fmt.Errorf("command is not a proxy command") + c.RunE = func(cmd *cobra.Command, args []string) error { + return c.Run() } - return p.Run() + + return &c.Command } diff --git a/cmd/gkeserver.go b/cmd/gkeserver.go index c1e724c..641ef28 100644 --- a/cmd/gkeserver.go +++ b/cmd/gkeserver.go @@ -1,15 +1,13 @@ package cmd import ( - "fmt" - "github.com/binxio/simple-iap-proxy/flags" "github.com/binxio/simple-iap-proxy/gkeserver" "github.com/spf13/cobra" ) func newGKEServerCmd() *cobra.Command { - gkeServerCmd := gkeserver.ReverseProxy{ + c := gkeserver.ReverseProxy{ RootCommand: flags.RootCommand{ Command: cobra.Command{ Use: "gke-server", @@ -19,18 +17,13 @@ reads the Host header of the http requests and if it matches the ip address of GKE cluster master endpoint, forwards the request to it. `, - RunE: runReverseProxy, }, }, } - - return &gkeServerCmd.Command -} - -func runReverseProxy(c *cobra.Command, _ []string) error { - s, ok := interface{}(c).(gkeserver.ReverseProxy) - if !ok { - return fmt.Errorf("command is not a reverse proxy command") + c.AddPersistentFlags() + c.RunE = func(cmd *cobra.Command, args []string) error { + return c.Run() } - return s.Run() + + return &c.Command }