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
10 changes: 6 additions & 4 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ var (
// It's overridden from the root command so that we can set the version.
UserAgent = "github.com/exercism/cli"

// DefaultHTTPClient configures a timeout to use by default.
DefaultHTTPClient = &http.Client{Timeout: 10 * time.Second}
// TimeoutInSeconds is the timeout the default HTTP client will use.
TimeoutInSeconds = 60
// HTTPClient is the client used to make HTTP calls in the cli package.
HTTPClient = &http.Client{Timeout: time.Duration(TimeoutInSeconds) * time.Second}
)

// Client is an http client that is configured for Exercism.
Expand All @@ -29,7 +31,7 @@ type Client struct {
// NewClient returns an Exercism API client.
func NewClient(token, baseURL string) (*Client, error) {
return &Client{
Client: DefaultHTTPClient,
Client: HTTPClient,
Token: token,
APIBaseURL: baseURL,
}, nil
Expand All @@ -38,7 +40,7 @@ func NewClient(token, baseURL string) (*Client, error) {
// NewRequest returns an http.Request with information for the Exercism API.
func (c *Client) NewRequest(method, url string, body io.Reader) (*http.Request, error) {
if c.Client == nil {
c.Client = DefaultHTTPClient
c.Client = HTTPClient
}

req, err := http.NewRequest(method, url, body)
Expand Down
4 changes: 3 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ var (
)

var (
// TimeoutInSeconds is the timeout the default HTTP client will use.
TimeoutInSeconds = 60
// HTTPClient is the client used to make HTTP calls in the cli package.
HTTPClient = &http.Client{Timeout: 10 * time.Second}
HTTPClient = &http.Client{Timeout: time.Duration(TimeoutInSeconds) * time.Second}
// ReleaseURL is the endpoint that provides information about cli releases.
ReleaseURL = "https://api.github.com/repos/exercism/cli/releases"
)
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"

"github.com/exercism/cli/api"
"github.com/exercism/cli/cli"
"github.com/exercism/cli/config"
"github.com/exercism/cli/debug"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -39,6 +40,10 @@ Download exercises and submit your solutions.`,
if verbose, _ := cmd.Flags().GetBool("verbose"); verbose {
debug.Verbose = verbose
}
if timeout, _ := cmd.Flags().GetInt("timeout"); timeout > 0 {
cli.TimeoutInSeconds = timeout
api.TimeoutInSeconds = timeout
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you’re talking about in issue #719. I will follow up shortly.

}
},
}

Expand All @@ -57,4 +62,5 @@ func init() {
In = os.Stdin
api.UserAgent = fmt.Sprintf("github.com/exercism/cli v%s (%s/%s)", Version, runtime.GOOS, runtime.GOARCH)
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
RootCmd.PersistentFlags().IntP("timeout", "", 0, "override the default HTTP timeout (seconds)")
}
3 changes: 1 addition & 2 deletions cmd/troubleshoot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"html/template"
"net/http"
"runtime"
"strings"
"sync"
Expand All @@ -30,7 +29,7 @@ If you're running into trouble, copy and paste the output from the troubleshoot
command into a GitHub issue so we can help figure out what's going on.
`,
RunE: func(cmd *cobra.Command, args []string) error {
cli.HTTPClient = &http.Client{Timeout: 20 * time.Second}
cli.TimeoutInSeconds = cli.TimeoutInSeconds * 2
c := cli.New(Version)

cfg := config.NewConfig()
Expand Down