Skip to content

Commit

Permalink
feat(cli): add support for custom local port in glasskube open (gla…
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshal101 authored Apr 23, 2024
1 parent 03d4fd9 commit b6d98ca
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion cmd/glasskube/cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion pkg/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -89,6 +96,7 @@ func (o *opener) Open(ctx context.Context, packageName string, entrypointName st
}
}
result.Completion = future.All(futures...)

return &result, nil
}

Expand Down

0 comments on commit b6d98ca

Please sign in to comment.