Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
295 changes: 80 additions & 215 deletions internal/cli/app.go

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/ryanfowler/fetch/internal/printer"
"github.com/ryanfowler/fetch/internal/core"
)

type CLI struct {
Expand Down Expand Up @@ -219,17 +219,17 @@ func Parse(args []string) (*App, error) {
return &app, nil
}

func printHelp(cli *CLI, p *printer.Printer) {
func printHelp(cli *CLI, p *core.Printer) {
p.WriteString(cli.Description)
p.WriteString("\n\n")

p.Set(printer.Bold)
p.Set(printer.Underline)
p.Set(core.Bold)
p.Set(core.Underline)
p.WriteString("Usage")
p.Reset()
p.WriteString(": ")

p.Set(printer.Bold)
p.Set(core.Bold)
p.WriteString("fetch")
p.Reset()

Expand All @@ -247,8 +247,8 @@ func printHelp(cli *CLI, p *printer.Printer) {
if len(cli.Args) > 0 {
p.WriteString("\n")

p.Set(printer.Bold)
p.Set(printer.Underline)
p.Set(core.Bold)
p.Set(core.Underline)
p.WriteString("Arguments")
p.Reset()
p.WriteString(":\n")
Expand All @@ -265,8 +265,8 @@ func printHelp(cli *CLI, p *printer.Printer) {
if len(cli.Flags) > 0 {
p.WriteString("\n")

p.Set(printer.Bold)
p.Set(printer.Underline)
p.Set(core.Bold)
p.Set(core.Underline)
p.WriteString("Options")
p.Reset()
p.WriteString(":\n")
Expand All @@ -277,7 +277,7 @@ func printHelp(cli *CLI, p *printer.Printer) {
continue
}

p.Set(printer.Bold)
p.Set(core.Bold)
p.WriteString(" ")

if flag.Short == "" {
Expand Down
3 changes: 1 addition & 2 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"unicode/utf8"

"github.com/ryanfowler/fetch/internal/core"
"github.com/ryanfowler/fetch/internal/printer"
)

func TestCLI(t *testing.T) {
app, err := Parse(nil)
if err != nil {
t.Fatalf("unable to parse cli: %s", err.Error())
}
p := printer.NewHandle(core.ColorOff).Stdout()
p := core.NewHandle(core.ColorOff).Stdout()

// Verify that no line of the help command is over 80 characters.
app.PrintHelp(p)
Expand Down
16 changes: 15 additions & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -135,10 +136,23 @@ func (c *Client) NewRequest(ctx context.Context, cfg RequestConfig) (*http.Reque
cfg.Body = cfg.Multipart
}

// Create the initial HTTP request.
// If no scheme was provided, use various heuristics to choose between
// http and https.
if cfg.URL.Scheme == "" {
host := cfg.URL.Hostname()
if !strings.Contains(host, ".") || net.ParseIP(host) != nil {
cfg.URL.Scheme = "http"
} else {
cfg.URL.Scheme = "https"
}
}

// If no method was provided, default to GET.
if cfg.Method == "" {
cfg.Method = "GET"
}

// Create the initial HTTP request.
req, err := http.NewRequestWithContext(ctx, cfg.Method, cfg.URL.String(), cfg.Body)
if err != nil {
return nil, err
Expand Down
Loading
Loading