diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a0fa701..02a77ec 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 \ No newline at end of file diff --git a/Makefile b/Makefile index b1798fd..12ba864 100644 --- a/Makefile +++ b/Makefile @@ -32,3 +32,6 @@ run: test: GOTRACEBACK=all GODEBUG=netdns=go $(GO_TEST) -v ./... + +lint: + @golangci-lint run \ No newline at end of file diff --git a/steamid/steamid.go b/steamid/steamid.go index e3a8cab..180c1c0 100644 --- a/steamid/steamid.go +++ b/steamid/steamid.go @@ -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" @@ -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 @@ -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 @@ -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 @@ -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, diff --git a/steamid/steamid_test.go b/steamid/steamid_test.go index 26738c8..b053f9c 100644 --- a/steamid/steamid_test.go +++ b/steamid/steamid_test.go @@ -2,6 +2,7 @@ package steamid import ( "context" + "fmt" "github.com/stretchr/testify/require" "os" "testing" @@ -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") @@ -85,24 +85,24 @@ 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()) } @@ -110,7 +110,9 @@ func TestResolveSID(t *testing.T) { 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()) }