1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-02-08 01:20:07 +08:00

fit:replace ctx err equal check as error.is (#5588)

This commit is contained in:
yonwoo9 2024-06-27 17:47:10 +08:00 committed by GitHub
parent 070484772b
commit a6367fd96b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 11 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package mysql
import ( import (
"context" "context"
"database/sql" "database/sql"
"errors"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -54,7 +55,7 @@ func NewMysqlClient(conn client.DBInfo) (MysqlClient, error) {
global.LOG.Errorf("test mysql conn failed, err: %v", err) global.LOG.Errorf("test mysql conn failed, err: %v", err)
return nil, err return nil, err
} }
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, buserr.New(constant.ErrExecTimeOut) return nil, buserr.New(constant.ErrExecTimeOut)
} }

View File

@ -350,7 +350,7 @@ func (r *Local) ExecSQL(command string, timeout uint) error {
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, "docker", itemCommand...) cmd := exec.CommandContext(ctx, "docker", itemCommand...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return buserr.New(constant.ErrExecTimeOut) return buserr.New(constant.ErrExecTimeOut)
} }
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "") stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")
@ -367,7 +367,7 @@ func (r *Local) ExecSQLForRows(command string, timeout uint) ([]string, error) {
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, "docker", itemCommand...) cmd := exec.CommandContext(ctx, "docker", itemCommand...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, buserr.New(constant.ErrExecTimeOut) return nil, buserr.New(constant.ErrExecTimeOut)
} }
stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "") stdStr := strings.ReplaceAll(string(stdout), "mysql: [Warning] Using a password on the command line interface can be insecure.\n", "")

View File

@ -387,7 +387,7 @@ func (r *Remote) ExecSQL(command string, timeout uint) error {
if _, err := r.Client.ExecContext(ctx, command); err != nil { if _, err := r.Client.ExecContext(ctx, command); err != nil {
return err return err
} }
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return buserr.New(constant.ErrExecTimeOut) return buserr.New(constant.ErrExecTimeOut)
} }
@ -402,7 +402,7 @@ func (r *Remote) ExecSQLForHosts(timeout uint) ([]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, buserr.New(constant.ErrExecTimeOut) return nil, buserr.New(constant.ErrExecTimeOut)
} }
var rows []string var rows []string

View File

@ -204,7 +204,7 @@ func (r *Local) ExecSQL(command string, timeout uint) error {
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, "docker", itemCommand...) cmd := exec.CommandContext(ctx, "docker", itemCommand...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return buserr.New(constant.ErrExecTimeOut) return buserr.New(constant.ErrExecTimeOut)
} }
if err != nil || strings.HasPrefix(string(stdout), "ERROR ") { if err != nil || strings.HasPrefix(string(stdout), "ERROR ") {
@ -220,7 +220,7 @@ func (r *Local) ExecSQLForRows(command string, timeout uint) ([]string, error) {
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, "docker", itemCommand...) cmd := exec.CommandContext(ctx, "docker", itemCommand...)
stdout, err := cmd.CombinedOutput() stdout, err := cmd.CombinedOutput()
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, buserr.New(constant.ErrExecTimeOut) return nil, buserr.New(constant.ErrExecTimeOut)
} }
if err != nil || strings.HasPrefix(string(stdout), "ERROR ") { if err != nil || strings.HasPrefix(string(stdout), "ERROR ") {

View File

@ -223,7 +223,7 @@ func (r *Remote) SyncDB() ([]SyncDBInfo, error) {
} }
datas = append(datas, SyncDBInfo{Name: dbName, From: r.From, PostgresqlName: r.Database}) datas = append(datas, SyncDBInfo{Name: dbName, From: r.From, PostgresqlName: r.Database})
} }
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return nil, buserr.New(constant.ErrExecTimeOut) return nil, buserr.New(constant.ErrExecTimeOut)
} }
return datas, nil return datas, nil
@ -240,7 +240,7 @@ func (r *Remote) ExecSQL(command string, timeout uint) error {
if _, err := r.Client.ExecContext(ctx, command); err != nil { if _, err := r.Client.ExecContext(ctx, command); err != nil {
return err return err
} }
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return buserr.New(constant.ErrExecTimeOut) return buserr.New(constant.ErrExecTimeOut)
} }
@ -300,7 +300,7 @@ func loadImageTag() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel() defer cancel()
if _, err := client.ImagePull(ctx, itemTag, image.PullOptions{}); err != nil { if _, err := client.ImagePull(ctx, itemTag, image.PullOptions{}); err != nil {
if ctx.Err() == context.DeadlineExceeded { if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return itemTag, buserr.New(constant.ErrPgImagePull) return itemTag, buserr.New(constant.ErrPgImagePull)
} }
global.LOG.Errorf("image %s pull failed, err: %v", itemTag, err) global.LOG.Errorf("image %s pull failed, err: %v", itemTag, err)