feat(util): add util function implements

with #22357426

Signed-off-by: Cloudstriff <chenjiongwendao@qq.com>
This commit is contained in:
Cloudstriff 2024-07-11 18:59:21 +08:00 committed by slasher
parent 562c16fc63
commit db9ffe9d1c
2 changed files with 118 additions and 0 deletions

77
blobstore/util/util.go Normal file
View File

@ -0,0 +1,77 @@
// Copyright 2023 The Cuber Authors.
//
// 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 util
import (
"errors"
"net"
"os"
"unsafe"
"github.com/google/uuid"
)
// GenTmpPath create a temporary path
func GenTmpPath() (string, error) {
id := uuid.NewString()
path := os.TempDir() + "/" + id
if err := os.RemoveAll(path); err != nil {
return "", err
}
if err := os.MkdirAll(path, 0o755); err != nil {
return "", err
}
return path, nil
}
func StringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
func BytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
func GetLocalIP() (string, error) {
addresses, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addresses {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", errors.New("can not find the local ip address")
}
func GenUnusedPort() int {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}

View File

@ -0,0 +1,41 @@
// Copyright 2023 The Cuber Authors.
//
// 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 util
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestGenTmpPath(t *testing.T) {
path, err := GenTmpPath()
require.NoError(t, err)
require.NotEqual(t, "", path)
}
func TestStringsToBytes(t *testing.T) {
str := "test"
b := StringToBytes(str)
require.Equal(t, str, string(b))
require.Equal(t, 0, len(StringToBytes("")))
}
func TestBytesToString(t *testing.T) {
b := []byte("test")
str := BytesToString(b)
require.Equal(t, str, string(b))
require.Equal(t, "", BytesToString(nil))
}