From b6d98ca72ed87e6331b4be056e80912165aab92a Mon Sep 17 00:00:00 2001 From: Hanshal Mehta <122217807+hanshal101@users.noreply.github.com> Date: Tue, 23 Apr 2024 18:02:06 +0530 Subject: [PATCH] feat(cli): add support for custom local port in `glasskube open` (#543) --- cmd/glasskube/cmd/open.go | 7 ++++++- internal/web/server.go | 2 +- pkg/open/open.go | 10 +++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cmd/glasskube/cmd/open.go b/cmd/glasskube/cmd/open.go index da1a57c78..8af412ea7 100644 --- a/cmd/glasskube/cmd/open.go +++ b/cmd/glasskube/cmd/open.go @@ -11,6 +11,10 @@ import ( "github.com/spf13/cobra" ) +var ( + port int32 +) + var openCmd = &cobra.Command{ Use: "open [package-name] [entrypoint]", Short: "Open the Web UI of a package", @@ -25,7 +29,7 @@ If the package manifest has more than one entrypoint, specify the name of the en entrypointName = args[1] } - result, err := open.NewOpener().Open(cmd.Context(), pkgName, entrypointName) + result, err := open.NewOpener().Open(cmd.Context(), pkgName, entrypointName, port) if err != nil { fmt.Fprintf(os.Stderr, "❌ Could not open package %v: %v\n", pkgName, err) cliutils.ExitWithError() @@ -70,5 +74,6 @@ If the package manifest has more than one entrypoint, specify the name of the en } func init() { + openCmd.Flags().Int32Var(&port, "port", 0, "Custom port for opening the package") RootCmd.AddCommand(openCmd) } diff --git a/internal/web/server.go b/internal/web/server.go index 65fc34d35..fe9dfea4b 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -346,7 +346,7 @@ func (s *server) open(w http.ResponseWriter, r *http.Request) { return } - result, err := open.NewOpener().Open(r.Context(), pkgName, "") + result, err := open.NewOpener().Open(r.Context(), pkgName, "", 0) if err != nil { s.respondAlertAndLog(w, err, "Could not open "+pkgName, "danger") } else { diff --git a/pkg/open/open.go b/pkg/open/open.go index d34d72954..81193b0f5 100644 --- a/pkg/open/open.go +++ b/pkg/open/open.go @@ -35,7 +35,7 @@ func NewOpener() *opener { return &opener{} } -func (o *opener) Open(ctx context.Context, packageName string, entrypointName string) (*OpenResult, error) { +func (o *opener) Open(ctx context.Context, packageName string, entrypointName string, port int32) (*OpenResult, error) { if err := o.initFromContext(ctx); err != nil { return nil, err } @@ -49,6 +49,10 @@ func (o *opener) Open(ctx context.Context, packageName string, entrypointName st return nil, fmt.Errorf("package has no entrypoint") } + if port != 0 && len(manifest.Entrypoints) > 1 && entrypointName == "" { + return nil, fmt.Errorf("package has more than one entrypoint: %w", err) + } + if entrypointName != "" { exists := false for _, entrypoint := range manifest.Entrypoints { @@ -67,6 +71,9 @@ func (o *opener) Open(ctx context.Context, packageName string, entrypointName st for _, entrypoint := range manifest.Entrypoints { if entrypointName == "" || entrypoint.Name == entrypointName { e := entrypoint + if port != 0 { + e.LocalPort = port + } readyCh := make(chan struct{}) stopCh := make(chan struct{}) o.readyCh = append(o.readyCh, readyCh) @@ -89,6 +96,7 @@ func (o *opener) Open(ctx context.Context, packageName string, entrypointName st } } result.Completion = future.All(futures...) + return &result, nil }