Skip to content

Commit

Permalink
Merge #27
Browse files Browse the repository at this point in the history
Add ability to change user agent.
  • Loading branch information
melbahja committed Oct 19, 2020
2 parents ba72f4c + 1a5df7e commit d5dfae4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 11 additions & 1 deletion cmd/got/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ func main() {
},
&cli.StringSliceFlag{
Name: "header",
Usage: "Set these HTTP-Headers on the requests. The format has to be Key: Value",
Usage: `Set these HTTP-Headers on the requests. The format has to be: -H "Key: Value"`,
Aliases: []string{"H"},
},
&cli.StringFlag{
Name: "agent",
Usage: `Set user agent for got HTTP requests.`,
Aliases: []string{"u"},
},
},
Version: version,
Authors: []*cli.Author{
Expand Down Expand Up @@ -147,6 +152,11 @@ func run(ctx context.Context, c *cli.Context) error {
}
}

// Set default user agent.
if c.String("agent") != "" {
got.UserAgent = c.String("agent")
}

// Piped stdin
if info.Mode()&os.ModeNamedPipe > 0 || info.Size() > 0 {

Expand Down
6 changes: 3 additions & 3 deletions got.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Got struct {
ctx context.Context
}

// DefaulUserAgent is the default Got user agent to send http requests.
const DefaulUserAgent = "Got/1.0"
// UserAgent is the default Got user agent to send http requests.
var UserAgent = "Got/1.0"

// ErrDownloadAborted - When download is aborted by the OS before it is completed, ErrDownloadAborted will be triggered
var ErrDownloadAborted = errors.New("Operation aborted")
Expand Down Expand Up @@ -82,7 +82,7 @@ func NewRequest(ctx context.Context, method, URL string, header []GotHeader) (re
return
}

req.Header.Set("User-Agent", DefaulUserAgent)
req.Header.Set("User-Agent", UserAgent)

for _, h := range header {
req.Header.Set(h.Key, h.Value)
Expand Down

0 comments on commit d5dfae4

Please sign in to comment.