From ceac3a4110d1db0080ec9e80c414f9dc76666e28 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong (cubxxw)" <3293172751nss@gmail.com> Date: Wed, 27 Mar 2024 12:31:57 +0800 Subject: [PATCH] fix: remove pkg file --- pkg/common/http/http_client.go | 69 ------------------------- pkg/common/tls/doc.go | 15 ------ pkg/common/tls/tls.go | 88 -------------------------------- pkg/common/version/base.go | 61 ---------------------- pkg/common/version/doc.go | 15 ------ pkg/common/version/types.go | 44 ---------------- pkg/common/version/version.go | 72 -------------------------- pkg/util/genutil/doc.go | 15 ------ pkg/util/genutil/genutil.go | 73 -------------------------- pkg/util/genutil/genutil_test.go | 40 --------------- 10 files changed, 492 deletions(-) delete mode 100644 pkg/common/tls/doc.go delete mode 100755 pkg/common/tls/tls.go delete mode 100644 pkg/common/version/base.go delete mode 100644 pkg/common/version/doc.go delete mode 100644 pkg/common/version/types.go delete mode 100644 pkg/common/version/version.go delete mode 100644 pkg/util/genutil/doc.go delete mode 100644 pkg/util/genutil/genutil.go delete mode 100644 pkg/util/genutil/genutil_test.go diff --git a/pkg/common/http/http_client.go b/pkg/common/http/http_client.go index cd4dcce0c..c447433af 100644 --- a/pkg/common/http/http_client.go +++ b/pkg/common/http/http_client.go @@ -15,16 +15,13 @@ package http import ( - "bytes" "context" "encoding/json" - "io" "net/http" "time" "github.com/openimsdk/open-im-server/v3/pkg/callbackstruct" "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/openimsdk/protocol/constant" "github.com/openimsdk/tools/errs" "github.com/openimsdk/tools/log" ) @@ -41,72 +38,6 @@ func init() { http.DefaultTransport.(*http.Transport).MaxConnsPerHost = 100 // default: 2 } -func Get(url string) (response []byte, err error) { - hclient := http.Client{Timeout: 5 * time.Second} - resp, err := hclient.Get(url) - if err != nil { - return nil, err - } - - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, errs.WrapMsg(err, "failed to read response body", "url", url) - } - return body, nil -} - -func Post(ctx context.Context, url string, header map[string]string, data any, timeout int) (content []byte, err error) { - if timeout > 0 { - var cancel func() - ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(timeout)) - defer cancel() - } - - jsonStr, err := json.Marshal(data) - if err != nil { - return nil, errs.WrapMsg(err, "Post: JSON marshal failed") - } - - req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(jsonStr)) - if err != nil { - return nil, errs.WrapMsg(err, "Post: NewRequestWithContext failed") - } - - if operationID, _ := ctx.Value(constant.OperationID).(string); operationID != "" { - req.Header.Set(constant.OperationID, operationID) - } - for k, v := range header { - req.Header.Set(k, v) - } - req.Header.Add("content-type", "application/json; charset=utf-8") - - resp, err := client.Do(req) - if err != nil { - return nil, errs.WrapMsg(err, "Post: client.Do failed") - } - defer resp.Body.Close() - - result, err := io.ReadAll(resp.Body) - if err != nil { - return nil, errs.WrapMsg(err, "Post: ReadAll failed") - } - - return result, nil -} - -func PostReturn(ctx context.Context, url string, header map[string]string, input, output any, timeOutSecond int) error { - b, err := Post(ctx, url, header, input, timeOutSecond) - if err != nil { - return err - } - err = json.Unmarshal(b, output) - if err != nil { - return errs.WrapMsg(err, "PostReturn: JSON unmarshal failed") - } - return nil -} - func callBackPostReturn(ctx context.Context, url, command string, input interface{}, output callbackstruct.CallbackResp, callbackConfig config.CallBackConfig) error { url = url + "/" + command log.ZInfo(ctx, "callback", "url", url, "input", input, "config", callbackConfig) diff --git a/pkg/common/tls/doc.go b/pkg/common/tls/doc.go deleted file mode 100644 index fcf8067b1..000000000 --- a/pkg/common/tls/doc.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package tls // import "github.com/openimsdk/open-im-server/v3/pkg/common/tls" diff --git a/pkg/common/tls/tls.go b/pkg/common/tls/tls.go deleted file mode 100755 index 6c556f27f..000000000 --- a/pkg/common/tls/tls.go +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright © 2023 OpenIM. All rights reserved. -// -// 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. - -package tls - -import ( - "crypto/tls" - "crypto/x509" - "encoding/pem" - "errors" - "os" - - "github.com/openimsdk/tools/errs" -) - -// decryptPEM decrypts a PEM block using a password. -func decryptPEM(data []byte, passphrase []byte) ([]byte, error) { - if len(passphrase) == 0 { - return data, nil - } - b, _ := pem.Decode(data) - d, err := x509.DecryptPEMBlock(b, passphrase) - if err != nil { - return nil, err - } - return pem.EncodeToMemory(&pem.Block{ - Type: b.Type, - Bytes: d, - }), nil -} - -func readEncryptablePEMBlock(path string, pwd []byte) ([]byte, error) { - data, err := os.ReadFile(path) - if err != nil { - return nil, err - } - return decryptPEM(data, pwd) -} - -// NewTLSConfig setup the TLS config from general config file. -func NewTLSConfig(clientCertFile, clientKeyFile, caCertFile string, keyPwd []byte, insecureSkipVerify bool) (*tls.Config, error) { - tlsConfig := tls.Config{} - - if clientCertFile != "" && clientKeyFile != "" { - certPEMBlock, err := os.ReadFile(clientCertFile) - if err != nil { - return nil, errs.WrapMsg(err, "NewTLSConfig: failed to read client cert file") - } - keyPEMBlock, err := readEncryptablePEMBlock(clientKeyFile, keyPwd) - if err != nil { - return nil, err - } - - cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock) - if err != nil { - return nil, errs.WrapMsg(err, "NewTLSConfig: failed to create X509 key pair") - } - tlsConfig.Certificates = []tls.Certificate{cert} - } - - if caCertFile != "" { - caCert, err := os.ReadFile(caCertFile) - if err != nil { - return nil, errs.WrapMsg(err, "NewTLSConfig: failed to read CA cert file") - } - - caCertPool := x509.NewCertPool() - if ok := caCertPool.AppendCertsFromPEM(caCert); !ok { - return nil, errors.New("NewTLSConfig: not a valid CA cert") - } - tlsConfig.RootCAs = caCertPool - } - - tlsConfig.InsecureSkipVerify = insecureSkipVerify - - return &tlsConfig, nil -} diff --git a/pkg/common/version/base.go b/pkg/common/version/base.go deleted file mode 100644 index 40c273776..000000000 --- a/pkg/common/version/base.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright © 2023 OpenIM. All rights reserved. -// -// 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. - -package version - -// Base version information. -// -// This is the fallback data used when version information from git is not -// provided via go ldflags. It provides an approximation of the Kubernetes -// version for ad-hoc builds (e.g. `go build`) that cannot get the version -// information from git. -// -// If you are looking at these fields in the git tree, they look -// strange. They are modified on the fly by the build process. The -// in-tree values are dummy values used for "git archive", which also -// works for GitHub tar downloads. -// -// When releasing a new Kubernetes version, this file is updated by -// build/mark_new_version.sh to reflect the new version, and then a -// git annotated tag (using format vX.Y where X == Major version and Y -// == Minor version) is created to point to the commit that updates. -var ( - // TODO: Deprecate gitMajor and gitMinor, use only gitVersion - // instead. First step in deprecation, keep the fields but make - // them irrelevant. (Next we'll take it out, which may muck with - // scripts consuming the kubectl version output - but most of - // these should be looking at gitVersion already anyways.) - gitMajor string = "" // major version, always numeric - gitMinor string = "" // minor version, numeric possibly followed by "+" - - // Semantic version, derived by build scripts (see - // https://github.com/kubernetes/sig-release/blob/master/release-engineering/versioning.md#kubernetes-release-versioning - // https://kubernetes.io/releases/version-skew-policy/ - // for a detailed discussion of this field) - // - // TODO: This field is still called "gitVersion" for legacy - // reasons. For prerelease versions, the build metadata on the - // semantic version is a git hash, but the version itself is no - // longer the direct output of "git describe", but a slight - // translation to be semver compliant. - - // NOTE: The $Format strings are replaced during 'git archive' thanks to the - // companion .gitattributes file containing 'export-subst' in this same - // directory. See also https://git-scm.com/docs/gitattributes - gitVersion string = "latest" - gitCommit string = "" // sha1 from git, output of $(git rev-parse HEAD) - gitTreeState string = "" // state of git tree, either "clean" or "dirty" - - buildDate string = "1970-01-01T00:00:00Z" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ') -) diff --git a/pkg/common/version/doc.go b/pkg/common/version/doc.go deleted file mode 100644 index a41ce8d13..000000000 --- a/pkg/common/version/doc.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package version // import "github.com/openimsdk/open-im-server/v3/pkg/common/version" diff --git a/pkg/common/version/types.go b/pkg/common/version/types.go deleted file mode 100644 index 6753c49a7..000000000 --- a/pkg/common/version/types.go +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright © 2023 OpenIM. All rights reserved. -// -// 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. - -package version - -// Info contains versioning information. -// TODO: Add []string of api versions supported? It's still unclear -// how we'll want to distribute that information. -type Info struct { - Major string `json:"major,omitempty"` - Minor string `json:"minor,omitempty"` - GitVersion string `json:"gitVersion"` - GitTreeState string `json:"gitTreeState,omitempty"` - GitCommit string `json:"gitCommit,omitempty"` - BuildDate string `json:"buildDate"` - GoVersion string `json:"goVersion"` - Compiler string `json:"compiler"` - Platform string `json:"platform"` -} - -type Output struct { - OpenIMServerVersion Info `json:"OpenIMServerVersion,omitempty" yaml:"OpenIMServerVersion,omitempty"` - OpenIMClientVersion *OpenIMClientVersion `json:"OpenIMClientVersion,omitempty" yaml:"OpenIMClientVersion,omitempty"` -} - -type OpenIMClientVersion struct { - ClientVersion string `json:"clientVersion,omitempty" yaml:"clientVersion,omitempty"` // sdk core version -} - -// String returns info as a human-friendly version string. -func (info Info) String() string { - return info.GitVersion -} diff --git a/pkg/common/version/version.go b/pkg/common/version/version.go deleted file mode 100644 index 3b271b3f6..000000000 --- a/pkg/common/version/version.go +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright © 2023 OpenIM. All rights reserved. -// -// 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. - -package version - -import ( - "fmt" - "runtime" - - "gopkg.in/src-d/go-git.v4" -) - -// Get returns the overall codebase version. It's for detecting -// what code a binary was built from. -func Get() Info { - // These variables typically come from -ldflags settings and in - // their absence fallback to the settings in ./base.go - return Info{ - Major: gitMajor, - Minor: gitMinor, - GitVersion: gitVersion, - GitTreeState: gitTreeState, - GitCommit: gitCommit, - BuildDate: buildDate, - GoVersion: runtime.Version(), - Compiler: runtime.Compiler, - Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), - } -} - -// GetClientVersion returns the git version of the OpenIM client repository. -func GetClientVersion() (*OpenIMClientVersion, error) { - clientVersion, err := getClientVersion() - if err != nil { - return nil, err - } - return &OpenIMClientVersion{ - ClientVersion: clientVersion, - }, nil -} - -func getClientVersion() (string, error) { - repo, err := git.PlainClone("/tmp/openim-sdk-core", false, &git.CloneOptions{ - URL: "https://github.com/OpenIMSDK/openim-sdk-core", - }) - if err != nil { - return "", fmt.Errorf("error cloning repository: %w", err) - } - - ref, err := repo.Head() - if err != nil { - return "", fmt.Errorf("error getting head reference: %w", err) - } - - return ref.Hash().String(), nil -} - -// GetSingleVersion returns single version of sealer. -func GetSingleVersion() string { - return gitVersion -} diff --git a/pkg/util/genutil/doc.go b/pkg/util/genutil/doc.go deleted file mode 100644 index aa9efeb56..000000000 --- a/pkg/util/genutil/doc.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package genutil // import "github.com/openimsdk/open-im-server/v3/pkg/util/genutil" diff --git a/pkg/util/genutil/genutil.go b/pkg/util/genutil/genutil.go deleted file mode 100644 index 5fb2443a6..000000000 --- a/pkg/util/genutil/genutil.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package genutil - -import ( - "fmt" - "os" - "path/filepath" - "strings" - "time" - - "github.com/openimsdk/tools/errs" -) - -// OutDir creates the absolute path name from path and checks if the path exists and is a directory. -// Returns absolute path including trailing '/' or error if the path does not exist or is not a directory. -func OutDir(path string) (string, error) { - outDir, err := filepath.Abs(path) - if err != nil { - return "", errs.WrapMsg(err, "failed to resolve absolute path", "path", path) - } - - stat, err := os.Stat(outDir) - if err != nil { - if os.IsNotExist(err) { - return "", errs.WrapMsg(err, "output directory does not exist", "path", outDir) - } - return "", errs.WrapMsg(err, "failed to stat output directory", "path", outDir) - } - - if !stat.IsDir() { - return "", errs.Wrap(fmt.Errorf("specified path %s is not a directory", outDir)) // Correctly constructs a new error as 'err' would be nil here - } - outDir += "/" - return outDir, nil -} - -func GetCurrentTimeFormatted() string { - return time.Now().Format("2006-01-02 15:04:05") -} - -func ExitWithError(err error) { - progName := filepath.Base(os.Args[0]) - fmt.Fprintf(os.Stderr, "%s exit -1: %+v\n", progName, err) - os.Exit(-1) -} -func GetProcessName() string { - args := os.Args - if len(args) > 0 { - segments := strings.Split(args[0], "/") - if len(segments) > 0 { - return segments[len(segments)-1] - } - } - return "" -} - -func SIGTERMExit() { - progName := filepath.Base(os.Args[0]) - fmt.Fprintf(os.Stderr, "Warning %s receive process terminal SIGTERM exit 0\n", progName) -} diff --git a/pkg/util/genutil/genutil_test.go b/pkg/util/genutil/genutil_test.go deleted file mode 100644 index 050d14040..000000000 --- a/pkg/util/genutil/genutil_test.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package genutil - -import ( - "testing" -) - -func TestValidDir(t *testing.T) { - _, err := OutDir("./") - if err != nil { - t.Fatal(err) - } -} - -func TestInvalidDir(t *testing.T) { - _, err := OutDir("./nondir") - if err == nil { - t.Fatal("expected an error") - } -} - -func TestNotDir(t *testing.T) { - _, err := OutDir("./genutils_test.go") - if err == nil { - t.Fatal("expected an error") - } -}