-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b608ace
commit 25c8d70
Showing
5 changed files
with
280 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package webspace | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/netsoc/cli/pkg/config" | ||
"github.com/netsoc/cli/pkg/util" | ||
webspaced "github.com/netsoc/webspaced/client" | ||
) | ||
|
||
type portsOptions struct { | ||
Config func() (*config.Config, error) | ||
WebspacedClient func() (*webspaced.APIClient, error) | ||
|
||
OutputFormat string | ||
User string | ||
} | ||
|
||
// NewCmdPorts creates a new webspace ports command | ||
func NewCmdPorts(f *util.CmdFactory) *cobra.Command { | ||
opts := portsOptions{ | ||
Config: f.Config, | ||
WebspacedClient: f.WebspacedClient, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "ports", | ||
Short: "Configure webspace port forwards", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return portsRun(opts) | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&opts.OutputFormat, "output", "o", "text", "output format `text|yaml|json|template=<Go template>`") | ||
util.AddOptUser(cmd, &opts.User) | ||
|
||
cmd.AddCommand(NewCmdPortsAdd(f), NewCmdPortsRemove(f)) | ||
|
||
return cmd | ||
} | ||
|
||
func printPorts(ports map[string]int32, outputType string) error { | ||
if strings.HasPrefix(outputType, "template=") { | ||
tpl, err := template.New("anonymous").Parse(strings.TrimPrefix(outputType, "template=")) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse template: %w", err) | ||
} | ||
|
||
if err := tpl.Execute(os.Stdout, ports); err != nil { | ||
return fmt.Errorf("failed to execute template: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
switch outputType { | ||
case "json": | ||
if err := json.NewEncoder(os.Stdout).Encode(ports); err != nil { | ||
return fmt.Errorf("failed to encode JSON: %w", err) | ||
} | ||
case "yaml": | ||
if err := yaml.NewEncoder(os.Stdout).Encode(ports); err != nil { | ||
return fmt.Errorf("failed to encode YAML: %w", err) | ||
} | ||
case "text": | ||
fmt.Println("Webspace port forwards:") | ||
for i, e := range ports { | ||
fmt.Printf(" - %v -> %v\n", i, e) | ||
} | ||
default: | ||
return fmt.Errorf(`unknown output format "%v"`, outputType) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func portsRun(opts portsOptions) error { | ||
c, err := opts.Config() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.Token == "" { | ||
return errors.New("not logged in") | ||
} | ||
|
||
client, err := opts.WebspacedClient() | ||
if err != nil { | ||
return err | ||
} | ||
ctx := context.WithValue(context.Background(), webspaced.ContextAccessToken, c.Token) | ||
|
||
ports, _, err := client.PortsApi.GetPorts(ctx, opts.User) | ||
if err != nil { | ||
return util.APIError(err) | ||
} | ||
|
||
return printPorts(ports, opts.OutputFormat) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package webspace | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/netsoc/cli/pkg/config" | ||
"github.com/netsoc/cli/pkg/util" | ||
webspaced "github.com/netsoc/webspaced/client" | ||
) | ||
|
||
type portsAddOptions struct { | ||
Config func() (*config.Config, error) | ||
WebspacedClient func() (*webspaced.APIClient, error) | ||
|
||
User string | ||
ExternalPort uint16 | ||
InternalPort uint16 | ||
} | ||
|
||
// NewCmdPortsAdd creates a new webspace ports add command | ||
func NewCmdPortsAdd(f *util.CmdFactory) *cobra.Command { | ||
opts := portsAddOptions{ | ||
Config: f.Config, | ||
WebspacedClient: f.WebspacedClient, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "add <internal port>", | ||
Short: "Add port forward", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
p, err := strconv.ParseUint(args[0], 10, 16) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse internal port: %w", err) | ||
} | ||
opts.InternalPort = uint16(p) | ||
|
||
return portsAddRun(opts) | ||
}, | ||
} | ||
|
||
util.AddOptUser(cmd, &opts.User) | ||
cmd.Flags().Uint16VarP(&opts.ExternalPort, "external-port", "p", 0, "external port (0 means random)") | ||
|
||
return cmd | ||
} | ||
|
||
func portsAddRun(opts portsAddOptions) error { | ||
c, err := opts.Config() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.Token == "" { | ||
return errors.New("not logged in") | ||
} | ||
|
||
client, err := opts.WebspacedClient() | ||
if err != nil { | ||
return err | ||
} | ||
ctx := context.WithValue(context.Background(), webspaced.ContextAccessToken, c.Token) | ||
|
||
if opts.ExternalPort == 0 { | ||
i, _, err := client.PortsApi.AddRandomPort(ctx, opts.User, int32(opts.InternalPort)) | ||
if err != nil { | ||
return util.APIError(err) | ||
} | ||
|
||
opts.ExternalPort = uint16(i.EPort) | ||
} else { | ||
if _, err := client.PortsApi.AddPort(ctx, opts.User, int32(opts.ExternalPort), int32(opts.InternalPort)); err != nil { | ||
return util.APIError(err) | ||
} | ||
} | ||
|
||
if util.IsInteractive() { | ||
fmt.Printf("Port %v in webspace is now accessible externally via port %v\n", opts.InternalPort, opts.ExternalPort) | ||
} else { | ||
fmt.Println(opts.ExternalPort) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package webspace | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/netsoc/cli/pkg/config" | ||
"github.com/netsoc/cli/pkg/util" | ||
webspaced "github.com/netsoc/webspaced/client" | ||
) | ||
|
||
type portsRemoveOptions struct { | ||
Config func() (*config.Config, error) | ||
WebspacedClient func() (*webspaced.APIClient, error) | ||
|
||
User string | ||
Port uint16 | ||
} | ||
|
||
// NewCmdPortsRemove creates a new webspace ports remove command | ||
func NewCmdPortsRemove(f *util.CmdFactory) *cobra.Command { | ||
opts := portsRemoveOptions{ | ||
Config: f.Config, | ||
WebspacedClient: f.WebspacedClient, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "remove <external port>", | ||
Aliases: []string{"delete"}, | ||
Short: "Remove port forward", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
p, err := strconv.ParseUint(args[0], 10, 16) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse port: %w", err) | ||
} | ||
opts.Port = uint16(p) | ||
|
||
return portsRemoveRun(opts) | ||
}, | ||
} | ||
|
||
util.AddOptUser(cmd, &opts.User) | ||
|
||
return cmd | ||
} | ||
|
||
func portsRemoveRun(opts portsRemoveOptions) error { | ||
c, err := opts.Config() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.Token == "" { | ||
return errors.New("not logged in") | ||
} | ||
|
||
client, err := opts.WebspacedClient() | ||
if err != nil { | ||
return err | ||
} | ||
ctx := context.WithValue(context.Background(), webspaced.ContextAccessToken, c.Token) | ||
|
||
if _, err := client.PortsApi.RemovePort(ctx, opts.User, int32(opts.Port)); err != nil { | ||
return util.APIError(err) | ||
} | ||
|
||
log.Print("Removed successfully") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters