Skip to content

Commit

Permalink
fix: Enable and fix all linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed Oct 2, 2022
1 parent b474522 commit 2ef52a1
Show file tree
Hide file tree
Showing 27 changed files with 705 additions and 345 deletions.
118 changes: 118 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- cyclop
- deadcode
- decorder
- depguard
- dogsled
- dupl
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- forbidigo
- forcetypeassert
- funlen
- gci
- gocognit
- goconst
- gocritic
- gocyclo
- godot
- godox
- goerr113
- gofmt
- goheader
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- govet
- grouper
- importas
- ineffassign
- lll
- maintidx
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- nonamedreturns
- nosnakecase
- nosprintfhostport
- paralleltest
- prealloc
- predeclared
- promlinter
- revive
- staticcheck
- stylecheck
- tenv
- testpackage
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- varcheck
- whitespace
- wsl
- gofumpt
disable:
# Not working with generics
# enable in the future
- contextcheck
- rowserrcheck
- sqlclosecheck
- structcheck
- wastedassign

# These are two strict or deprecated
- interfacer
- ifshort
- ireturn
- wrapcheck
- varnamelen
- gomnd
- tagliatelle
- gochecknoglobals
- exhaustivestruct
- exhaustruct
- golint
- maligned
- gochecknoinits
- scopelint
linters-settings:
goheader:
template: |-
Copyright {{ YEAR }} Tigris Data, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
50 changes: 37 additions & 13 deletions client/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,33 @@ import (
"github.com/tigrisdata/tigris-client-go/driver"
)

// D is single instance of client
// D is single instance of client.
var D driver.Driver

// A is single instance of auth service client
// M is single instance of auth service client.
var M driver.Management
var cfg *cconfig.Driver

func Init(config config.Config) error {
proto := strings.ToLower(strings.Trim(config.Protocol, " "))
if proto == "grpc" {
var ErrUnknownProtocol = fmt.Errorf("unknown protocol set by TIGRIS_PROTOCOL. allowed: grpc, http, https")

func initProtocol(config *config.Config) error {
switch proto := strings.ToLower(strings.Trim(config.Protocol, " ")); proto {
case "grpc":
driver.DefaultProtocol = driver.GRPC
} else if proto == "https" || proto == "http" {
case "https", "http":
driver.DefaultProtocol = driver.HTTP
} else if proto != "" {
return fmt.Errorf("unknown protocol set by TIGRIS_PROTOCOL: %s. allowed: grpc, http, https", proto)
case "":
default:
return fmt.Errorf("%w, got: %s", ErrUnknownProtocol, proto)
}

return nil
}

func initURL(config *config.Config) string {
// URL prefix has precedence over environment variable
url := config.URL
//nolint:golint,gocritic
if strings.HasPrefix(config.URL, "http://") {
driver.DefaultProtocol = driver.HTTP
} else if strings.HasPrefix(config.URL, "https://") {
Expand All @@ -55,13 +63,23 @@ func Init(config config.Config) error {
url = strings.TrimPrefix(config.URL, "grpc://")
}

//Client would use HTTPS if scheme is not explicitly specified
//Avoid this for localhost connections
// Client would use HTTPS if scheme is not explicitly specified.
// Avoid this for localhost connections.
if !strings.Contains(url, "://") && driver.DefaultProtocol == driver.HTTP &&
(strings.HasPrefix(url, "localhost") || strings.HasPrefix(url, "127.0.0.1")) {
url = "http://" + url
}

return url
}

func Init(config *config.Config) error {
if err := initProtocol(config); err != nil {
return err
}

url := initURL(config)

cfg = &cconfig.Driver{
URL: url,
ClientID: config.ClientID,
Expand All @@ -70,7 +88,7 @@ func Init(config config.Config) error {
}

if config.UseTLS || cfg.ClientSecret != "" || cfg.ClientID != "" || cfg.Token != "" {
cfg.TLS = &tls.Config{}
cfg.TLS = &tls.Config{MinVersion: tls.VersionTLS12}
}

_ = os.Unsetenv("TIGRIS_PROTOCOL")
Expand All @@ -87,20 +105,23 @@ func InitLow() error {
if err != nil {
return err
}

D = drv
}

return nil
}

// Get returns an instance of client
// Get returns an instance of client.
func Get() driver.Driver {
if err := InitLow(); err != nil {
util.Error(err, "tigris client initialization failed")
}

return D
}

// ManagementGet returns an instance of authentication API client
// ManagementGet returns an instance of authentication API client.
func ManagementGet() driver.Management {
if M == nil {
ctx, cancel := util.GetContext(context.Background())
Expand All @@ -110,8 +131,10 @@ func ManagementGet() driver.Management {
if err != nil {
util.Error(err, "tigris client initialization failed")
}

M = drv
}

return M
}

Expand All @@ -123,6 +146,7 @@ func Transact(bctx context.Context, db string, fn func(ctx context.Context, tx d
if err != nil {
util.Error(err, "begin transaction failed")
}

defer func() { _ = tx.Rollback(ctx) }()

fn(ctx, tx)
Expand Down
16 changes: 11 additions & 5 deletions cmd/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,30 @@ import (
"github.com/tigrisdata/tigris-client-go/driver"
)

var ErrSchemaNameMissing = fmt.Errorf("schema name is missing")

func createCollection(ctx context.Context, tx driver.Tx, raw driver.Schema) {
type Schema struct {
Name string `json:"title"`
}

var schema Schema

if err := json.Unmarshal(raw, &schema); err != nil {
util.Error(err, "error parsing collection schema")
}

if schema.Name == "" {
util.Error(fmt.Errorf("schema name is missing"), "create collection failed")
util.Error(ErrSchemaNameMissing, "create collection failed")
}

err := tx.CreateOrUpdateCollection(ctx, schema.Name, raw)
if err != nil {
util.Error(err, "create collection failed")
}
}

// DescribeCollectionResponse adapter to convert Schema field to json.RawMessage
// DescribeCollectionResponse adapter to convert Schema field to json.RawMessage.
type DescribeCollectionResponse struct {
Collection string `json:"collection,omitempty"`
Metadata *api.CollectionMetadata `json:"metadata,omitempty"`
Expand All @@ -65,7 +71,7 @@ var describeCollectionCmd = &cobra.Command{

tr := DescribeCollectionResponse{
Collection: resp.Collection,
//Metadata: resp.Metadata,
// Metadata: resp.Metadata,
Schema: resp.Schema,
}

Expand All @@ -74,7 +80,7 @@ var describeCollectionCmd = &cobra.Command{
util.Error(err, "describe collection failed")
}

util.Stdout("%s\n", string(b))
util.Stdoutf("%s\n", string(b))
},
}

Expand All @@ -90,7 +96,7 @@ var listCollectionsCmd = &cobra.Command{
util.Error(err, "list collections failed")
}
for _, v := range resp {
util.Stdout("%s\n", v)
util.Stdoutf("%s\n", v)
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var showConfigCmd = &cobra.Command{
if err != nil {
util.Error(err, "marshal config failed")
}
util.Stdout("%s\n", string(info))
util.Stdoutf("%s\n", string(info))
},
}

Expand Down
16 changes: 8 additions & 8 deletions cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ var listDatabasesCmd = &cobra.Command{
util.Error(err, "list databases failed")
}
for _, v := range resp {
util.Stdout("%s\n", v)
util.Stdoutf("%s\n", v)
}
},
}

// DescribeDatabaseResponse adapter to convert schema to json.RawMessage
// DescribeDatabaseResponse adapter to convert schema to json.RawMessage.
type DescribeDatabaseResponse struct {
Db string `json:"db,omitempty"`
DB string `json:"db,omitempty"`
Metadata *api.DatabaseMetadata `json:"metadata,omitempty"`
Collections []*DescribeCollectionResponse `json:"collections,omitempty"`
}
Expand All @@ -66,18 +66,18 @@ var describeDatabaseCmd = &cobra.Command{

if schemaOnly {
for _, v := range resp.Collections {
util.Stdout("%s\n", string(v.Schema))
util.Stdoutf("%s\n", string(v.Schema))
}
} else {
tr := DescribeDatabaseResponse{
Db: resp.Db,
//Metadata: resp.Metadata,
DB: resp.Db,
// Metadata: resp.Metadata,
}

for _, v := range resp.Collections {
tr.Collections = append(tr.Collections, &DescribeCollectionResponse{
Collection: v.Collection,
//Metadata: v.Metadata,
// Metadata: v.Metadata,
Schema: v.Schema,
})
}
Expand All @@ -87,7 +87,7 @@ var describeDatabaseCmd = &cobra.Command{
util.Error(err, "describe database failed")
}

util.Stdout("%s\n", string(b))
util.Stdoutf("%s\n", string(b))
}
},
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,17 @@ var sampleSchemaCmd = &cobra.Command{
}
})

util.Stdout("%v created with the collections\n", sampleDBName)
util.Stdoutf("%v created with the collections\n", sampleDBName)
} else {
stdout, err := cmd.Flags().GetBool("stdout")
if err != nil {
util.Error(err, "error reading the 'stdout' option")
}
for name, schema := range schemas {
if stdout {
util.Stdout("%s\n", string(schema))
util.Stdoutf("%s\n", string(schema))
} else {
if err := os.WriteFile(fmt.Sprintf("%v.json", name), schema, 0644); err != nil {
if err := os.WriteFile(fmt.Sprintf("%v.json", name), schema, 0o600); err != nil {
util.Error(err, "error generating sample schema file")
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var infoCmd = &cobra.Command{
if err != nil {
util.Error(err, "get server info failed")
}
util.Stdout("%s\n", string(info))
util.Stdoutf("%s\n", string(info))
},
}

Expand Down
Loading

0 comments on commit 2ef52a1

Please sign in to comment.