portforward

package
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NetworkPortForwardCmd = &cobra.Command{
	Use:   "port-forward",
	Short: "Manage network port forwarding rules",
}
View Source
var NetworkPortForwardCreateCmd = &cobra.Command{
	Use:   "create",
	Short: "Create a port forwarding rule for a network",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("networkId"),
			cli.IsUlid("networkId"),
			cli.Required("protocol"),
			cli.OneOf("protocol", "TCP", "UDP"),
			cli.Required("publicPort"),
			cli.Required("privatePort"),
			cli.Required("privateIp"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneId := cli.ZoneIDFromContext(cmd.Context())

		if err := cli.LoadFromCobraFlags(cmd, &portForwardCreateOpts); err != nil {
			return err
		}

		request := map[string]interface{}{
			"network_id":   portForwardCreateOpts.NetworkID,
			"protocol":     portForwardCreateOpts.Protocol,
			"public_port":  portForwardCreateOpts.PublicPort,
			"private_port": portForwardCreateOpts.PrivatePort,
			"private_ip":   portForwardCreateOpts.PrivateIP,
		}

		httpClient := http.NewClient(token)
		resp, err := httpClient.CreatePortForward(zoneId, request)
		if err != nil {
			slog.Error("failed to create port forwarding rule", "error", err)
			return fmt.Errorf("failed to create port forwarding rule: %w", err)
		}

		if resp.Data.Success {
			fmt.Println("Port forwarding rule created successfully.")
		} else {
			fmt.Println("Failed to create port forwarding rule.")
		}
		return nil
	},
}
View Source
var NetworkPortForwardDeleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "Delete a port forwarding rule",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("id"),
			cli.IsUlid("id"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneId := cli.ZoneIDFromContext(cmd.Context())

		if err := cli.LoadFromCobraFlags(cmd, &portForwardDeleteOpts); err != nil {
			return err
		}

		httpClient := http.NewClient(token)
		resp, err := httpClient.DeletePortForward(zoneId, portForwardDeleteOpts.ID)
		if err != nil {
			slog.Error("failed to delete port forwarding rule", "error", err)
			return fmt.Errorf("failed to delete port forwarding rule: %w", err)
		}

		if resp.Data.Success {
			fmt.Println("Port forwarding rule deleted successfully.")
		} else {
			fmt.Println("Failed to delete port forwarding rule.")
		}
		return nil
	},
}
View Source
var NetworkPortForwardListCmd = &cobra.Command{
	Use:   "list",
	Short: "List port forwarding rules for a network",
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if err := cli.Preflight(true)(cmd, args); err != nil {
			return err
		}
		return cli.Validate(cmd,
			cli.Required("networkId"),
			cli.IsUlid("networkId"),
		)
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		token := cli.TokenFromContext(cmd.Context())
		zoneId := cli.ZoneIDFromContext(cmd.Context())

		if err := cli.LoadFromCobraFlags(cmd, &portForwardListOpts); err != nil {
			return err
		}

		httpClient := http.NewClient(token)
		resp, err := httpClient.ListPortForwards(zoneId, portForwardListOpts.NetworkID)
		if err != nil {
			slog.Error("failed to list port forwarding rules", "error", err)
			return fmt.Errorf("failed to list port forwarding rules: %w", err)
		}

		if len(resp.Data) == 0 {
			fmt.Println("No port forwarding rules found.")
			return nil
		}

		presenter.RenderPortForwardList(resp.Data)
		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL