Skip to content

Commit 6888da6

Browse files
authored
Merge pull request #145 from ryanfowler/main-path
Move main.go to project root
2 parents b57b8a2 + b0f5413 commit 6888da6

File tree

9 files changed

+37
-44
lines changed

9 files changed

+37
-44
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ jobs:
8888
GOOS: ${{ matrix.goos }}
8989
GOARCH: ${{ matrix.goarch }}
9090
CGO_ENABLED: "0"
91-
run: go install -trimpath -ldflags="-s -w" ./cmd/fetch
91+
run: go install -trimpath -ldflags="-s -w"

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
GOOS: ${{ matrix.goos }}
6464
GOARCH: ${{ matrix.goarch }}
6565
CGO_ENABLED: "0"
66-
run: go build -trimpath -ldflags="-s -w" -o ${{ env.BIN_NAME }} ./cmd/fetch/main.go
66+
run: go build -trimpath -ldflags="-s -w" -o ${{ env.BIN_NAME }} ./main.go
6767

6868
- name: Build archive (unix)
6969
if: ${{ matrix.goos != 'windows' }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ to download the binary for your operating system.
2121
Make sure you have Go installed, then run:
2222

2323
```bash
24-
go install -trimpath -ldflags="-s -w" github.com/ryanfowler/fetch/cmd/fetch@latest
24+
go install github.com/ryanfowler/fetch@latest
2525
```
2626

2727
### Updating

internal/client/client.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ const (
2424
)
2525

2626
type Client struct {
27-
c *http.Client
27+
c *http.Client
28+
userAgent string
2829
}
2930

3031
type ClientConfig struct {
31-
HTTP HTTPVersion
32-
Insecure bool
33-
Proxy *url.URL
34-
Timeout time.Duration
32+
HTTP HTTPVersion
33+
Insecure bool
34+
Proxy *url.URL
35+
Timeout time.Duration
36+
UserAgent string
3537
}
3638

3739
func NewClient(cfg ClientConfig) *Client {
@@ -63,6 +65,7 @@ func NewClient(cfg ClientConfig) *Client {
6365
Timeout: cfg.Timeout,
6466
Transport: transport,
6567
},
68+
userAgent: cfg.UserAgent,
6669
}
6770
}
6871

@@ -113,7 +116,7 @@ func (c *Client) NewRequest(ctx context.Context, cfg RequestConfig) (*http.Reque
113116
}
114117

115118
req.Header.Set("Accept", "application/json,application/xml,image/webp,*/*")
116-
req.Header.Set("User-Agent", vars.UserAgent)
119+
req.Header.Set("User-Agent", c.userAgent)
117120

118121
switch {
119122
case cfg.JSON:

internal/fetch/fetch.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Request struct {
4141
NoPager bool
4242
Output string
4343
PrinterHandle *printer.Handle
44+
UserAgent string
4445
Verbosity Verbosity
4546

4647
Method string
@@ -93,10 +94,11 @@ func fetch(ctx context.Context, r *Request) (bool, error) {
9394
}
9495

9596
c := client.NewClient(client.ClientConfig{
96-
HTTP: r.HTTP,
97-
Insecure: r.Insecure,
98-
Proxy: r.Proxy,
99-
Timeout: r.Timeout,
97+
HTTP: r.HTTP,
98+
Insecure: r.Insecure,
99+
Proxy: r.Proxy,
100+
Timeout: r.Timeout,
101+
UserAgent: r.UserAgent,
100102
})
101103
req, err := c.NewRequest(ctx, client.RequestConfig{
102104
Method: r.Method,

internal/update/update.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ import (
1414

1515
"github.com/ryanfowler/fetch/internal/client"
1616
"github.com/ryanfowler/fetch/internal/printer"
17-
"github.com/ryanfowler/fetch/internal/vars"
1817
)
1918

20-
func Update(ctx context.Context, p *printer.Printer, timeout time.Duration) bool {
21-
err := update(ctx, p, timeout)
19+
func Update(ctx context.Context, p *printer.Printer, timeout time.Duration, version string) bool {
20+
err := update(ctx, p, timeout, version)
2221
if err == nil {
2322
return true
2423
}
@@ -33,8 +32,8 @@ func Update(ctx context.Context, p *printer.Printer, timeout time.Duration) bool
3332
return false
3433
}
3534

36-
func update(ctx context.Context, p *printer.Printer, timeout time.Duration) error {
37-
cfg := client.ClientConfig{Timeout: timeout}
35+
func update(ctx context.Context, p *printer.Printer, timeout time.Duration, version string) error {
36+
cfg := client.ClientConfig{Timeout: timeout, UserAgent: "fetch/" + version}
3837
c := client.NewClient(cfg)
3938

4039
writeInfo(p, "fetching latest release tag")
@@ -43,9 +42,9 @@ func update(ctx context.Context, p *printer.Printer, timeout time.Duration) erro
4342
return err
4443
}
4544

46-
if strings.TrimPrefix(latest, "v") == vars.Version {
45+
if strings.TrimPrefix(latest, "v") == version {
4746
p.WriteString("\n currently using the latest version (v")
48-
p.WriteString(vars.Version)
47+
p.WriteString(version)
4948
p.WriteString(")\n")
5049
p.Flush(os.Stderr)
5150
return nil
@@ -78,7 +77,7 @@ func update(ctx context.Context, p *printer.Printer, timeout time.Duration) erro
7877
}
7978

8079
p.WriteString("\n fetch successfully updated (v")
81-
p.WriteString(vars.Version)
80+
p.WriteString(version)
8281
p.WriteString(" -> ")
8382
p.WriteString(latest)
8483
p.WriteString(")\n")

internal/vars/vars.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@ package vars
33
import (
44
"os"
55

6-
"github.com/ryanfowler/fetch"
7-
86
"golang.org/x/term"
97
)
108

119
var (
12-
UserAgent string
13-
Version = fetch.Version
14-
1510
IsStderrTerm bool
1611
IsStdoutTerm bool
1712
)
@@ -21,8 +16,6 @@ type KeyVal struct {
2116
}
2217

2318
func init() {
24-
UserAgent = "fetch/" + Version
25-
2619
IsStderrTerm = term.IsTerminal(int(os.Stderr.Fd()))
2720
IsStdoutTerm = term.IsTerminal(int(os.Stdout.Fd()))
2821
}

cmd/fetch/main.go renamed to main.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ package main
22

33
import (
44
"context"
5+
_ "embed"
56
"errors"
67
"fmt"
78
"os"
9+
"strings"
810

911
"github.com/ryanfowler/fetch/internal/cli"
1012
"github.com/ryanfowler/fetch/internal/fetch"
1113
"github.com/ryanfowler/fetch/internal/multipart"
1214
"github.com/ryanfowler/fetch/internal/printer"
1315
"github.com/ryanfowler/fetch/internal/update"
14-
"github.com/ryanfowler/fetch/internal/vars"
1516
)
1617

18+
//go:embed VERSION
19+
var version string
20+
21+
func init() {
22+
version = strings.TrimSpace(version)
23+
}
24+
1725
func main() {
1826
ctx, cancel := context.WithCancel(context.Background())
1927
defer cancel()
@@ -34,12 +42,12 @@ func main() {
3442
os.Exit(0)
3543
}
3644
if app.Version {
37-
fmt.Fprintln(os.Stdout, "fetch", vars.Version)
45+
fmt.Fprintln(os.Stdout, "fetch", version)
3846
os.Exit(0)
3947
}
4048
if app.Update {
4149
p := printerHandle.Stderr()
42-
if ok := update.Update(ctx, p, app.Timeout); ok {
50+
if ok := update.Update(ctx, p, app.Timeout, version); ok {
4351
os.Exit(0)
4452
}
4553
os.Exit(1)
@@ -61,6 +69,7 @@ func main() {
6169
NoPager: app.NoPager,
6270
Output: app.Output,
6371
PrinterHandle: printerHandle,
72+
UserAgent: "fetch/" + version,
6473
Verbosity: getVerbosity(app),
6574

6675
Method: app.Method,

version.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)