Skip to content

Commit

Permalink
feat(connect): update default behavior to be cli-only (#3487)
Browse files Browse the repository at this point in the history
Signed-off-by: Brandt Keller <brandt.keller@defenseunicorns.com>
Signed-off-by: Austin Abro <AustinAbro321@gmail.com>
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Austin Abro <37223396+AustinAbro321@users.noreply.github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 12, 2025
1 parent 724c7a2 commit 7236f65
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion site/src/content/docs/commands/zarf_connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ zarf connect { REGISTRY | GIT | connect-name } [flags]
### Options

```
--cli-only Disable browser auto-open
-h, --help help for connect
--local-port int (Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000.
--name string Specify the resource name. E.g. name=unicorns or name=unicorn-pod-7448499f4d-b5bk6. Ignored if connect-name is supplied.
--namespace string Specify the namespace. E.g. namespace=default. Ignored if connect-name is supplied. (default "zarf")
--open Enable browser auto-open
--remote-port int Specify the remote port of the resource to bind to. E.g. remote-port=8080. Ignored if connect-name is supplied.
--type string Specify the resource type. E.g. type=svc or type=pod. Ignored if connect-name is supplied. (default "svc")
```
Expand Down
15 changes: 8 additions & 7 deletions src/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
)

type connectOptions struct {
cliOnly bool
zt cluster.TunnelInfo
open bool
zt cluster.TunnelInfo
}

func newConnectCommand() *cobra.Command {
Expand All @@ -37,7 +37,7 @@ func newConnectCommand() *cobra.Command {
cmd.Flags().StringVar(&o.zt.ResourceType, "type", cluster.SvcResource, lang.CmdConnectFlagType)
cmd.Flags().IntVar(&o.zt.LocalPort, "local-port", 0, lang.CmdConnectFlagLocalPort)
cmd.Flags().IntVar(&o.zt.RemotePort, "remote-port", 0, lang.CmdConnectFlagRemotePort)
cmd.Flags().BoolVar(&o.cliOnly, "cli-only", false, lang.CmdConnectFlagCliOnly)
cmd.Flags().BoolVar(&o.open, "open", false, lang.CmdConnectFlagOpen)

// TODO(soltysh): consider splitting sub-commands into separate files
cmd.AddCommand(newConnectListCommand())
Expand All @@ -49,6 +49,7 @@ func (o *connectOptions) run(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
l := logger.From(ctx)
target := ""
// TODO: this leaves room for ignoring potential misuse
if len(args) > 0 {
target = args[0]
}
Expand Down Expand Up @@ -82,15 +83,15 @@ func (o *connectOptions) run(cmd *cobra.Command, args []string) error {

defer tunnel.Close()

if o.cliOnly {
spinner.Updatef(lang.CmdConnectEstablishedCLI, tunnel.FullURL())
l.Info("Tunnel established, waiting for user to interrupt (ctrl-c to end)", "url", tunnel.FullURL())
} else {
if o.open {
spinner.Updatef(lang.CmdConnectEstablishedWeb, tunnel.FullURL())
l.Info("Tunnel established, opening your default web browser (ctrl-c to end)", "url", tunnel.FullURL())
if err := exec.LaunchURL(tunnel.FullURL()); err != nil {
return err
}
} else {
spinner.Updatef(lang.CmdConnectEstablishedCLI, tunnel.FullURL())
l.Info("Tunnel established, waiting for user to interrupt (ctrl-c to end)", "url", tunnel.FullURL())
}

// Wait for the interrupt signal or an error.
Expand Down
2 changes: 1 addition & 1 deletion src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const (
CmdConnectFlagType = "Specify the resource type. E.g. type=svc or type=pod. Ignored if connect-name is supplied."
CmdConnectFlagLocalPort = "(Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000."
CmdConnectFlagRemotePort = "Specify the remote port of the resource to bind to. E.g. remote-port=8080. Ignored if connect-name is supplied."
CmdConnectFlagCliOnly = "Disable browser auto-open"
CmdConnectFlagOpen = "Enable browser auto-open"

CmdConnectPreparingTunnel = "Preparing a tunnel to connect to %s"
CmdConnectEstablishedCLI = "Tunnel established at %s, waiting for user to interrupt (ctrl-c to end)"
Expand Down
12 changes: 9 additions & 3 deletions src/test/e2e/09_component_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func TestComposabilityExample(t *testing.T) {
b, err := goyaml.Marshal(pkgLayout.Pkg.Components)
require.NoError(t, err)

absComposeExample, err := filepath.Abs(composeExample)
require.NoError(t, err)

rel, err := filepath.Rel(absComposeExample, tmpDir)
require.NoError(t, err)

expectedYaml := fmt.Sprintf(`- name: local-games-path
description: Example of a local composed package with a unique description for this component
required: true
Expand All @@ -55,8 +61,8 @@ func TestComposabilityExample(t *testing.T) {
- name: multi-games
namespace: dos-games
files:
- ../../../../../../..%s/oci/dirs/9ece174e362633b637e3c6b554b70f7d009d0a27107bee822336fdf2ce9a9def/manifests/multi-games-0.yaml
- ../../../../../../..%s/oci/dirs/9ece174e362633b637e3c6b554b70f7d009d0a27107bee822336fdf2ce9a9def/manifests/multi-games-1.yaml
- %s/oci/dirs/9ece174e362633b637e3c6b554b70f7d009d0a27107bee822336fdf2ce9a9def/manifests/multi-games-0.yaml
- %s/oci/dirs/9ece174e362633b637e3c6b554b70f7d009d0a27107bee822336fdf2ce9a9def/manifests/multi-games-1.yaml
images:
- ghcr.io/zarf-dev/doom-game:0.0.1
actions:
Expand All @@ -70,7 +76,7 @@ func TestComposabilityExample(t *testing.T) {
name: game
namespace: dos-games
condition: available
`, tmpDir, tmpDir)
`, rel, rel)
require.YAMLEq(t, expectedYaml, string(b))
}

Expand Down

0 comments on commit 7236f65

Please sign in to comment.