Skip to content

Commit

Permalink
style: fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Dec 21, 2023
1 parent b9d85f1 commit f8fa1d0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ linters:
- gci
- varnamelen
- exhaustruct
- depguard
run:
tests: false
issues:
Expand Down
13 changes: 12 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,18 @@ func init() { //nolint: gochecknoinits
}

func initConfig() {
dep = cli.GetDependencies()
dep, err = cli.GetDependencies()

if err != nil {
fmt.Println(err)
os.Exit(1)
}

ctx, err = cli.GetContext(dep, options, configPath)

if err != nil {
fmt.Println(err)
os.Exit(1)
}

}
10 changes: 7 additions & 3 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,22 @@ func Validate(ctx *c.Context, options *Options, prevErr *error) func(*cobra.Comm
}
}

func GetDependencies() c.Dependencies {
func GetDependencies() (c.Dependencies, error) {

client := yahooClient.New(resty.New(), resty.New())
yahooClient.RefreshSession(client, resty.New())
err := yahooClient.RefreshSession(client, resty.New())

if err != nil {
return c.Dependencies{}, err
}

return c.Dependencies{
Fs: afero.NewOsFs(),
HttpClients: c.DependenciesHttpClients{
Default: resty.New(),
Yahoo: client,
},
}
}, nil

}

Expand Down
34 changes: 18 additions & 16 deletions internal/quote/yahoo/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
userAgentClientHintPlatform = "\"Windows\""
)

var errSessionRefresh = errors.New("yahoo session refresh error")

func New(clientMain *resty.Client, clientSession *resty.Client) *resty.Client {

client := clientMain.
Expand Down Expand Up @@ -95,7 +97,7 @@ func getCookie(client *resty.Client) ([]*http.Cookie, error) {
Get("https://finance.yahoo.com/")

if err != nil && !strings.Contains(err.Error(), "stopped after") {
return nil, fmt.Errorf("error requesting a cookie: %w", err)
return nil, fmt.Errorf("%w: error requesting a cookie: %w", errSessionRefresh, err)

Check failure on line 100 in internal/quote/yahoo/client/client.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

fmt.Errorf call has more than one error-wrapping directive %w
}

if isEUConsentRedirect(res) {
Expand All @@ -104,7 +106,7 @@ func getCookie(client *resty.Client) ([]*http.Cookie, error) {

x := res.Cookies()
if !isRequiredCookieSet(res) {
return nil, errors.New("unexpected response from Yahoo API: A3 session cookie missing from response")
return nil, fmt.Errorf("%w: unexpected response from Yahoo API: A3 session cookie missing from response", errSessionRefresh)
}

return x, nil
Expand All @@ -116,7 +118,7 @@ func getCookieEU(client *resty.Client) ([]*http.Cookie, error) {
var cookies []*http.Cookie

reCsrfToken := regexp.MustCompile("gcrumb=(?:([A-Za-z0-9_]*))")
reSessionId := regexp.MustCompile("sessionId=(?:([A-Za-z0-9_-]*))")
reSessionID := regexp.MustCompile("sessionId=(?:([A-Za-z0-9_-]*))")

res1, err1 := client.
SetRedirectPolicy(resty.FlexibleRedirectPolicy(3)).
Expand All @@ -140,29 +142,29 @@ func getCookieEU(client *resty.Client) ([]*http.Cookie, error) {
}

if !strings.HasPrefix(res1.Status(), "2") {
return cookies, fmt.Errorf("unexpected response from Yahoo API: non-2xx response code: %s", res1.Status())
return cookies, fmt.Errorf("%w: unexpected response from Yahoo API: non-2xx response code: %s", errSessionRefresh, res1.Status())
}

sessionIdMatchResult := reSessionId.FindStringSubmatch(res1.RawResponse.Request.URL.String())
sessionIDMatchResult := reSessionID.FindStringSubmatch(res1.RawResponse.Request.URL.String())

if len(sessionIdMatchResult) != 2 {
return cookies, fmt.Errorf("error unable to extract session id from redirected request URL: '%s'", res1.Request.URL)
if len(sessionIDMatchResult) != 2 {
return cookies, fmt.Errorf("%w: error unable to extract session id from redirected request URL: %s", errSessionRefresh, res1.Request.URL)
}

sessionId := sessionIdMatchResult[1]
sessionID := sessionIDMatchResult[1]

csrfTokenMatchResult := reCsrfToken.FindStringSubmatch(res1.RawResponse.Request.Response.Request.URL.String())

if len(csrfTokenMatchResult) != 2 {
return cookies, fmt.Errorf("error unable to extract CSRF token from Location header: '%s'", res1.Header().Get("Location"))
return cookies, fmt.Errorf("%w: error unable to extract CSRF token from Location header: '%s'", errSessionRefresh, res1.Header().Get("Location"))
}

csrfToken := csrfTokenMatchResult[1]

GUCSCookie := res1.RawResponse.Request.Response.Request.Response.Cookies()

if len(GUCSCookie) == 0 {
return cookies, fmt.Errorf("no cookies set by finance.yahoo.com")
return cookies, fmt.Errorf("%w: no cookies set by finance.yahoo.com", errSessionRefresh)
}

res2, err2 := client.
Expand All @@ -183,23 +185,23 @@ func getCookieEU(client *resty.Client) ([]*http.Cookie, error) {
SetHeader("sec-fetch-mode", "navigate").
SetHeader("sec-fetch-site", "same-origin").
SetHeader("sec-fetch-user", "?1").
SetHeader("referer", "https://consent.yahoo.com/v2/collectConsent?sessionId="+sessionId).
SetHeader("referer", "https://consent.yahoo.com/v2/collectConsent?sessionId="+sessionID).
SetHeader("user-agent", userAgent).
SetCookies(GUCSCookie).
SetFormData(map[string]string{
"csrfToken": csrfToken,
"sessionId": sessionId,
"sessionId": sessionID,
"namespace": "yahoo",
"agree": "agree",
}).
Post("https://consent.yahoo.com/v2/collectConsent?sessionId=" + sessionId)
Post("https://consent.yahoo.com/v2/collectConsent?sessionId=" + sessionID)

if err2 != nil && !strings.Contains(err2.Error(), "stopped after") {
return cookies, fmt.Errorf("error attempting to agree to EU consent request: %w", err2)
return cookies, fmt.Errorf("%w: error attempting to agree to EU consent request: %w", errSessionRefresh, err2)

Check failure on line 200 in internal/quote/yahoo/client/client.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

fmt.Errorf call has more than one error-wrapping directive %w
}

if !isRequiredCookieSet(res2) {
return nil, fmt.Errorf("unexpected response from Yahoo API: A3 session cookie missing from response after agreeing to EU consent request: %s", res2.Status())
return nil, fmt.Errorf("%w: unexpected response from Yahoo API: A3 session cookie missing from response after agreeing to EU consent request: %s", errSessionRefresh, res2.Status())
}

return res2.Cookies(), nil
Expand Down Expand Up @@ -228,7 +230,7 @@ func getCrumb(client *resty.Client, cookies []*http.Cookie) (string, error) {
}

if !strings.HasPrefix(res.Status(), "2") {
return "", fmt.Errorf("unexpected response from Yahoo API when attempting to retrieve crumb: non-2xx response code: %s", res.Status())
return "", fmt.Errorf("%w: unexpected response from Yahoo API when attempting to retrieve crumb: non-2xx response code: %s", errSessionRefresh, res.Status())
}

return res.String(), err
Expand Down
8 changes: 4 additions & 4 deletions internal/ui/component/watchlist/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ func buildCells(asset c.Asset, config c.Config, styles c.Styles, cellWidths cell
cells = append(
[]grid.Cell{
{
Text: textVolumeMarketCapLabels(asset, styles),
Text: textVolumeMarketCapLabels(styles),
Width: widthLabel,
Align: grid.Right,
VisibleMinWidth: widthMinTerm + cellWidths.WidthQuoteExtended + (6 * widthGutter) + (3 * widthLabel) + cellWidths.WidthQuoteRange + cellWidths.WidthVolumeMarketCap,
},
{
Text: textVolumeMarketCap(asset, styles),
Text: textVolumeMarketCap(asset),
Width: cellWidths.WidthVolumeMarketCap,
Align: grid.Right,
VisibleMinWidth: widthMinTerm + cellWidths.WidthQuoteExtended + (5 * widthGutter) + (2 * widthLabel) + cellWidths.WidthQuoteRange + cellWidths.WidthVolumeMarketCap,
Expand Down Expand Up @@ -384,14 +384,14 @@ func textQuoteRangeLabels(asset c.Asset, styles c.Styles) string {
return textDayRange
}

func textVolumeMarketCap(asset c.Asset, styles c.Styles) string {
func textVolumeMarketCap(asset c.Asset) string {

return u.ConvertFloatToString(asset.QuoteExtended.MarketCap, true) +
"\n" +
u.ConvertFloatToString(asset.QuoteExtended.Volume, true)
}

func textVolumeMarketCapLabels(asset c.Asset, styles c.Styles) string {
func textVolumeMarketCapLabels(styles c.Styles) string {

return styles.TextLabel("Market Cap:") +
"\n" +
Expand Down

0 comments on commit f8fa1d0

Please sign in to comment.