Skip to content

Commit

Permalink
chore: first
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Gelloz committed Dec 29, 2022
1 parent cad000b commit cd7d8a0
Show file tree
Hide file tree
Showing 65 changed files with 823 additions and 1,634 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
vendor
coverage.out
coverage.*
23 changes: 0 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
linters-settings:
goimports:
local-prefixes: github.com/golangci/golangci-lint
govet:
check-shadowing: false

nolintlint:
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped

linters:
disable-all: true
enable:
- deadcode #Default linter
- errcheck #Default linter
- gosimple #Default linter
- govet #Default linter
- ineffassign #Default linter
- staticcheck #Default linter
- structcheck #Default linter
- typecheck #Default linter
- unused #Default linter
- varcheck #Default linter
- gofmt
- gci
- goimports
Expand Down
25 changes: 23 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
version: '3'

vars:
PKG: ./...
PKG: "./..."
FAILFAST: "-failfast"
TIMEOUT: "1m"
RUN: "''"

tasks:
default:
cmds:
- task: lint
- task: tests
- task: tests:local

lint:
cmds:
Expand All @@ -16,3 +19,21 @@ tasks:
tests:
cmds:
- go test -v -coverpkg {{.PKG}} -coverprofile coverage.out -covermode atomic {{.PKG}}

tests:local:
cmds:
- >
go test -v {{.FAILFAST}} -coverpkg {{.PKG}} -coverprofile coverage.out
-run {{.RUN}} -timeout {{.TIMEOUT}} {{.PKG}} |
sed ''/PASS/s//$(printf "\033[32mPASS\033[0m")/'' |
sed ''/FAIL/s//$(printf "\033[31mFAIL\033[0m")/'' |
sed ''/RUN/s//$(printf "\033[34mRUN\033[0m")/''
- task: coverage

coverage:
cmds:
- go tool cover -html=coverage.out -o coverage.html
- echo "To open the html coverage file, use one of the following commands:"
- echo "open coverage.html on mac"
- echo "xdg-open coverage.html on linux"
silent: true
22 changes: 13 additions & 9 deletions sharedanalytics/pkg/cli.go → analytics/cli.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sharedanalytics
package analytics

import (
"context"
"time"

"github.com/coreos/go-semver/semver"
"github.com/formancehq/go-libs/v2/sharedlogging"
"github.com/formancehq/go-libs/logging"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/fx"
Expand Down Expand Up @@ -42,10 +42,14 @@ func InitAnalyticsFlags(cmd *cobra.Command, defaultWriteKey string, useDeprecate

func NewAnalyticsModule(v *viper.Viper, version string, useDeprecatedFlags bool) fx.Option {
if v.GetBool(telemetryEnabledFlag) || (useDeprecatedFlags && v.GetBool(segmentEnabledFlag)) {
applicationId := viper.GetString(telemetryApplicationIdFlag)
if applicationId == "" && useDeprecatedFlags {
applicationId = viper.GetString(segmentApplicationIdFlag)
}

/*
applicationId := viper.GetString(telemetryApplicationIdFlag)
if applicationId == "" && useDeprecatedFlags {
applicationId = viper.GetString(segmentApplicationIdFlag)
}
*/

writeKey := viper.GetString(telemetryWriteKeyFlag)
if writeKey == "" && useDeprecatedFlags {
writeKey = viper.GetString(segmentWriteKeyFlag)
Expand All @@ -55,13 +59,13 @@ func NewAnalyticsModule(v *viper.Viper, version string, useDeprecatedFlags bool)
interval = viper.GetDuration(segmentHeartbeatIntervalFlag)
}
if writeKey == "" {
sharedlogging.GetLogger(context.Background()).Infof("telemetry enabled but no write flagKey provided")
logging.GetLogger(context.Background()).Infof("telemetry enabled but no write flagKey provided")
} else if interval == 0 {
sharedlogging.GetLogger(context.Background()).Error("telemetry heartbeat interval is 0")
logging.GetLogger(context.Background()).Error("telemetry heartbeat interval is 0")
} else {
_, err := semver.NewVersion(version)
if err != nil {
sharedlogging.GetLogger(context.Background()).Infof("telemetry enabled but version '%s' is not semver, skip", version)
logging.GetLogger(context.Background()).Infof("telemetry enabled but version '%s' is not semver, skip", version)
} else {
return NewHeartbeatModule(version, writeKey, interval)
}
Expand Down
2 changes: 1 addition & 1 deletion sharedanalytics/pkg/cli_test.go → analytics/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedanalytics
package analytics

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion sharedanalytics/pkg/module.go → analytics/module.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedanalytics
package analytics

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedanalytics
package analytics

import (
"context"
Expand Down
8 changes: 4 additions & 4 deletions sharedanalytics/pkg/segment.go → analytics/segment.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sharedanalytics
package analytics

import (
"context"
"runtime"
"time"

"github.com/formancehq/go-libs/v2/sharedlogging"
"github.com/formancehq/go-libs/logging"
"github.com/pbnjay/memory"
"github.com/segmentio/analytics-go"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func (m *heartbeat) Run(ctx context.Context) error {
enqueue := func() {
err := m.enqueue(ctx)
if err != nil {
sharedlogging.GetLogger(ctx).WithFields(map[string]interface{}{
logging.GetLogger(ctx).WithFields(map[string]interface{}{
"error": err,
}).Error("enqueuing analytics")
}
Expand Down Expand Up @@ -103,7 +103,7 @@ func (m *heartbeat) enqueue(ctx context.Context) error {

for _, enricher := range m.enrichers {
if err := enricher.Enrich(ctx, properties); err != nil {
sharedlogging.GetLogger(ctx).Errorf("Enricher return error: %s", err)
logging.GetLogger(ctx).Errorf("Enricher return error: %s", err)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package sharedanalytics
package analytics

import (
"bytes"
"context"
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"sync"
"testing"
Expand Down Expand Up @@ -96,7 +96,7 @@ func EventuallyQueueNotEmpty[ITEM any](t *testing.T, queue *Queue[ITEM]) {
}

var emptyHttpResponse = &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte{})),
Body: io.NopCloser(bytes.NewReader([]byte{})),
StatusCode: http.StatusOK,
}

Expand Down
2 changes: 1 addition & 1 deletion sharedapi/handler_info.go → api/handler_info.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedapi
package api

import (
"encoding/json"
Expand Down
10 changes: 5 additions & 5 deletions sharedapi/response.go → api/response.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedapi
package api

type BaseResponse[T any] struct {
Data *T `json:"data,omitempty"`
Expand All @@ -7,7 +7,7 @@ type BaseResponse[T any] struct {

type Cursor[T any] struct {
PageSize int `json:"pageSize,omitempty"`
Total Total `json:"total,omitempty"`
Total *Total `json:"total,omitempty"`
HasMore bool `json:"hasMore"`
Previous string `json:"previous,omitempty"`
Next string `json:"next,omitempty"`
Expand All @@ -16,12 +16,12 @@ type Cursor[T any] struct {
// deprecated
PageSizeDeprecated int `json:"page_size,omitempty"`
// deprecated
HasMoreDeprecated *bool `json:"has_more"`
HasMoreDeprecated *bool `json:"has_more,omitempty"`
}

type Total struct {
Value uint64 `json:"value"`
Rel string `json:"relation"`
Value uint64 `json:"value,omitempty"`
Rel string `json:"relation,omitempty"`
}

type ErrorResponse struct {
Expand Down
33 changes: 33 additions & 0 deletions api/response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package api

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCursor(t *testing.T) {
c := Cursor[int64]{
Data: []int64{1, 2, 3},
}
by, err := json.Marshal(c)
require.NoError(t, err)
assert.Equal(t, `{"hasMore":false,"data":[1,2,3]}`, string(by))

hasMore := true
c = Cursor[int64]{
Data: []int64{1, 2, 3},
Total: &Total{
Value: 10,
Rel: "eq",
},
HasMoreDeprecated: &hasMore,
}
by, err = json.Marshal(c)
require.NoError(t, err)
assert.Equal(t,
`{"total":{"value":10,"relation":"eq"},"hasMore":false,"data":[1,2,3],"has_more":true}`,
string(by))
}
4 changes: 1 addition & 3 deletions sharedauth/auth.go → auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package sharedauth
package auth

import (
"net/http"

_ "github.com/formancehq/go-libs/v2/sharedlogging/sharedlogginglogrus"
)

type Agent interface {
Expand Down
2 changes: 1 addition & 1 deletion sharedauth/auth_test.go → auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedauth
package auth

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedauthjwt
package authjwt

import (
"net/http"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedauthjwt
package authjwt

import (
"net/http"
Expand Down
14 changes: 7 additions & 7 deletions sharedauth/basic.go → auth/basic.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedauth
package auth

import (
"net/http"
Expand All @@ -18,18 +18,18 @@ func (c Credential) GetScopes() []string {

type Credentials map[string]Credential

type httpBasicMethod struct {
type HttpBasicMethod struct {
credentials Credentials
}

func (h httpBasicMethod) IsMatching(c *http.Request) bool {
func (h HttpBasicMethod) IsMatching(c *http.Request) bool {
return strings.HasPrefix(
strings.ToLower(c.Header.Get("Authorization")),
"basic",
)
}

func (h httpBasicMethod) Check(c *http.Request) (Agent, error) {
func (h HttpBasicMethod) Check(c *http.Request) (Agent, error) {
username, password, ok := c.BasicAuth()
if !ok {
return nil, errors.New("malformed basic")
Expand All @@ -47,10 +47,10 @@ func (h httpBasicMethod) Check(c *http.Request) (Agent, error) {
return credential, nil
}

func NewHTTPBasicMethod(credentials Credentials) *httpBasicMethod {
return &httpBasicMethod{
func NewHTTPBasicMethod(credentials Credentials) *HttpBasicMethod {
return &HttpBasicMethod{
credentials: credentials,
}
}

var _ Method = &httpBasicMethod{}
var _ Method = &HttpBasicMethod{}
2 changes: 1 addition & 1 deletion sharedauth/basic_test.go → auth/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sharedauth
package auth

import (
"net/http"
Expand Down
16 changes: 8 additions & 8 deletions sharedauth/bearer.go → auth/bearer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package sharedauth
package auth

import (
"context"
"fmt"
"net/http"
"strings"

"github.com/formancehq/go-libs/v2/oauth2/oauth2introspect"
"github.com/formancehq/go-libs/oauth2/oauth2introspect"
"github.com/golang-jwt/jwt"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -123,18 +123,18 @@ func (o oauth2Agent) GetScopes() []string {
return strings.Split(scopeClaimAsString, " ")
}

type oauth2BearerMethod struct {
type Oauth2BearerMethod struct {
validator validator
}

func (h oauth2BearerMethod) IsMatching(c *http.Request) bool {
func (h Oauth2BearerMethod) IsMatching(c *http.Request) bool {
return strings.HasPrefix(
strings.ToLower(c.Header.Get("Authorization")),
"bearer",
)
}

func (h *oauth2BearerMethod) Check(c *http.Request) (Agent, error) {
func (h *Oauth2BearerMethod) Check(c *http.Request) (Agent, error) {
token := c.Header.Get("Authorization")[len("bearer "):]
err := h.validator.Validate(c.Context(), token)
if err != nil {
Expand All @@ -150,10 +150,10 @@ func (h *oauth2BearerMethod) Check(c *http.Request) (Agent, error) {
}, nil
}

var _ Method = &oauth2BearerMethod{}
var _ Method = &Oauth2BearerMethod{}

func NewHttpBearerMethod(validator validator) *oauth2BearerMethod {
return &oauth2BearerMethod{
func NewHttpBearerMethod(validator validator) *Oauth2BearerMethod {
return &Oauth2BearerMethod{
validator: validator,
}
}
Loading

0 comments on commit cd7d8a0

Please sign in to comment.