Skip to content

Commit

Permalink
Drop invalid prometheus log import
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 8, 2021
1 parent ceed32c commit 0ababad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
goos: ${{ matrix.goos }}
goarch: ${{ matrix.goarch }}
goversion: "https://dl.google.com/go/go1.16.5.linux-amd64.tar.gz"
goversion: "1.16"
project_path: "."
ldflags: "-X 'github.com/leighmacdonald/steamid/v2/steamid.BuildVersion=`git describe --abbrev=0`'"
binary_name: "steamid"
extra_files: LICENSE.md README.md
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ run:

test:
GOTRACEBACK=all GODEBUG=netdns=go $(GO_TEST) -v ./...

lint:
@golangci-lint run
19 changes: 10 additions & 9 deletions steamid/steamid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package steamid
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/prometheus/common/log"
"io/ioutil"
"math/big"
"net/http"
Expand Down Expand Up @@ -117,12 +117,12 @@ func (t *SID64) UnmarshalJSON(data []byte) error {
//
// You can alternatively set the key with the environment variable `STEAM_TOKEN={YOUR_API_KEY`
// To get a key see: https://steamcommunity.com/dev/apikey
func SetKey(key string) {
func SetKey(key string) error {
if len(key) != 32 && len(key) != 0 {
log.Warnf("Tried to set invalid key, must be 32 chars or 0 to remove it")
return
return errors.New("Tried to set invalid key, must be 32 chars or 0 to remove it")
}
apiKey = key
return nil
}

// GetKey returns the steam web api key, if set, otherwise empty string
Expand Down Expand Up @@ -422,8 +422,8 @@ func ResolveVanity(ctx context.Context, query string) (SID64, error) {
if len(vanityURLResponse.Response.Steamid) != 17 {
return SID64(0), errors.Errorf("Malformed steamid received: %s", vanityURLResponse.Response.Steamid)
}
output, err := strconv.ParseInt(vanityURLResponse.Response.Steamid, 10, 64)
if err != nil {
output, errI := strconv.ParseInt(vanityURLResponse.Response.Steamid, 10, 64)
if errI != nil {
return SID64(0), errors.Wrap(err, "Failed to parse int from steamid received")
}
return SID64(output), nil
Expand Down Expand Up @@ -523,8 +523,7 @@ func ParseString(body string) []SID64 {

for _, i := range m1 {
s, err := SID64FromString(i[0])
if err != nil {
log.Warnf("Failed to parse string to sid64: %s", i[0])
if err != nil || !s.Valid() {
continue
}
found[s] = true
Expand All @@ -542,7 +541,9 @@ func ParseString(body string) []SID64 {

func init() {
if t, found := os.LookupEnv("STEAM_TOKEN"); found && t != "" {
SetKey(t)
if err := SetKey(t); err != nil {
fmt.Printf("(steamid) STEAM_TOKEN invalid: %v\n", err)
}
}
httpClient = &http.Client{
Timeout: time.Second * 10,
Expand Down
26 changes: 14 additions & 12 deletions steamid/steamid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package steamid

import (
"context"
"fmt"
"github.com/stretchr/testify/require"
"os"
"testing"
Expand Down Expand Up @@ -65,7 +66,6 @@ func TestConversions(t *testing.T) {
require.Equal(t, SIDToSID64("STEAM_0:0:86173181"), SID64(76561198132612090))
}


func TestResolveGID(t *testing.T) {
gid1, err := ResolveGID(context.Background(), "SQTreeHouse")
require.NoError(t, err, "Failed to fetch gid")
Expand All @@ -85,32 +85,34 @@ func TestResolveSID(t *testing.T) {
require.NoError(t, err)
require.Equal(t, sid1, SID64(76561197961279983))

sid2, err := ResolveSID64(context.Background(), "https://steamcommunity.com/id/FAKEXXXXXXXXXX123123")
require.Error(t, err)
sid2, err2 := ResolveSID64(context.Background(), "https://steamcommunity.com/id/FAKEXXXXXXXXXX123123")
require.Error(t, err2)
require.False(t, sid2.Valid())

sid3, err := ResolveSID64(context.Background(), "http://steamcommunity.com/profiles/76561197961279983")
require.NoError(t, err)
sid3, err3 := ResolveSID64(context.Background(), "http://steamcommunity.com/profiles/76561197961279983")
require.NoError(t, err3)
require.Equal(t, sid3, SID64(76561197961279983))

sid4, err := ResolveSID64(context.Background(), "[U:1:1014255]")
require.NoError(t, err)
sid4, err4 := ResolveSID64(context.Background(), "[U:1:1014255]")
require.NoError(t, err4)
require.Equal(t, sid4, SID64(76561197961279983))

sid5, err := ResolveSID64(context.Background(), "STEAM_0:1:507127")
sid5, err5 := ResolveSID64(context.Background(), "STEAM_0:1:507127")
require.Equal(t, sid5, SID64(76561197961279983))
require.NoError(t, err)
require.NoError(t, err5)

sid6, err := ResolveSID64(context.Background(), "")
require.Error(t, err)
sid6, err6 := ResolveSID64(context.Background(), "")
require.Error(t, err6)
require.False(t, sid6.Valid())

}

func TestMain(m *testing.M) {
key, found := os.LookupEnv("STEAM_TOKEN")
if found {
SetKey(key)
if e := SetKey(key); e != nil {
fmt.Printf(e.Error())
}
}
os.Exit(m.Run())
}

0 comments on commit 0ababad

Please sign in to comment.