From b11b96fccfddc5b214f0f644f83ce7c5ed36918f Mon Sep 17 00:00:00 2001 From: xuwentao Date: Thu, 6 Apr 2023 19:42:47 +0800 Subject: [PATCH] style: lint code --- .github/workflows/pitr-golint.yml | 2 +- pitr/agent/.golangci.yml | 301 +++++++++++++++++- pitr/agent/Makefile | 4 +- pitr/agent/internal/cons/error.go | 10 +- pitr/agent/internal/handler/backup.go | 6 +- .../handler/middleware/http_header.go | 2 +- .../internal/handler/middleware/logger.go | 12 +- .../internal/handler/middleware/recovery.go | 1 + .../handler/middleware/uniform_err_resp.go | 1 + pitr/agent/internal/handler/restore.go | 8 +- pitr/agent/internal/handler/show.go | 13 +- pitr/agent/internal/handler/view/backup.go | 12 +- pitr/agent/internal/handler/view/restore.go | 19 +- pitr/agent/internal/handler/view/show.go | 39 +-- pitr/agent/internal/pkg/model/backup.go | 4 +- pitr/agent/internal/pkg/opengauss.go | 22 +- pitr/agent/internal/pkg/opengauss_test.go | 1 + pitr/agent/main.go | 11 +- pitr/agent/pkg/cmds/cmd.go | 2 +- pitr/agent/pkg/gsutil/conn.go | 7 +- pitr/agent/pkg/logging/field.go | 6 +- pitr/agent/pkg/logging/log.go | 1 + pitr/agent/pkg/logging/zap_log.go | 1 + pitr/agent/pkg/strutil/rand_string.go | 23 +- pitr/agent/pkg/syncutils/recover_func.go | 4 +- pitr/cli/.golangci.yml | 300 ++++++++++++++++- pitr/cli/Makefile | 4 +- pitr/cli/internal/cmd/backup.go | 18 +- pitr/cli/internal/cmd/restore.go | 10 +- pitr/cli/internal/cmd/restore_test.go | 2 +- pitr/cli/internal/cmd/show.go | 12 +- pitr/cli/internal/cmd/show_test.go | 4 +- pitr/cli/internal/pkg/agent-server.go | 9 +- pitr/cli/internal/pkg/agent-server_test.go | 20 +- pitr/cli/internal/pkg/local-storage.go | 17 +- pitr/cli/internal/pkg/mocks/local-storage.go | 4 +- pitr/cli/internal/pkg/model/as_backup.go | 4 +- pitr/cli/internal/pkg/model/as_restore.go | 6 +- pitr/cli/internal/pkg/model/as_show.go | 12 +- pitr/cli/internal/pkg/shardingsphere-proxy.go | 19 +- pitr/cli/internal/pkg/xerr/err.go | 11 +- pitr/cli/main.go | 2 +- pitr/cli/pkg/httputils/req.go | 3 + pitr/cli/pkg/logging/field.go | 21 +- pitr/cli/pkg/stringutil/rand_string.go | 24 +- pitr/cli/pkg/stringutil/rand_string_test.go | 4 +- 46 files changed, 812 insertions(+), 206 deletions(-) diff --git a/.github/workflows/pitr-golint.yml b/.github/workflows/pitr-golint.yml index 8e799cfd..456f3a39 100644 --- a/.github/workflows/pitr-golint.yml +++ b/.github/workflows/pitr-golint.yml @@ -55,5 +55,5 @@ jobs: $(go env GOPATH)/bin/golangci-lint run -v --timeout 300s ./... - name: Lint Pitr Agent run: | - cd pitr/gent + cd pitr/agent $(go env GOPATH)/bin/golangci-lint run -v --timeout 300s ./... diff --git a/pitr/agent/.golangci.yml b/pitr/agent/.golangci.yml index b7d0d560..1eb74498 100644 --- a/pitr/agent/.golangci.yml +++ b/pitr/agent/.golangci.yml @@ -15,35 +15,188 @@ # limitations under the License. # - run: timeout: 10m + skip-files: + - "^zz_generated.*" + - "_test.go" linters: disable-all: true enable: + # The base lints + - errcheck + - gosimple + - govet - ineffassign + - staticcheck - typecheck - - varcheck - unused - - structcheck - - deadcode - - gosimple + - unused + - bodyclose + - cyclop + - nilerr - goimports - - errcheck - - staticcheck - - stylecheck - - gosec - asciicheck - - bodyclose + - prealloc + - stylecheck - exportloopref - rowserrcheck - makezero - durationcheck - - prealloc + - gosec - predeclared + # Deprecated lints + - structcheck + - varcheck + - deadcode + # The advanced lints + - dupl + - exhaustive + - godot + - misspell + #- varnamelen + - gocritic + #- exhaustruct + #- nestif + #- wsl + #- gocognit # Refers: https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322 linters-settings: + wsl: + # See https://github.com/bombsimon/wsl/blob/master/doc/configuration.md for documentation of available settings. + # These are the defaults for `golangci-lint`. + + # Do strict checking when assigning from append (x = append(x, y)). If + # this is set to true - the append call must append either a variable + # assigned, called or used on the line above. + strict-append: true + # Allows assignments to be cuddled with variables used in calls on + # line above and calls to be cuddled with assignments of variables + # used in call on line above. + allow-assign-and-call: true + # Allows assignments to be cuddled with anything. + allow-assign-and-anything: false + # Allows cuddling to assignments even if they span over multiple lines. + allow-multiline-assign: true + # If the number of lines in a case block is equal to or lager than this + # number, the case *must* end white a newline. + force-case-trailing-whitespace: 0 + # Allow blocks to end with comments. + allow-trailing-comment: false + # Allow multiple comments in the beginning of a block separated with newline. + allow-separated-leading-comment: false + # Allow multiple var/declaration statements to be cuddled. + allow-cuddle-declarations: false + # A list of call idents that everything can be cuddled with. + # Defaults to calls looking like locks. + allow-cuddle-with-calls: ["Lock", "RLock"] + # AllowCuddleWithRHS is a list of right hand side variables that is allowed + # to be cuddled with anything. Defaults to assignments or calls looking + # like unlocks. + allow-cuddle-with-rhs: ["Unlock", "RUnlock"] + # Causes an error when an If statement that checks an error variable doesn't + # cuddle with the assignment of that variable. + force-err-cuddling: false + # When force-err-cuddling is enabled this is a list of names + # used for error variables to check for in the conditional. + error-variable-names: ["err"] + # Causes an error if a short declaration (:=) cuddles with anything other than + # another short declaration. + # This logic overrides force-err-cuddling among others. + force-short-decl-cuddling: false + varnamelen: + # The longest distance, in source lines, that is being considered a "small scope". + # Variables used in at most this many lines will be ignored. + # Default: 5 + max-distance: 6 + # The minimum length of a variable's name that is considered "long". + # Variable names that are at least this long will be ignored. + # Default: 3 + min-name-length: 2 + # Check method receivers. + # Default: false + check-receiver: false + # Check named return values. + # Default: false + check-return: true + # Check type parameters. + # Default: false + check-type-param: true + # Ignore "ok" variables that hold the bool return value of a type assertion. + # Default: false + ignore-type-assert-ok: true + # Ignore "ok" variables that hold the bool return value of a map index. + # Default: false + ignore-map-index-ok: true + # Ignore "ok" variables that hold the bool return value of a channel receive. + # Default: false + ignore-chan-recv-ok: true + # Optional list of variable names that should be ignored completely. + # Default: [] + ignore-names: + - err + # Optional list of variable declarations that should be ignored completely. + # Entries must be in one of the following forms (see below for examples): + # - for variables, parameters, named return values, method receivers, or type parameters: + # ( can also be a pointer/slice/map/chan/...) + # - for constants: const + # + # Default: [] + ignore-decls: + - c echo.Context + - t testing.T + - f *foo.Bar + - e error + - i int + - const C + - T any + - m map[string]int + prealloc: + # IMPORTANT: we don't recommend using this linter before doing performance profiling. + # For most programs usage of prealloc will be a premature optimization. + + # Report pre-allocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. + # Default: true + simple: false + # Report pre-allocation suggestions on range loops. + # Default: true + range-loops: false + # Report pre-allocation suggestions on for loops. + # Default: false + for-loops: true + #nestif: + # Minimal complexity of if statements to report. + # Default: 5 + # min-complexity: 4 + misspell: + # Correct spellings using locale preferences for US or UK. + # Setting locale to US will correct the British spelling of 'colour' to 'color'. + # Default is to use a neutral variety of English. + locale: US + # Default: [] + ignore-words: + - someword + godot: + # Comments to be checked: `declarations`, `toplevel`, or `all`. + # Default: declarations + scope: toplevel + # List of regexps for excluding particular comment lines from check. + # Default: [] + exclude: + # Exclude todo and fixme comments. + - "^fixme:" + - "^todo:" + # Check that each sentence ends with a period. + # Default: true + period: false + # Check that each sentence starts with a capital letter. + # Default: false + capital: false + dupl: + # Tokens count to trigger issue. + # Default: 150 + threshold: 100 cyclop: # The maximal code complexity to report. # Default: 10 @@ -76,6 +229,132 @@ linters-settings: # Minimal code complexity to report. # Default: 30 (but we recommend 10-20) min-complexity: 20 + gocritic: + # Which checks should be enabled; can't be combined with 'disabled-checks'. + # See https://go-critic.github.io/overview#checks-overview. + # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`. + # By default, list of stable checks is used. + enabled-checks: + - elseif + - nestingReduce + - unnamedResult + # - ruleguard + - truncateCmp + - hugeparam + - rangevalcopy + - captlocal + - underef + - toomanyresultschecker + - rangeexprcopy + # Which checks should be disabled; can't be combined with 'enabled-checks'. + # Default: [] + disabled-checks: + - regexpMust + # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. + # See https://github.com/go-critic/go-critic#usage -> section "Tags". + # Default: [] + enabled-tags: + - diagnostic + - style + - performance + - experimental + - opinionated + disabled-tags: + - diagnostic + - style + - performance + - experimental + - opinionated + # Settings passed to gocritic. + # The settings key is the name of a supported gocritic checker. + # The list of supported checkers can be find in https://go-critic.github.io/overview. + settings: + # Must be valid enabled check name. + captLocal: + # Whether to restrict checker to params only. + # Default: true + paramsOnly: false + elseif: + # Whether to skip balanced if-else pairs. + # Default: true + skipBalanced: false + hugeParam: + # Size in bytes that makes the warning trigger. + # Default: 80 + sizeThreshold: 70 + nestingReduce: + # Min number of statements inside a branch to trigger a warning. + # Default: 5 + bodyWidth: 4 + rangeExprCopy: + # Size in bytes that makes the warning trigger. + # Default: 512 + sizeThreshold: 516 + # Whether to check test functions + # Default: true + skipTestFuncs: false + rangeValCopy: + # Size in bytes that makes the warning trigger. + # Default: 128 + sizeThreshold: 32 + # Whether to check test functions. + # Default: true + skipTestFuncs: false + ruleguard: + # Enable debug to identify which 'Where' condition was rejected. + # The value of the parameter is the name of a function in a ruleguard file. + # + # When a rule is evaluated: + # If: + # The Match() clause is accepted; and + # One of the conditions in the Where() clause is rejected, + # Then: + # ruleguard prints the specific Where() condition that was rejected. + # + # The flag is passed to the ruleguard 'debug-group' argument. + # Default: "" + debug: 'emptyDecl' + # Deprecated, use 'failOn' param. + # If set to true, identical to failOn='all', otherwise failOn='' + failOnError: false + # Determines the behavior when an error occurs while parsing ruleguard files. + # If flag is not set, log error and skip rule files that contain an error. + # If flag is set, the value must be a comma-separated list of error conditions. + # - 'all': fail on all errors. + # - 'import': ruleguard rule imports a package that cannot be found. + # - 'dsl': gorule file does not comply with the ruleguard DSL. + # Default: "" + failOn: dsl + # Comma-separated list of file paths containing ruleguard rules. + # If a path is relative, it is relative to the directory where the golangci-lint command is executed. + # The special '${configDir}' variable is substituted with the absolute directory containing the golangci config file. + # Glob patterns such as 'rules-*.go' may be specified. + # Default: "" + rules: '${configDir}/ruleguard/rules-*.go,${configDir}/myrule1.go' + # Comma-separated list of enabled groups or skip empty to enable everything. + # Tags can be defined with # character prefix. + # Default: "" + enable: "myGroupName,#myTagName" + # Comma-separated list of disabled groups or skip empty to enable everything. + # Tags can be defined with # character prefix. + # Default: "" + disable: "myGroupName,#myTagName" + tooManyResultsChecker: + # Maximum number of results. + # Default: 5 + maxResults: 10 + truncateCmp: + # Whether to skip int/uint/uintptr types. + # Default: true + skipArchDependent: false + underef: + # Whether to skip (*x).method() calls where x is a pointer receiver. + # Default: true + skipRecvDeref: false + unnamedResult: + # Whether to check exported functions. + # Default: false + checkExported: true issues: exclude-rules: - path: _test\.go diff --git a/pitr/agent/Makefile b/pitr/agent/Makefile index 6f3a0ae0..7fd3b967 100644 --- a/pitr/agent/Makefile +++ b/pitr/agent/Makefile @@ -1,4 +1,4 @@ -.PHONY:openssl-local test build +.PHONY:openssl-local test build lint GOOS := $(shell go env GOOS) @@ -12,3 +12,5 @@ test: build: GOOS=$(GOOS) go build -o pitr-agent +lint: + golangci-lint run -v --timeout 5m diff --git a/pitr/agent/internal/cons/error.go b/pitr/agent/internal/cons/error.go index d9b2b2a1..bf4a9611 100644 --- a/pitr/agent/internal/cons/error.go +++ b/pitr/agent/internal/cons/error.go @@ -23,7 +23,7 @@ import ( var ( Internal = xerror.New(10000, "Internal error.") - InvalidHttpHeader = xerror.New(10001, "Invalid http header.") + InvalidHTTPHeader = xerror.New(10001, "Invalid http header.") DataNotFound = xerror.New(10002, "Data not found.") CmdOperateFailed = xerror.New(10003, "Command operate failed.") BackupPathAlreadyExist = xerror.New(10004, "The backup path already exists.") @@ -33,7 +33,7 @@ var ( StartOpenGaussFailed = xerror.New(10008, "Failed to start opengauss.") StopOpenGaussFailed = xerror.New(10009, "Failed to stop opengauss.") RestoreFailed = xerror.New(10010, "Failed to restore opengauss.") - InvalidDbPort = xerror.New(10011, "Invalid dn port.") + InvalidDBPort = xerror.New(10011, "Invalid dn port.") MissingUsername = xerror.New(10012, "Missing username") MissingPassword = xerror.New(10013, "Missing password.") MissingDnBackupPath = xerror.New(10014, "Missing dn backup path.") @@ -41,10 +41,10 @@ var ( MissingDnBackupMode = xerror.New(10016, "Missing dn backup mode.") InvalidDnBackupMode = xerror.New(10017, "Invalid dn backup mode.") MissingInstance = xerror.New(10018, "Missing instance.") - MissingDnBackupId = xerror.New(10019, "Missing dn backup id.") + MissingDnBackupID = xerror.New(10019, "Missing dn backup id.") BodyParseFailed = xerror.New(10020, "Invalid http request body.") - MissingDbName = xerror.New(10021, "Missing db name.") - DbConnectionFailed = xerror.New(10022, "Database connection failed.") + MissingDBName = xerror.New(10021, "Missing db name.") + DBConnectionFailed = xerror.New(10022, "Database connection failed.") UnmatchBackupID = xerror.New(10023, "Unmatch any backup id.") InvalidPgDataDir = xerror.New(10024, "Invalid PGDATA dir.") UnknownOgStatus = xerror.New(10025, "Unknown openGauss status.") diff --git a/pitr/agent/internal/handler/backup.go b/pitr/agent/internal/handler/backup.go index c58c8237..bb000f70 100644 --- a/pitr/agent/internal/handler/backup.go +++ b/pitr/agent/internal/handler/backup.go @@ -39,9 +39,9 @@ func Backup(ctx *fiber.Ctx) error { return fmt.Errorf("invalid parameter,err=%w", err) } - if err := pkg.OG.Auth(in.Username, in.Password, in.DbName, in.DbPort); err != nil { + if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil { efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w" - return fmt.Errorf(efmt, in.Username, len(in.Password), in.DbName, err) + return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err) } // try to add backup instance @@ -49,7 +49,7 @@ func Backup(ctx *fiber.Ctx) error { return fmt.Errorf("add instance failed, err=%w", err) } - backupID, err := pkg.OG.AsyncBackup(in.DnBackupPath, in.Instance, in.DnBackupMode, 1, in.DbPort) + backupID, err := pkg.OG.AsyncBackup(in.DnBackupPath, in.Instance, in.DnBackupMode, 1, in.DBPort) if err != nil { efmt := "pkg.OG.AsyncBackup[path=%s,instance=%s,mode=%s] failure,err=%w" return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupMode, err) diff --git a/pitr/agent/internal/handler/middleware/http_header.go b/pitr/agent/internal/handler/middleware/http_header.go index 09bd16eb..d41962bb 100644 --- a/pitr/agent/internal/handler/middleware/http_header.go +++ b/pitr/agent/internal/handler/middleware/http_header.go @@ -29,7 +29,7 @@ import ( func RequestIDChecker() fiber.Handler { return func(ctx *fiber.Ctx) error { if id := ctx.Get(cons.RequestID); strings.Trim(id, " ") == "" { - return responder.Error(ctx, cons.InvalidHttpHeader) + return responder.Error(ctx, cons.InvalidHTTPHeader) } return ctx.Next() } diff --git a/pitr/agent/internal/handler/middleware/logger.go b/pitr/agent/internal/handler/middleware/logger.go index fe8b062a..beac16fd 100644 --- a/pitr/agent/internal/handler/middleware/logger.go +++ b/pitr/agent/internal/handler/middleware/logger.go @@ -33,13 +33,14 @@ func Logger(log logging.ILog) fiber.Handler { start = time.Now() ) err := ctx.Next() + //nolint:exhaustive m := map[logging.FieldKey]string{ logging.Duration: fmt.Sprintf("%dms", time.Since(start).Milliseconds()), logging.Path: ctx.Route().Path, - logging.RequestUri: string(ctx.Request().RequestURI()), + logging.RequestURI: string(ctx.Request().RequestURI()), logging.RequestID: ctx.Get(cons.RequestID), - logging.HttpStatus: fmt.Sprintf("%d", ctx.Response().StatusCode()), - logging.HttpMethod: ctx.Method(), + logging.HTTPStatus: fmt.Sprintf("%d", ctx.Response().StatusCode()), + logging.HTTPMethod: ctx.Method(), } if err != nil { m[logging.ErrorKey] = err.Error() @@ -52,11 +53,12 @@ func Logger(log logging.ILog) fiber.Handler { // AccessLog logging Access log. func AccessLog(log logging.ILog) fiber.Handler { return func(ctx *fiber.Ctx) error { + //nolint:exhaustive log.Fields(map[logging.FieldKey]string{ logging.Path: ctx.Route().Path, - logging.RequestUri: string(ctx.Request().RequestURI()), + logging.RequestURI: string(ctx.Request().RequestURI()), logging.RequestID: ctx.Get(cons.RequestID), - logging.HttpMethod: ctx.Method(), + logging.HTTPMethod: ctx.Method(), }).Info("Access log") return ctx.Next() } diff --git a/pitr/agent/internal/handler/middleware/recovery.go b/pitr/agent/internal/handler/middleware/recovery.go index cd8acaf3..868b262d 100644 --- a/pitr/agent/internal/handler/middleware/recovery.go +++ b/pitr/agent/internal/handler/middleware/recovery.go @@ -32,6 +32,7 @@ func Recover(log logging.ILog) fiber.Handler { return func(ctx *fiber.Ctx) error { defer func() { if r := recover(); r != nil { + //nolint:exhaustive log.Fields(map[logging.FieldKey]string{ logging.RequestID: ctx.Get(cons.RequestID), logging.ErrorKey: fmt.Sprint(r), diff --git a/pitr/agent/internal/handler/middleware/uniform_err_resp.go b/pitr/agent/internal/handler/middleware/uniform_err_resp.go index e8b49583..06e8083b 100644 --- a/pitr/agent/internal/handler/middleware/uniform_err_resp.go +++ b/pitr/agent/internal/handler/middleware/uniform_err_resp.go @@ -32,6 +32,7 @@ func UniformErrResp(log logging.ILog) fiber.Handler { if err == nil { return nil } + //nolint:exhaustive log.Fields(map[logging.FieldKey]string{ logging.ErrorKey: err.Error(), logging.RequestID: ctx.Get(cons.RequestID), diff --git a/pitr/agent/internal/handler/restore.go b/pitr/agent/internal/handler/restore.go index 080e0e2a..32d20450 100644 --- a/pitr/agent/internal/handler/restore.go +++ b/pitr/agent/internal/handler/restore.go @@ -42,9 +42,9 @@ func Restore(ctx *fiber.Ctx) (err error) { return } - if err = pkg.OG.Auth(in.Username, in.Password, in.DbName, in.DbPort); err != nil { + if err = pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil { efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w" - err = fmt.Errorf(efmt, in.Username, len(in.Password), in.DbName, err) + err = fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err) return } @@ -68,9 +68,9 @@ func Restore(ctx *fiber.Ctx) (err error) { return } - if err = pkg.OG.Restore(in.DnBackupPath, in.Instance, in.DnBackupId); err != nil { + if err = pkg.OG.Restore(in.DnBackupPath, in.Instance, in.DnBackupID); err != nil { efmt := "pkg.OG.Restore failure[path=%s,instance=%s,backupID=%s],err=%w" - err = fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupId, err) + err = fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupID, err) err2 := pkg.OG.MvTempToPgData() err = fmt.Errorf("resotre failre[err=%s],pkg.OG.MvTempToPgData return err=%w", err, err2) diff --git a/pitr/agent/internal/handler/show.go b/pitr/agent/internal/handler/show.go index 076eb7f6..58d80923 100644 --- a/pitr/agent/internal/handler/show.go +++ b/pitr/agent/internal/handler/show.go @@ -19,6 +19,7 @@ package handler import ( "fmt" + "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg" "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/responder" @@ -40,15 +41,15 @@ func Show(ctx *fiber.Ctx) error { return fmt.Errorf("invalid parameter,err=%w", err) } - if err := pkg.OG.Auth(in.Username, in.Password, in.DbName, in.DbPort); err != nil { + if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil { efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w" - return fmt.Errorf(efmt, in.Username, len(in.Password), in.DbName, err) + return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err) } - data, err := pkg.OG.ShowBackup(in.DnBackupPath, in.Instance, in.DnBackupId) + data, err := pkg.OG.ShowBackup(in.DnBackupPath, in.Instance, in.DnBackupID) if err != nil { efmt := "pkg.OG.ShowBackupDetail failure[backupPath=%s,instance=%s,backupID=%s],err=%w" - return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupId, err) + return fmt.Errorf(efmt, in.DnBackupPath, in.Instance, in.DnBackupID, err) } return responder.Success(ctx, view.NewBackupInfo(data, in.DnBackupPath, in.Instance)) @@ -65,9 +66,9 @@ func ShowList(ctx *fiber.Ctx) error { return fmt.Errorf("invalid parameter,err=%w", err) } - if err := pkg.OG.Auth(in.Username, in.Password, in.DbName, in.DbPort); err != nil { + if err := pkg.OG.Auth(in.Username, in.Password, in.DBName, in.DBPort); err != nil { efmt := "pkg.OG.Auth failure[un=%s,pw.len=%d,db=%s],err=%w" - return fmt.Errorf(efmt, in.Username, len(in.Password), in.DbName, err) + return fmt.Errorf(efmt, in.Username, len(in.Password), in.DBName, err) } //Show list diff --git a/pitr/agent/internal/handler/view/backup.go b/pitr/agent/internal/handler/view/backup.go index b08952de..738078b7 100644 --- a/pitr/agent/internal/handler/view/backup.go +++ b/pitr/agent/internal/handler/view/backup.go @@ -21,8 +21,8 @@ import "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons" type ( BackupIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` @@ -42,12 +42,12 @@ func (in *BackupIn) Validate() error { return cons.Internal } - if in.DbPort == 0 { - return cons.InvalidDbPort + if in.DBPort == 0 { + return cons.InvalidDBPort } - if in.DbName == "" { - return cons.MissingDbName + if in.DBName == "" { + return cons.MissingDBName } if in.Username == "" { diff --git a/pitr/agent/internal/handler/view/restore.go b/pitr/agent/internal/handler/view/restore.go index 632db613..39a340cf 100644 --- a/pitr/agent/internal/handler/view/restore.go +++ b/pitr/agent/internal/handler/view/restore.go @@ -20,26 +20,27 @@ package view import "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons" type RestoreIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` Instance string `json:"instance"` DnBackupPath string `json:"dn_backup_path"` - DnBackupId string `json:"dn_backup_id"` + DnBackupID string `json:"dn_backup_id"` } +//nolint:dupl func (in *RestoreIn) Validate() error { if in == nil { return cons.Internal } - if in.DbPort == 0 { - return cons.InvalidDbPort + if in.DBPort == 0 { + return cons.InvalidDBPort } - if in.DbName == "" { - return cons.MissingDbName + if in.DBName == "" { + return cons.MissingDBName } if in.Username == "" { @@ -54,8 +55,8 @@ func (in *RestoreIn) Validate() error { return cons.MissingDnBackupPath } - if in.DnBackupId == "" { - return cons.MissingDnBackupId + if in.DnBackupID == "" { + return cons.MissingDnBackupID } if in.Instance == "" { diff --git a/pitr/agent/internal/handler/view/show.go b/pitr/agent/internal/handler/view/show.go index 022180cb..22434af5 100644 --- a/pitr/agent/internal/handler/view/show.go +++ b/pitr/agent/internal/handler/view/show.go @@ -24,18 +24,18 @@ import ( type ( ShowIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` - DnBackupId string `json:"dn_backup_id"` + DnBackupID string `json:"dn_backup_id"` DnBackupPath string `json:"dn_backup_path"` Instance string `json:"instance"` } ShowListIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` DnBackupPath string `json:"dn_backup_path"` @@ -43,7 +43,7 @@ type ( } BackupInfo struct { - Id string `json:"dn_backup_id"` + ID string `json:"dn_backup_id"` Path string `json:"dn_backup_path"` Mode string `json:"db_backup_mode"` Instance string `json:"instance"` @@ -53,17 +53,18 @@ type ( } ) +//nolint:dupl func (in *ShowIn) Validate() error { if in == nil { return cons.Internal } - if in.DbPort == 0 { - return cons.InvalidDbPort + if in.DBPort == 0 { + return cons.InvalidDBPort } - if in.DbName == "" { - return cons.MissingDbName + if in.DBName == "" { + return cons.MissingDBName } if in.Username == "" { @@ -78,8 +79,8 @@ func (in *ShowIn) Validate() error { return cons.MissingDnBackupPath } - if in.DnBackupId == "" { - return cons.MissingDnBackupId + if in.DnBackupID == "" { + return cons.MissingDnBackupID } if in.Instance == "" { @@ -93,7 +94,7 @@ func NewBackupInfo(data *model.Backup, path, instance string) *BackupInfo { return nil } return &BackupInfo{ - Id: data.ID, + ID: data.ID, Path: path, Mode: data.BackupMode, Instance: instance, @@ -103,14 +104,14 @@ func NewBackupInfo(data *model.Backup, path, instance string) *BackupInfo { } } -func NewBackupInfoList(list []model.Backup, path, instance string) []BackupInfo { +func NewBackupInfoList(list []*model.Backup, path, instance string) []BackupInfo { if len(list) == 0 { return []BackupInfo{} } ret := make([]BackupInfo, 0, len(list)) for _, v := range list { ret = append(ret, BackupInfo{ - Id: v.ID, + ID: v.ID, Path: path, Mode: v.BackupMode, Instance: instance, @@ -140,12 +141,12 @@ func (in *ShowListIn) Validate() error { return cons.Internal } - if in.DbPort == 0 { - return cons.InvalidDbPort + if in.DBPort == 0 { + return cons.InvalidDBPort } - if in.DbName == "" { - return cons.MissingDbName + if in.DBName == "" { + return cons.MissingDBName } if in.Username == "" { diff --git a/pitr/agent/internal/pkg/model/backup.go b/pitr/agent/internal/pkg/model/backup.go index 017a64d7..5672564b 100644 --- a/pitr/agent/internal/pkg/model/backup.go +++ b/pitr/agent/internal/pkg/model/backup.go @@ -48,7 +48,7 @@ type ( } BackupList struct { - Instance string `json:"instance"` - List []Backup `json:"backups"` + Instance string `json:"instance"` + List []*Backup `json:"backups"` } ) diff --git a/pitr/agent/internal/pkg/opengauss.go b/pitr/agent/internal/pkg/opengauss.go index 32b89706..3db2ff86 100644 --- a/pitr/agent/internal/pkg/opengauss.go +++ b/pitr/agent/internal/pkg/opengauss.go @@ -21,15 +21,14 @@ import ( "encoding/json" "errors" "fmt" - "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/gsutil" - "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging" "strings" - "github.com/dlclark/regexp2" - "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons" "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg/model" "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/cmds" + "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/gsutil" + "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging" + "github.com/dlclark/regexp2" ) type ( @@ -50,7 +49,7 @@ type ( Stop() error Status() (string, error) Restore(backupPath, instance, backupID string) error - ShowBackupList(backupPath, instanceName string) ([]model.Backup, error) + ShowBackupList(backupPath, instanceName string) ([]*model.Backup, error) Auth(user, password, dbName string, dbPort uint16) error MvTempToPgData() error MvPgDataToTemp() error @@ -115,9 +114,9 @@ func (og *openGauss) AsyncBackup(backupPath, instanceName, backupMode string, th if err != nil { return "", fmt.Errorf("og.getBackupID[source=%s] return err=%w", output.Message, err) } - //ignore other output + // ignore other output go og.ignore(outputs) - return bid, nil + return bid, nil //nolint } return "", fmt.Errorf("unknow err") } @@ -129,7 +128,7 @@ func (og *openGauss) ShowBackup(backupPath, instanceName, backupID string) (*mod return nil, fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err) } - var list []model.BackupList + var list []*model.BackupList if err = json.Unmarshal([]byte(output), &list); err != nil { return nil, fmt.Errorf("json.Unmarshal[output=%s] return err=%s,wrap=%w", output, err, cons.Internal) } @@ -140,7 +139,7 @@ func (og *openGauss) ShowBackup(backupPath, instanceName, backupID string) (*mod return nil, fmt.Errorf("instance[name=%s],backupList[v=%+v],err=%w", ins.Instance, list, cons.DataNotFound) } - return &ins.List[0], nil + return ins.List[0], nil } } @@ -278,6 +277,7 @@ func (og *openGauss) Restore(backupPath, instance, backupID string) error { outputs, err := cmds.AsyncExec(og.shell, cmd) for output := range outputs { og.log. + //nolint:exhaustive Fields(map[logging.FieldKey]string{ "backup_path": backupPath, "instance": instance, @@ -295,14 +295,14 @@ func (og *openGauss) Restore(backupPath, instance, backupID string) error { return nil } -func (og *openGauss) ShowBackupList(backupPath, instanceName string) ([]model.Backup, error) { +func (og *openGauss) ShowBackupList(backupPath, instanceName string) ([]*model.Backup, error) { cmd := fmt.Sprintf(_showListFmt, instanceName, backupPath) output, err := cmds.Exec(og.shell, cmd) if err != nil { return nil, fmt.Errorf("cmds.Exec[shell=%s,cmd=%s] return err=%w", og.shell, cmd, err) } - var list []model.BackupList + var list []*model.BackupList if err = json.Unmarshal([]byte(output), &list); err != nil { return nil, fmt.Errorf("json.Unmarshal[output=%s] return err=%s,wrap=%w", output, err, cons.Internal) } diff --git a/pitr/agent/internal/pkg/opengauss_test.go b/pitr/agent/internal/pkg/opengauss_test.go index ac511540..19f8b150 100644 --- a/pitr/agent/internal/pkg/opengauss_test.go +++ b/pitr/agent/internal/pkg/opengauss_test.go @@ -49,6 +49,7 @@ var _ = Describe("OpenGauss,requires opengauss environment", func() { instance, "full", 1, + 3306, ) Expect(err).To(BeNil()) diff --git a/pitr/agent/main.go b/pitr/agent/main.go index 56f0e024..b737a4cf 100644 --- a/pitr/agent/main.go +++ b/pitr/agent/main.go @@ -20,6 +20,11 @@ package main import ( "flag" "fmt" + "os" + "os/signal" + "strings" + "syscall" + "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler" "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/handler/middleware" "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/pkg" @@ -28,10 +33,6 @@ import ( "github.com/gofiber/fiber/v2" "go.uber.org/zap" "go.uber.org/zap/zapcore" - "os" - "os/signal" - "strings" - "syscall" ) const ( @@ -112,7 +113,7 @@ func main() { zap.AddStacktrace(zapcore.FatalLevel), ) if err != nil { - panic(fmt.Errorf("an unknown error occured in the zap-log")) + panic(fmt.Errorf("an unknown error occurred in the zap-log")) } log = logging.Init(logger) diff --git a/pitr/agent/pkg/cmds/cmd.go b/pitr/agent/pkg/cmds/cmd.go index dc248d2f..47200200 100644 --- a/pitr/agent/pkg/cmds/cmd.go +++ b/pitr/agent/pkg/cmds/cmd.go @@ -20,11 +20,11 @@ package cmds import ( "bufio" "fmt" - "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging" "io" "os/exec" "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons" + "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/logging" "github.com/apache/shardingsphere-on-cloud/pitr/agent/pkg/syncutils" ) diff --git a/pitr/agent/pkg/gsutil/conn.go b/pitr/agent/pkg/gsutil/conn.go index 4dcbdf8b..72a6e8aa 100644 --- a/pitr/agent/pkg/gsutil/conn.go +++ b/pitr/agent/pkg/gsutil/conn.go @@ -20,9 +20,10 @@ package gsutil import ( "database/sql" "fmt" + "strings" + _ "gitee.com/opengauss/openGauss-connector-go-pq" "github.com/apache/shardingsphere-on-cloud/pitr/agent/internal/cons" - "strings" ) const defaultOGHost = "127.0.0.1" @@ -49,7 +50,7 @@ func Open(user, password, dbName string, dbPort uint16) (*OpenGauss, error) { db, err := sql.Open("opengauss", fmt.Sprintf(connStr, defaultOGHost, dbPort, user, password, dbName)) if err != nil { efmt := "sql:open fail[user=%s,pwLen=%d,dbName=%s],err=%s,wrap=%w" - return nil, fmt.Errorf(efmt, user, len(password), dbName, err, cons.DbConnectionFailed) + return nil, fmt.Errorf(efmt, user, len(password), dbName, err, cons.DBConnectionFailed) } return &OpenGauss{ @@ -63,7 +64,7 @@ func Open(user, password, dbName string, dbPort uint16) (*OpenGauss, error) { func (og *OpenGauss) Ping() error { if err := og.db.Ping(); err != nil { efmt := "db ping fail[user=%s,pwLen=%d,dbName=%s],err=%s,wrap=%w" - return fmt.Errorf(efmt, og.user, og.pwLen, og.dbName, err, cons.DbConnectionFailed) + return fmt.Errorf(efmt, og.user, og.pwLen, og.dbName, err, cons.DBConnectionFailed) } return nil } diff --git a/pitr/agent/pkg/logging/field.go b/pitr/agent/pkg/logging/field.go index 21192785..e8d9e3e1 100644 --- a/pitr/agent/pkg/logging/field.go +++ b/pitr/agent/pkg/logging/field.go @@ -40,7 +40,7 @@ const ( Stack FieldKey = "stack" Duration FieldKey = "duration" Path FieldKey = "path" // original routing path - RequestUri FieldKey = "requestUri" // http requesting uri - HttpMethod FieldKey = "httpMethod" - HttpStatus FieldKey = "httpStatus" + RequestURI FieldKey = "requestUri" // http requesting uri + HTTPMethod FieldKey = "httpMethod" + HTTPStatus FieldKey = "httpStatus" ) diff --git a/pitr/agent/pkg/logging/log.go b/pitr/agent/pkg/logging/log.go index b2d2209a..0505bd4e 100644 --- a/pitr/agent/pkg/logging/log.go +++ b/pitr/agent/pkg/logging/log.go @@ -31,6 +31,7 @@ func Init(logger *zap.Logger) ILog { } func Field(k FieldKey, v string) ILog { + //nolint:exhaustive m := map[FieldKey]string{k: v} for k, v := range l.fields { m[k] = v diff --git a/pitr/agent/pkg/logging/zap_log.go b/pitr/agent/pkg/logging/zap_log.go index cfda378c..f0dd89b6 100644 --- a/pitr/agent/pkg/logging/zap_log.go +++ b/pitr/agent/pkg/logging/zap_log.go @@ -32,6 +32,7 @@ func NewLog(l *zap.Logger) ILog { } func (z *ZapLogger) Field(k FieldKey, v string) ILog { + //nolint:exhaustive m := map[FieldKey]string{k: v} for k, v := range z.fields { m[k] = v diff --git a/pitr/agent/pkg/strutil/rand_string.go b/pitr/agent/pkg/strutil/rand_string.go index 769b7b74..53056bb6 100644 --- a/pitr/agent/pkg/strutil/rand_string.go +++ b/pitr/agent/pkg/strutil/rand_string.go @@ -18,9 +18,8 @@ package strutil import ( - "math/rand" + "crypto/rand" "strconv" - "time" ) const ( @@ -34,20 +33,24 @@ const ( ) func Random(n uint) string { - rand.Seed(time.Now().UnixNano()) - bs := make([]byte, 0, n) + bs := make([]byte, n) for i := uint(0); i < n; i++ { - bs = append(bs, charSet[rand.Intn(charSize)]) + _, _ = rand.Read(bs[i : i+1]) + } + for k, v := range bs { + bs[k] = charSet[v%byte(charSize)] } return string(bs) } func RandomInt(n uint) int64 { - rand.Seed(time.Now().UnixNano()) - bs := make([]byte, 0, n) - bs = append(bs, digitSet[rand.Intn(digitSize-1)]) - for i := uint(0); i < n-1; i++ { - bs = append(bs, digitSet[rand.Intn(digitSize)]) + bs := make([]byte, n) + _, _ = rand.Read(bs[0:1]) + for i := uint(1); i < n; i++ { + _, _ = rand.Read(bs[i : i+1]) + } + for k, v := range bs { + bs[k] = digitSet[v%byte(digitSize)] } v, _ := strconv.ParseInt(string(bs), 10, 64) return v diff --git a/pitr/agent/pkg/syncutils/recover_func.go b/pitr/agent/pkg/syncutils/recover_func.go index af49a224..147cd7ea 100644 --- a/pitr/agent/pkg/syncutils/recover_func.go +++ b/pitr/agent/pkg/syncutils/recover_func.go @@ -27,9 +27,9 @@ func NewRecoverFuncWithErrRet(msg string, fn func() error) func() (err error) { r := recover() if r != nil { if err, ok := r.(error); ok { - err = fmt.Errorf("NewRecoverFuncWithErrRet[msg=%s],err=%s", msg, err) + err = fmt.Errorf("NewRecoverFuncWithErrRet[msg=%s],err=%s", msg, err) //nolint } else { - err = fmt.Errorf("NewRecoverFuncWithErrRet[msg=%s],recover msg=%+v", msg, r) + err = fmt.Errorf("NewRecoverFuncWithErrRet[msg=%s],recover msg=%+v", msg, r) //nolint } } }() diff --git a/pitr/cli/.golangci.yml b/pitr/cli/.golangci.yml index 1b9b6ce0..5b6aed24 100644 --- a/pitr/cli/.golangci.yml +++ b/pitr/cli/.golangci.yml @@ -17,32 +17,186 @@ run: timeout: 10m + skip-files: + - "^zz_generated.*" + - "_test.go" linters: disable-all: true enable: + # The base lints + - errcheck + - gosimple + - govet - ineffassign + - staticcheck - typecheck - - varcheck - unused - - structcheck - - deadcode - - gosimple + - unused + - bodyclose + - cyclop + - nilerr - goimports - - errcheck - - staticcheck - - stylecheck - - gosec - asciicheck - - bodyclose + - prealloc + - stylecheck - exportloopref - rowserrcheck - makezero - durationcheck - - prealloc + - gosec - predeclared + # Deprecated lints + - structcheck + - varcheck + - deadcode + # The advanced lints + - dupl + - exhaustive + - godot + - misspell + # - varnamelen + - gocritic + #- exhaustruct + #- nestif + #- wsl + - gocognit # Refers: https://gist.github.com/maratori/47a4d00457a92aa426dbd48a18776322 linters-settings: + wsl: + # See https://github.com/bombsimon/wsl/blob/master/doc/configuration.md for documentation of available settings. + # These are the defaults for `golangci-lint`. + + # Do strict checking when assigning from append (x = append(x, y)). If + # this is set to true - the append call must append either a variable + # assigned, called or used on the line above. + strict-append: true + # Allows assignments to be cuddled with variables used in calls on + # line above and calls to be cuddled with assignments of variables + # used in call on line above. + allow-assign-and-call: true + # Allows assignments to be cuddled with anything. + allow-assign-and-anything: false + # Allows cuddling to assignments even if they span over multiple lines. + allow-multiline-assign: true + # If the number of lines in a case block is equal to or lager than this + # number, the case *must* end white a newline. + force-case-trailing-whitespace: 0 + # Allow blocks to end with comments. + allow-trailing-comment: false + # Allow multiple comments in the beginning of a block separated with newline. + allow-separated-leading-comment: false + # Allow multiple var/declaration statements to be cuddled. + allow-cuddle-declarations: false + # A list of call idents that everything can be cuddled with. + # Defaults to calls looking like locks. + allow-cuddle-with-calls: ["Lock", "RLock"] + # AllowCuddleWithRHS is a list of right hand side variables that is allowed + # to be cuddled with anything. Defaults to assignments or calls looking + # like unlocks. + allow-cuddle-with-rhs: ["Unlock", "RUnlock"] + # Causes an error when an If statement that checks an error variable doesn't + # cuddle with the assignment of that variable. + force-err-cuddling: false + # When force-err-cuddling is enabled this is a list of names + # used for error variables to check for in the conditional. + error-variable-names: ["err"] + # Causes an error if a short declaration (:=) cuddles with anything other than + # another short declaration. + # This logic overrides force-err-cuddling among others. + force-short-decl-cuddling: false + varnamelen: + # The longest distance, in source lines, that is being considered a "small scope". + # Variables used in at most this many lines will be ignored. + # Default: 5 + max-distance: 6 + # The minimum length of a variable's name that is considered "long". + # Variable names that are at least this long will be ignored. + # Default: 3 + min-name-length: 2 + # Check method receivers. + # Default: false + check-receiver: false + # Check named return values. + # Default: false + check-return: true + # Check type parameters. + # Default: false + check-type-param: true + # Ignore "ok" variables that hold the bool return value of a type assertion. + # Default: false + ignore-type-assert-ok: true + # Ignore "ok" variables that hold the bool return value of a map index. + # Default: false + ignore-map-index-ok: true + # Ignore "ok" variables that hold the bool return value of a channel receive. + # Default: false + ignore-chan-recv-ok: true + # Optional list of variable names that should be ignored completely. + # Default: [] + ignore-names: + - err + # Optional list of variable declarations that should be ignored completely. + # Entries must be in one of the following forms (see below for examples): + # - for variables, parameters, named return values, method receivers, or type parameters: + # ( can also be a pointer/slice/map/chan/...) + # - for constants: const + # + # Default: [] + ignore-decls: + - c echo.Context + - t testing.T + - f *foo.Bar + - e error + - i int + - const C + - T any + - m map[string]int + prealloc: + # IMPORTANT: we don't recommend using this linter before doing performance profiling. + # For most programs usage of prealloc will be a premature optimization. + + # Report pre-allocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them. + # Default: true + simple: false + # Report pre-allocation suggestions on range loops. + # Default: true + range-loops: false + # Report pre-allocation suggestions on for loops. + # Default: false + for-loops: true + #nestif: + # Minimal complexity of if statements to report. + # Default: 5 + # min-complexity: 4 + misspell: + # Correct spellings using locale preferences for US or UK. + # Setting locale to US will correct the British spelling of 'colour' to 'color'. + # Default is to use a neutral variety of English. + locale: US + # Default: [] + ignore-words: + - someword + godot: + # Comments to be checked: `declarations`, `toplevel`, or `all`. + # Default: declarations + scope: toplevel + # List of regexps for excluding particular comment lines from check. + # Default: [] + exclude: + # Exclude todo and fixme comments. + - "^fixme:" + - "^todo:" + # Check that each sentence ends with a period. + # Default: true + period: false + # Check that each sentence starts with a capital letter. + # Default: false + capital: false + dupl: + # Tokens count to trigger issue. + # Default: 150 + threshold: 100 cyclop: # The maximal code complexity to report. # Default: 10 @@ -75,6 +229,132 @@ linters-settings: # Minimal code complexity to report. # Default: 30 (but we recommend 10-20) min-complexity: 20 + gocritic: + # Which checks should be enabled; can't be combined with 'disabled-checks'. + # See https://go-critic.github.io/overview#checks-overview. + # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`. + # By default, list of stable checks is used. + enabled-checks: + - elseif + - nestingReduce + - unnamedResult + # - ruleguard + - truncateCmp + - hugeparam + - rangevalcopy + - captlocal + - underef + - toomanyresultschecker + - rangeexprcopy + # Which checks should be disabled; can't be combined with 'enabled-checks'. + # Default: [] + disabled-checks: + - regexpMust + # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. + # See https://github.com/go-critic/go-critic#usage -> section "Tags". + # Default: [] + enabled-tags: + - diagnostic + - style + - performance + - experimental + - opinionated + disabled-tags: + - diagnostic + - style + - performance + - experimental + - opinionated + # Settings passed to gocritic. + # The settings key is the name of a supported gocritic checker. + # The list of supported checkers can be find in https://go-critic.github.io/overview. + settings: + # Must be valid enabled check name. + captLocal: + # Whether to restrict checker to params only. + # Default: true + paramsOnly: false + elseif: + # Whether to skip balanced if-else pairs. + # Default: true + skipBalanced: false + hugeParam: + # Size in bytes that makes the warning trigger. + # Default: 80 + sizeThreshold: 70 + nestingReduce: + # Min number of statements inside a branch to trigger a warning. + # Default: 5 + bodyWidth: 4 + rangeExprCopy: + # Size in bytes that makes the warning trigger. + # Default: 512 + sizeThreshold: 516 + # Whether to check test functions + # Default: true + skipTestFuncs: false + rangeValCopy: + # Size in bytes that makes the warning trigger. + # Default: 128 + sizeThreshold: 32 + # Whether to check test functions. + # Default: true + skipTestFuncs: false + ruleguard: + # Enable debug to identify which 'Where' condition was rejected. + # The value of the parameter is the name of a function in a ruleguard file. + # + # When a rule is evaluated: + # If: + # The Match() clause is accepted; and + # One of the conditions in the Where() clause is rejected, + # Then: + # ruleguard prints the specific Where() condition that was rejected. + # + # The flag is passed to the ruleguard 'debug-group' argument. + # Default: "" + debug: 'emptyDecl' + # Deprecated, use 'failOn' param. + # If set to true, identical to failOn='all', otherwise failOn='' + failOnError: false + # Determines the behavior when an error occurs while parsing ruleguard files. + # If flag is not set, log error and skip rule files that contain an error. + # If flag is set, the value must be a comma-separated list of error conditions. + # - 'all': fail on all errors. + # - 'import': ruleguard rule imports a package that cannot be found. + # - 'dsl': gorule file does not comply with the ruleguard DSL. + # Default: "" + failOn: dsl + # Comma-separated list of file paths containing ruleguard rules. + # If a path is relative, it is relative to the directory where the golangci-lint command is executed. + # The special '${configDir}' variable is substituted with the absolute directory containing the golangci config file. + # Glob patterns such as 'rules-*.go' may be specified. + # Default: "" + rules: '${configDir}/ruleguard/rules-*.go,${configDir}/myrule1.go' + # Comma-separated list of enabled groups or skip empty to enable everything. + # Tags can be defined with # character prefix. + # Default: "" + enable: "myGroupName,#myTagName" + # Comma-separated list of disabled groups or skip empty to enable everything. + # Tags can be defined with # character prefix. + # Default: "" + disable: "myGroupName,#myTagName" + tooManyResultsChecker: + # Maximum number of results. + # Default: 5 + maxResults: 10 + truncateCmp: + # Whether to skip int/uint/uintptr types. + # Default: true + skipArchDependent: false + underef: + # Whether to skip (*x).method() calls where x is a pointer receiver. + # Default: true + skipRecvDeref: false + unnamedResult: + # Whether to check exported functions. + # Default: false + checkExported: true issues: exclude-rules: - path: _test\.go diff --git a/pitr/cli/Makefile b/pitr/cli/Makefile index 5a74176d..d6b27df6 100644 --- a/pitr/cli/Makefile +++ b/pitr/cli/Makefile @@ -1,4 +1,4 @@ -.PHONY: build test +.PHONY: build test cover lint GOOS := $(shell go env GOOS) @@ -8,3 +8,5 @@ test: go test -v ./... -cover -coverprofile cover.out cover: go tool cover -html cover.out +lint: + golangci-lint run -v --timeout 5m diff --git a/pitr/cli/internal/cmd/backup.go b/pitr/cli/internal/cmd/backup.go index 8d3b3a9a..5681fd82 100644 --- a/pitr/cli/internal/cmd/backup.go +++ b/pitr/cli/internal/cmd/backup.go @@ -100,7 +100,7 @@ func init() { // 7. Double check backups all finished func backup() error { var err error - proxy, err := pkg.NewShardingSphereProxy(Username, Password, pkg.DefaultDbName, Host, Port) + proxy, err := pkg.NewShardingSphereProxy(Username, Password, pkg.DefaultDBName, Host, Port) if err != nil { return xerr.NewCliErr("create ss-proxy connect failed") } @@ -255,8 +255,8 @@ func execBackup(lsBackup *model.LsBackup) error { func _execBackup(as pkg.IAgentServer, node *model.StorageNode, dnCh chan *model.DataNode) error { in := &model.BackupIn{ - DbPort: node.Port, - DbName: node.Database, + DBPort: node.Port, + DBName: node.Database, Username: node.Username, Password: node.Password, DnBackupPath: BackupPath, @@ -331,7 +331,7 @@ func checkBackupStatus(lsBackup *model.LsBackup) model.BackupStatus { return backupFinalStatus } -func checkStatus(as pkg.IAgentServer, sn *model.StorageNode, backupId string, status model.BackupStatus, retryTimes uint8) model.BackupStatus { +func checkStatus(as pkg.IAgentServer, sn *model.StorageNode, backupID string, status model.BackupStatus, retryTimes uint8) model.BackupStatus { if retryTimes+1 == 0 { return status } @@ -343,18 +343,18 @@ func checkStatus(as pkg.IAgentServer, sn *model.StorageNode, backupId string, st time.Sleep(time.Second * 2) in := &model.ShowDetailIn{ - DbPort: sn.Port, - DbName: sn.Database, + DBPort: sn.Port, + DBName: sn.Database, Username: sn.Username, Password: sn.Password, - DnBackupId: backupId, + DnBackupID: backupID, DnBackupPath: BackupPath, Instance: defaultInstance, } backupInfo, err := as.ShowDetail(in) if err != nil { logging.Error(fmt.Sprintf("get storage node [IP:%s] backup detail from agent server failed, will retry %d times.\n%s", sn.IP, retryTimes, err.Error())) - return checkStatus(as, sn, backupId, model.SsBackupStatusCheckError, retryTimes-1) + return checkStatus(as, sn, backupID, model.SsBackupStatusCheckError, retryTimes-1) } - return checkStatus(as, sn, backupId, backupInfo.Status, retryTimes) + return checkStatus(as, sn, backupID, backupInfo.Status, retryTimes) } diff --git a/pitr/cli/internal/cmd/restore.go b/pitr/cli/internal/cmd/restore.go index 13ed7d1b..493ea614 100644 --- a/pitr/cli/internal/cmd/restore.go +++ b/pitr/cli/internal/cmd/restore.go @@ -89,7 +89,7 @@ func restore() error { if err != nil { return xerr.NewCliErr(fmt.Sprintf("new local storage failed, err:%s", err.Error())) } - proxy, err := pkg.NewShardingSphereProxy(Username, Password, pkg.DefaultDbName, Host, Port) + proxy, err := pkg.NewShardingSphereProxy(Username, Password, pkg.DefaultDBName, Host, Port) if err != nil { return xerr.NewCliErr(fmt.Sprintf("new ss-proxy failed, err:%s", err.Error())) } @@ -140,7 +140,7 @@ func checkDatabaseExist(proxy pkg.IShardingSphereProxy, bak *model.LsBackup) err return xerr.NewCliErr(fmt.Sprintf("get cluster metadata failed:%s", err.Error())) } - for k, _ := range bak.SsBackup.ClusterInfo.MetaData.Databases { + for k := range bak.SsBackup.ClusterInfo.MetaData.Databases { if _, ok := clusterNow.MetaData.Databases[k]; ok { databaseNamesExist = append(databaseNamesExist, k) } @@ -211,13 +211,13 @@ func execRestore(lsBackup *model.LsBackup) error { func _execRestore(as pkg.IAgentServer, node *model.StorageNode, backupID string, failedCh chan error) { in := &model.RestoreIn{ - DbPort: node.Port, - DbName: node.Database, + DBPort: node.Port, + DBName: node.Database, Username: node.Username, Password: node.Password, Instance: defaultInstance, DnBackupPath: BackupPath, - DnBackupId: backupID, + DnBackupID: backupID, } if err := as.Restore(in); err != nil { diff --git a/pitr/cli/internal/cmd/restore_test.go b/pitr/cli/internal/cmd/restore_test.go index 297531e8..40194eec 100644 --- a/pitr/cli/internal/cmd/restore_test.go +++ b/pitr/cli/internal/cmd/restore_test.go @@ -41,7 +41,7 @@ var _ = Describe("Restore", func() { backupRecordID string ) Context("Restore", func() { - proxy, _ := pkg.NewShardingSphereProxy(user, password, pkg.DefaultDbName, host, port) + proxy, _ := pkg.NewShardingSphereProxy(user, password, pkg.DefaultDBName, host, port) ls, _ := pkg.NewLocalStorage(pkg.DefaultRootDir()) bak, _ := ls.ReadByID(backupRecordID) It("restore to ss proxy should be ok", func() { diff --git a/pitr/cli/internal/cmd/show.go b/pitr/cli/internal/cmd/show.go index b1943789..045b4f74 100644 --- a/pitr/cli/internal/cmd/show.go +++ b/pitr/cli/internal/cmd/show.go @@ -66,7 +66,7 @@ func show() error { return nil } - if err := formatRecord([]model.LsBackup{*bak}); err != nil { + if err := formatRecord([]*model.LsBackup{bak}); err != nil { return err } return nil @@ -83,7 +83,7 @@ func show() error { return nil } - if err := formatRecord([]model.LsBackup{*bak}); err != nil { + if err := formatRecord([]*model.LsBackup{bak}); err != nil { return err } return nil @@ -106,7 +106,7 @@ func show() error { return nil } -func formatRecord(backups []model.LsBackup, mode ...string) error { +func formatRecord(backups []*model.LsBackup, mode ...string) error { var m string if len(mode) == 0 { @@ -116,16 +116,16 @@ func formatRecord(backups []model.LsBackup, mode ...string) error { } switch m { case "json": - return formatRecordJson(backups) + return formatRecordJSON(backups) case "table": // TODO format record table return nil default: - return formatRecordJson(backups) + return formatRecordJSON(backups) } } -func formatRecordJson(backups []model.LsBackup) error { +func formatRecordJSON(backups []*model.LsBackup) error { var ds []string for _, backup := range backups { data, err := json.MarshalIndent(backup, "", "\t") diff --git a/pitr/cli/internal/cmd/show_test.go b/pitr/cli/internal/cmd/show_test.go index b4c410f9..96176922 100644 --- a/pitr/cli/internal/cmd/show_test.go +++ b/pitr/cli/internal/cmd/show_test.go @@ -91,7 +91,7 @@ var _ = Describe("Show", func() { }, }, } - err := formatRecord([]model.LsBackup{*bak, *bak}) + err := formatRecord([]*model.LsBackup{bak, bak}) Expect(err).To(BeNil()) }) }) @@ -118,7 +118,7 @@ var _ = Describe("Show", func() { It("no record", func() { CSN = "" RecordID = "" - ls.EXPECT().ReadAll().Return([]model.LsBackup{}, nil) + ls.EXPECT().ReadAll().Return([]*model.LsBackup{}, nil) Expect(show()).To(BeNil()) }) diff --git a/pitr/cli/internal/pkg/agent-server.go b/pitr/cli/internal/pkg/agent-server.go index 5f10e576..393d14fc 100644 --- a/pitr/cli/internal/pkg/agent-server.go +++ b/pitr/cli/internal/pkg/agent-server.go @@ -65,6 +65,7 @@ func (as *agentServer) Backup(in *model.BackupIn) (string, error) { "content-type": "application/json", }) r.Body(in) + httpCode, err := r.Send(out) if err != nil { efmt := "httputils.NewRequest[url=%s,body=%v,out=%v] return err=%s,wrap=%w" @@ -72,7 +73,7 @@ func (as *agentServer) Backup(in *model.BackupIn) (string, error) { } if httpCode != http.StatusOK { - return "", fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHttpStatus)) + return "", fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHTTPStatus)) } if out.Code != 0 { @@ -100,7 +101,7 @@ func (as *agentServer) Restore(in *model.RestoreIn) error { } if httpCode != http.StatusOK { - return fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHttpStatus)) + return fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHTTPStatus)) } if out.Code != 0 { @@ -128,7 +129,7 @@ func (as *agentServer) ShowDetail(in *model.ShowDetailIn) (*model.BackupInfo, er } if httpCode != http.StatusOK { - return nil, fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHttpStatus)) + return nil, fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHTTPStatus)) } if out.Code != 0 { @@ -156,7 +157,7 @@ func (as *agentServer) ShowList(in *model.ShowListIn) ([]model.BackupInfo, error } if httpCode != http.StatusOK { - return nil, fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHttpStatus)) + return nil, fmt.Errorf("unknown http status[code=%d],err=%w", httpCode, xerr.NewCliErr(xerr.InvalidHTTPStatus)) } if out.Code != 0 { diff --git a/pitr/cli/internal/pkg/agent-server_test.go b/pitr/cli/internal/pkg/agent-server_test.go index 2cc9a35a..1bf05f21 100644 --- a/pitr/cli/internal/pkg/agent-server_test.go +++ b/pitr/cli/internal/pkg/agent-server_test.go @@ -38,8 +38,8 @@ func TestAgentServer_Backup(t *testing.T) { as := NewAgentServer("http://agent-server:18080") backupID, err := as.Backup(&model.BackupIn{ - DbPort: 5432, - DbName: "omm", + DBPort: 5432, + DBName: "omm", Username: "og", Password: "1234567890@SphereEx", DnBackupPath: "/home/omm/data", @@ -59,13 +59,13 @@ func TestAgentServer_Restore(t *testing.T) { as := NewAgentServer("http://agent-server:18080") err := as.Restore(&model.RestoreIn{ - DbPort: 5432, - DbName: "omm", + DBPort: 5432, + DBName: "omm", Username: "og", Password: "1234567890@SphereEx", DnBackupPath: "/home/omm/data", Instance: "ins-default-0", - DnBackupId: "RR3FIC", + DnBackupID: "RR3FIC", }) if err != nil { panic(err) @@ -79,13 +79,13 @@ func TestAgentServer_ShowDetail(t *testing.T) { as := NewAgentServer("http://agent-server:18080") backupInfo, err := as.ShowDetail(&model.ShowDetailIn{ - DbPort: 5432, - DbName: "omm", + DBPort: 5432, + DBName: "omm", Username: "og", Password: "1234567890@SphereEx", DnBackupPath: "/home/omm/data", Instance: "ins-default-0", - DnBackupId: "RR3FIC", + DnBackupID: "RR3FIC", }) if err != nil { panic(err) @@ -104,8 +104,8 @@ func TestAgentServer_ShowList(t *testing.T) { as := NewAgentServer("http://agent-server:18080") list, err := as.ShowList(&model.ShowListIn{ - DbPort: 5432, - DbName: "omm", + DBPort: 5432, + DBName: "omm", Username: "og", Password: "1234567890@SphereEx", DnBackupPath: "/home/omm/data", diff --git a/pitr/cli/internal/pkg/local-storage.go b/pitr/cli/internal/pkg/local-storage.go index c7b70291..e44c98a6 100644 --- a/pitr/cli/internal/pkg/local-storage.go +++ b/pitr/cli/internal/pkg/local-storage.go @@ -40,7 +40,7 @@ type ( ILocalStorage interface { WriteByJSON(name string, contents *model.LsBackup) error GenFilename(extn Extension) string - ReadAll() ([]model.LsBackup, error) + ReadAll() ([]*model.LsBackup, error) ReadByID(id string) (*model.LsBackup, error) ReadByCSN(csn string) (*model.LsBackup, error) } @@ -129,12 +129,14 @@ func (ls *localStorage) WriteByJSON(name string, contents *model.LsBackup) error return nil } -func (ls *localStorage) ReadAll() ([]model.LsBackup, error) { +func (ls *localStorage) ReadAll() ([]*model.LsBackup, error) { entries, err := os.ReadDir(ls.backupDir) if err != nil { return nil, xerr.NewCliErr(fmt.Sprintf("read the dir[path:%s] failed,err=%s", ls.backupDir, err)) } - backups := make([]model.LsBackup, 0, len(entries)) + + backups := make([]*model.LsBackup, 0, len(entries)) + for _, entry := range entries { if entry.IsDir() { continue @@ -157,10 +159,11 @@ func (ls *localStorage) ReadAll() ([]model.LsBackup, error) { return nil, xerr.NewCliErr(fmt.Sprintf("read file failed,err=%s", err)) } - b := model.LsBackup{} - if err := json.Unmarshal(file, &b); err != nil { + b := &model.LsBackup{} + if err := json.Unmarshal(file, b); err != nil { return nil, xerr.NewCliErr(fmt.Sprintf("invalid contents[filePath=%s],err=%s", path, err)) } + backups = append(backups, b) } return backups, nil @@ -173,7 +176,7 @@ func (ls *localStorage) ReadByCSN(csn string) (*model.LsBackup, error) { } for _, v := range list { if v.Info.CSN == csn { - return &v, nil + return v, nil } } return nil, xerr.NewCliErr(xerr.NotFound) @@ -186,7 +189,7 @@ func (ls *localStorage) ReadByID(id string) (*model.LsBackup, error) { } for _, v := range list { if v.Info.ID == id { - return &v, nil + return v, nil } } return nil, xerr.NewCliErr(xerr.NotFound) diff --git a/pitr/cli/internal/pkg/mocks/local-storage.go b/pitr/cli/internal/pkg/mocks/local-storage.go index 7445096e..aa182610 100644 --- a/pitr/cli/internal/pkg/mocks/local-storage.go +++ b/pitr/cli/internal/pkg/mocks/local-storage.go @@ -67,10 +67,10 @@ func (mr *MockILocalStorageMockRecorder) GenFilename(extn interface{}) *gomock.C } // ReadAll mocks base method. -func (m *MockILocalStorage) ReadAll() ([]model.LsBackup, error) { +func (m *MockILocalStorage) ReadAll() ([]*model.LsBackup, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ReadAll") - ret0, _ := ret[0].([]model.LsBackup) + ret0, _ := ret[0].([]*model.LsBackup) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/pitr/cli/internal/pkg/model/as_backup.go b/pitr/cli/internal/pkg/model/as_backup.go index 1f5cc351..63c45ef2 100644 --- a/pitr/cli/internal/pkg/model/as_backup.go +++ b/pitr/cli/internal/pkg/model/as_backup.go @@ -19,8 +19,8 @@ package model type ( BackupIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` diff --git a/pitr/cli/internal/pkg/model/as_restore.go b/pitr/cli/internal/pkg/model/as_restore.go index 015c871f..2381de7b 100644 --- a/pitr/cli/internal/pkg/model/as_restore.go +++ b/pitr/cli/internal/pkg/model/as_restore.go @@ -19,13 +19,13 @@ package model type ( RestoreIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` Instance string `json:"instance"` DnBackupPath string `json:"dn_backup_path"` - DnBackupId string `json:"dn_backup_id"` + DnBackupID string `json:"dn_backup_id"` } RestoreResp struct { diff --git a/pitr/cli/internal/pkg/model/as_show.go b/pitr/cli/internal/pkg/model/as_show.go index 8fefabfe..f75c2a7a 100644 --- a/pitr/cli/internal/pkg/model/as_show.go +++ b/pitr/cli/internal/pkg/model/as_show.go @@ -19,18 +19,18 @@ package model type ( ShowDetailIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` - DnBackupId string `json:"dn_backup_id"` + DnBackupID string `json:"dn_backup_id"` DnBackupPath string `json:"dn_backup_path"` Instance string `json:"instance"` } ShowListIn struct { - DbPort uint16 `json:"db_port"` - DbName string `json:"db_name"` + DBPort uint16 `json:"db_port"` + DBName string `json:"db_name"` Username string `json:"username"` Password string `json:"password"` DnBackupPath string `json:"dn_backup_path"` @@ -38,7 +38,7 @@ type ( } BackupInfo struct { - Id string `json:"dn_backup_id"` + ID string `json:"dn_backup_id"` Path string `json:"dn_backup_path"` Mode string `json:"db_backup_mode"` Instance string `json:"instance"` diff --git a/pitr/cli/internal/pkg/shardingsphere-proxy.go b/pitr/cli/internal/pkg/shardingsphere-proxy.go index f7803dfe..5fa9f2a1 100644 --- a/pitr/cli/internal/pkg/shardingsphere-proxy.go +++ b/pitr/cli/internal/pkg/shardingsphere-proxy.go @@ -44,7 +44,7 @@ type ( ) const ( - DefaultDbName = "postgres" + DefaultDBName = "postgres" ) func NewShardingSphereProxy(user, password, dbName, host string, port uint16) (IShardingSphereProxy, error) { @@ -103,6 +103,7 @@ func (ss *shardingSphereProxy) ExportMetaData() (*model.ClusterInfo, error) { if err != nil { return nil, xerr.NewCliErr(fmt.Sprintf("export meta data failure,err=%s", err)) } + var ( id string createTime string @@ -116,10 +117,15 @@ func (ss *shardingSphereProxy) ExportMetaData() (*model.ClusterInfo, error) { return nil, xerr.NewCliErr(fmt.Sprintf("query close failure,err=%s", err)) } } + if query.Err() != nil { + return nil, xerr.NewCliErr(fmt.Sprintf("query err=%s", query.Err())) + } + var out model.ClusterInfo if err = json.Unmarshal([]byte(data), &out); err != nil { return nil, fmt.Errorf("json unmarshal return err=%s", err) } + out.SnapshotInfo = nil return &out, nil } @@ -138,6 +144,7 @@ func (ss *shardingSphereProxy) ExportStorageNodes() ([]*model.StorageNode, error if err != nil { return nil, xerr.NewCliErr(fmt.Sprintf("export storage nodes failure,err=%s", err)) } + var ( id string createTime string @@ -152,6 +159,10 @@ func (ss *shardingSphereProxy) ExportStorageNodes() ([]*model.StorageNode, error return nil, xerr.NewCliErr(fmt.Sprintf("query close failure,err=%s", err)) } } + if query.Err() != nil { + return nil, xerr.NewCliErr(fmt.Sprintf("query err failure,err=%s", err)) + } + out := &model.StorageNodesInfo{} if err = json.Unmarshal([]byte(data), &out); err != nil { return nil, fmt.Errorf("json unmarshal return err=%s", err) @@ -159,14 +170,18 @@ func (ss *shardingSphereProxy) ExportStorageNodes() ([]*model.StorageNode, error // get all storage nodes and filter duplicate nodes var storageNodes []*model.StorageNode + var tmpNodesMap = make(map[string]struct{}) + for _, v := range out.StorageNodes { for _, vv := range v { // filter duplicate nodes if _, ok := tmpNodesMap[fmt.Sprintf("%s:%d", vv.IP, vv.Port)]; ok { continue } + tmpNodesMap[fmt.Sprintf("%s:%d", vv.IP, vv.Port)] = struct{}{} + storageNodes = append(storageNodes, vv) } } @@ -179,10 +194,12 @@ func (ss *shardingSphereProxy) ImportMetaData(in *model.ClusterInfo) error { if in == nil { return xerr.NewCliErr("import meta data is nil") } + marshal, err := json.Marshal(in) if err != nil { return xerr.NewCliErr(fmt.Sprintf("json marshal,invalid data[in=%+v]", in)) } + _, err = ss.db.Exec(fmt.Sprintf(`IMPORT METADATA '%s';`, marshal)) if err != nil { return xerr.NewCliErr(fmt.Sprintf("import metadata failure,err=%s", err)) diff --git a/pitr/cli/internal/pkg/xerr/err.go b/pitr/cli/internal/pkg/xerr/err.go index 6a09aeb3..5e363d65 100644 --- a/pitr/cli/internal/pkg/xerr/err.go +++ b/pitr/cli/internal/pkg/xerr/err.go @@ -19,16 +19,13 @@ package xerr import "fmt" -type ( - service string - err struct { - msg string - } -) +type err struct { + msg string +} const ( Unknown = "Unknown error" - InvalidHttpStatus = "Invalid http status" + InvalidHTTPStatus = "Invalid http status" NotFound = "Not found" ) diff --git a/pitr/cli/main.go b/pitr/cli/main.go index c20dd7f9..bbf5d75b 100644 --- a/pitr/cli/main.go +++ b/pitr/cli/main.go @@ -42,7 +42,7 @@ func main() { zap.AddStacktrace(zapcore.FatalLevel), ) if err != nil { - panic(fmt.Errorf("an unknown error occured in the zap-log")) + panic(fmt.Errorf("an unknown error occurred in the zap-log")) } logging.Init(logger) diff --git a/pitr/cli/pkg/httputils/req.go b/pitr/cli/pkg/httputils/req.go index a02787e8..7d990792 100644 --- a/pitr/cli/pkg/httputils/req.go +++ b/pitr/cli/pkg/httputils/req.go @@ -97,6 +97,7 @@ func (r *req) Send(body any) (int, error) { } tr := &http.Transport{ + //nolint:gosec TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } c := &http.Client{Transport: tr} @@ -105,6 +106,8 @@ func (r *req) Send(body any) (int, error) { return -1, fmt.Errorf("http request err=%w", err) } + defer resp.Body.Close() + all, err := io.ReadAll(resp.Body) if err != nil { return -1, fmt.Errorf("invalid response,err=%w", err) diff --git a/pitr/cli/pkg/logging/field.go b/pitr/cli/pkg/logging/field.go index 21192785..01087c04 100644 --- a/pitr/cli/pkg/logging/field.go +++ b/pitr/cli/pkg/logging/field.go @@ -34,13 +34,14 @@ func (f FieldKey) String() string { return string(f) } -const ( - ErrorKey FieldKey = "error" - RequestID FieldKey = "requestID" - Stack FieldKey = "stack" - Duration FieldKey = "duration" - Path FieldKey = "path" // original routing path - RequestUri FieldKey = "requestUri" // http requesting uri - HttpMethod FieldKey = "httpMethod" - HttpStatus FieldKey = "httpStatus" -) +// +//const ( +// ErrorKey FieldKey = "error" +// RequestID FieldKey = "requestID" +// Stack FieldKey = "stack" +// Duration FieldKey = "duration" +// Path FieldKey = "path" // original routing path +// RequestURI FieldKey = "requestUri" // http requesting uri +// HTTPMethod FieldKey = "httpMethod" +// HTTPStatus FieldKey = "httpStatus" +//) diff --git a/pitr/cli/pkg/stringutil/rand_string.go b/pitr/cli/pkg/stringutil/rand_string.go index 769b7b74..f7be6c5b 100644 --- a/pitr/cli/pkg/stringutil/rand_string.go +++ b/pitr/cli/pkg/stringutil/rand_string.go @@ -18,9 +18,8 @@ package strutil import ( - "math/rand" + "crypto/rand" "strconv" - "time" ) const ( @@ -34,20 +33,25 @@ const ( ) func Random(n uint) string { - rand.Seed(time.Now().UnixNano()) - bs := make([]byte, 0, n) + bs := make([]byte, n) + for i := uint(0); i < n; i++ { - bs = append(bs, charSet[rand.Intn(charSize)]) + _, _ = rand.Read(bs[i : i+1]) + } + for i, v := range bs { + bs[i] = charSet[int(v)%charSize] } return string(bs) } func RandomInt(n uint) int64 { - rand.Seed(time.Now().UnixNano()) - bs := make([]byte, 0, n) - bs = append(bs, digitSet[rand.Intn(digitSize-1)]) - for i := uint(0); i < n-1; i++ { - bs = append(bs, digitSet[rand.Intn(digitSize)]) + bs := make([]byte, n) + _, _ = rand.Read(bs[0:1]) + for i := uint(1); i < n; i++ { + _, _ = rand.Read(bs[i : i+1]) + } + for i, v := range bs { + bs[i] = digitSet[int(v)%digitSize] } v, _ := strconv.ParseInt(string(bs), 10, 64) return v diff --git a/pitr/cli/pkg/stringutil/rand_string_test.go b/pitr/cli/pkg/stringutil/rand_string_test.go index f2e37201..098171b4 100644 --- a/pitr/cli/pkg/stringutil/rand_string_test.go +++ b/pitr/cli/pkg/stringutil/rand_string_test.go @@ -17,7 +17,9 @@ package strutil -import "testing" +import ( + "testing" +) func TestRandom(t *testing.T) { one, two := Random(20), Random(20)