Skip to content

Commit

Permalink
cmd/forwarder: move PAC supportedFunctions to root
Browse files Browse the repository at this point in the history
- Rename wrap.go to decorators.go
- Add withPACSupportedFunctions
- Decorate pac-[eval,server] and proxy
  • Loading branch information
mmatczuk committed Oct 27, 2022
1 parent 87e98cb commit f29588d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 31 deletions.
16 changes: 16 additions & 0 deletions cmd/forwarder/wrap.go → cmd/forwarder/decorators.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,25 @@ import (
"regexp"
"strings"

"github.com/saucelabs/forwarder/pac"
"github.com/spf13/cobra"
)

func withPACSupportedFunctions(cmd *cobra.Command) *cobra.Command {
cmd.Example += "\n" + pacSupportedFunctions()
return cmd
}

func pacSupportedFunctions() string {
var sb strings.Builder
sb.WriteString("Supported PAC util functions:")
for _, fn := range pac.SupportedFunctions() {
sb.WriteString("\n ")
sb.WriteString(fn)
}
return sb.String()
}

func wrapLongAt(cmd *cobra.Command, width int) {
cmd.Long = wrapTextAt(cmd.Long, width)
}
Expand Down
13 changes: 1 addition & 12 deletions cmd/forwarder/paceval/paceval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"net/url"
"os"
"strings"

"github.com/saucelabs/forwarder"
"github.com/saucelabs/forwarder/bind"
Expand Down Expand Up @@ -80,7 +79,7 @@ func Command() (cmd *cobra.Command) {
Short: "Evaluate a PAC file for given URLs",
Long: long,
RunE: c.RunE,
Example: example + "\n" + supportedFunctions(),
Example: example,
}
}

Expand All @@ -104,13 +103,3 @@ const example = ` # Evaluate a PAC file for a URL
# Evaluate a PAC file for multiple URLs using a PAC file from a URL
forwarder pac-eval --pac https://example.com/pac.js https://www.google.com https://www.facebook.com
`

func supportedFunctions() string {
var sb strings.Builder
sb.WriteString("Supported PAC util functions:")
for _, fn := range pac.SupportedFunctions() {
sb.WriteString("\n ")
sb.WriteString(fn)
}
return sb.String()
}
21 changes: 5 additions & 16 deletions cmd/forwarder/pacserver/pacserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"net/url"
"os/signal"
"strings"
"syscall"

"github.com/saucelabs/forwarder"
Expand Down Expand Up @@ -80,7 +79,7 @@ func Command() (cmd *cobra.Command) {
Short: "Start HTTP(S) server that serves a PAC file",
Long: long,
RunE: c.RunE,
Example: example + "\n" + supportedFunctions(),
Example: example,
}
}

Expand All @@ -96,24 +95,14 @@ If you start an HTTPS server and you don't provide a certificate, the server wil
`

const example = ` # Start a HTTP server serving a PAC file
pac-server --pac pac.js --protocol http --address localhost:8080
forwarder pac-server --pac pac.js --protocol http --address localhost:8080
# Start a HTTPS server serving a PAC file
pac-server --pac pac.js --protocol https --address localhost:80443
forwarder pac-server --pac pac.js --protocol https --address localhost:80443
# Start a HTTPS server serving a PAC file with custom certificate
pac-server --pac pac.js --protocol https --address localhost:80443 --cert-file cert.pem --key-file key.pem
forwarder pac-server --pac pac.js --protocol https --address localhost:80443 --cert-file cert.pem --key-file key.pem
# Start a HTTPS server serving a PAC file with basic authentication
pac-server --pac pac.js --protocol https --address localhost:80443 --basic-auth user:pass
forwarder pac-server --pac pac.js --protocol https --address localhost:80443 --basic-auth user:pass
`

func supportedFunctions() string {
var sb strings.Builder
sb.WriteString("Supported PAC util functions:")
for _, fn := range pac.SupportedFunctions() {
sb.WriteString("\n ")
sb.WriteString(fn)
}
return sb.String()
}
6 changes: 3 additions & 3 deletions cmd/forwarder/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func rootCommand() *cobra.Command {
}

rootCmd.AddCommand(
paceval.Command(),
pacserver.Command(),
proxy.Command(),
withPACSupportedFunctions(paceval.Command()),
withPACSupportedFunctions(pacserver.Command()),
withPACSupportedFunctions(proxy.Command()),
version.Command(),
)
for _, cmd := range rootCmd.Commands() {
Expand Down

0 comments on commit f29588d

Please sign in to comment.